热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

java——mybatis——Mybatis注解开发——@Select查询——查询所有数据

pom.xml配置:

pom.xml配置:

xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>sunxlartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.27version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.1version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4jartifactId>
<version>2.17.2version>
dependency>
dependencies>
project>

 

 

 

 

 

 

 

 

 

 

 

mybatis-config.xml配置:

xml version="1.0" encoding="UTF-8"?>
DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"
>
<configuration>
<typeAliases>
<package name="com.sunxl.pojo"/>
typeAliases>

<environments default="development">
<environment id="development">

<transactionManager type="JDBC" />
<dataSource type="POOLED">

<property name="driver" value="com.mysql.jdbc.Driver" />

<property name="url" value="jdbc:mysql://localhost:3306/mysql8?characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="123456" />
dataSource>
environment>
environments>

<mappers>
<package name="com.sunxl.dao">package>
mappers>
configuration>

 

 

 

 

 

 

 

 

 

 

user的pojo类:

package com.sunxl.pojo;
public class User {
Integer id;
String name;
Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Users{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

DAO接口文件:

package com.sunxl.dao;
import com.sunxl.pojo.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface UserDao {
@Select(
"select * from users")
List
findAll();
// int saveUser(User user);
//
// int updateUser(User user);
//
// int deleteUser(Integer id);
//
// User findById(Integer id);
//
// List findByName(String name);
//
// int findTotal();
//
// int saveInsertUser(User user);

}

 

 

 

 

 

 

 

 

 

 

 

测试结果:

import com.sunxl.dao.UserDao;
import com.sunxl.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MybatisTest {
public static void main(String[] args) throws IOException {
InputStream config
= Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder builder
= new SqlSessionFactoryBuilder();
SqlSessionFactory factory
= builder.build(config);
SqlSession ss
= factory.openSession();
UserDao userdao
= ss.getMapper(UserDao.class);
List
users = userdao.findAll();
for (User user: users){
System.out.println(user);
}
ss.commit();
ss.close();
config.close();
}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

表结构:

 



推荐阅读
author-avatar
冷香之流光氤氲
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有