create table tb_user(
id int primary key auto_increment,
username varchar(20) not null,
birthday date,
sex char(1) default '男',
address varchar(50)
);
insert into tb_user values(null,'孙悟空','1980-10-24','男','花果山水帘洞');
insert into tb_user values(null,'白骨精','1992-11-12','女','白虎岭白虎洞');
insert into tb_user values(null,'猪八戒','1983-05-20','男','福临山云洞');
insert into tb_user values(null,'蜘蛛清','1995-10-21','女','盘丝洞');
<?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.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>mybatis_day01</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<!--依赖管理-->
<dependencies>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
<?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>
<!--配置数据源(数据库连接)-->
<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/hsp_db02?useUnicode=true&characterEncoding=utf-8&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--映射映射相关文件-->
<mappers>
<mapper resource="com/itheima/dao/UserMapper.xml"/>
</mappers>
</configuration>
package com.itheima.dao;
import com.itheima.pojo.User;
import java.util.List;
//dao层
public interface UserMapper {
/**
* 功能:查询所有的用户
* @return
*/
public List<User> findAllUser();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--利用namespace属性,绑定当前映射文件和mapper接口映射关联-->
<mapper namespace="com.itheima.dao.UserMapper">
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--利用namespace属性,绑定当前映射文件和mapper接口映射关联-->
<mapper namespace="com.itheima.dao.UserMapper">
<!--SQL查询文件-->
<select id="findAllUser" resultType="com.itheima.pojo.User">
select id,username,birthday,sex,address from tb_user
</select>
</mapper>
package com.itheima.pojo;
import java.util.Date;
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
public User() {
}
public User(Integer id, String username, Date birthday, String sex, String address) {
this.id = id;
this.username = username;
this.birthday = birthday;
this.sex = sex;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
//配置文件路径
String resource = "mybatis-config.xml";
//基于配置文件路径,创建字节输入流
InputStream is = Resources.getResourceAsStream(resource);
//创建SqlSessionFactory工厂类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//使用SqlSessionFactory工厂类,创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//利用sqlSession,创建一个代理对象
UserMapper sessionMapper = sqlSession.getMapper(UserMapper.class);
//使用代理对象和方法查询用户数据
List<User> userList = sessionMapper.findAllUser();
//测试用户数据
for (User user:userList){
System.out.println(user);
}
完整代码如下
package com.itheima.test;
import com.itheima.dao.UserMapper;
import com.itheima.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 org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MybatisTest {
@Test
public void testFindAllUser() throws IOException {
//配置文件路径
String resource = "mybatis-config.xml";
//基于配置文件路径,创建字节输入流
InputStream is = Resources.getResourceAsStream(resource);
//创建SqlSessionFactory工厂类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//使用SqlSessionFactory工厂类,创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//利用sqlSession,创建一个代理对象
UserMapper sessionMapper = sqlSession.getMapper(UserMapper.class);
//使用代理对象和方法查询用户数据
List<User> userList = sessionMapper.findAllUser();
//测试用户数据
for (User user:userList){
System.out.println(user);
}
}
}
测试的结果如下:
D:\jdk17\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IntelliJ IDEA 2021.3.3\lib\idea_rt.jar=49230:D:\IntelliJ IDEA 2021.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\IntelliJ IDEA 2021.3.3\lib\idea_rt.jar;D:\IntelliJ IDEA 2021.3.3\plugins\junit\lib\junit5-rt.jar;D:\IntelliJ IDEA 2021.3.3\plugins\junit\lib\junit-rt.jar;E:\Linuxshare\mybatis_day01\target\classes;D:\maven_repository\mysql\mysql-connector-java\5.1.47\mysql-connector-java-5.1.47.jar;D:\maven_repository\org\mybatis\mybatis\3.5.1\mybatis-3.5.1.jar;D:\maven_repository\junit\junit\4.12\junit-4.12.jar;D:\maven_repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.itheima.test.MybatisTest,testFindAllUser
User{id=1, username='孙悟空', birthday=Fri Oct 24 00:00:00 CST 1980, sex='男', address='花果山水帘洞'}
User{id=2, username='白骨精', birthday=Thu Nov 12 00:00:00 CST 1992, sex='女', address='白虎岭白虎洞'}
User{id=3, username='猪八戒', birthday=Fri May 20 00:00:00 CST 1983, sex='男', address='福临山云洞'}
User{id=4, username='蜘蛛清', birthday=Sat Oct 21 00:00:00 CST 1995, sex='女', address='盘丝洞'}
Process finished with exit code 0