MyBatis 初体验
MyBatis 初体验
记录第一次在项目中引入 MyBatis
准备工具:
-
IDEA
-
Maven
-
MySQL
具体步骤:
-
搭建一个Maven项目,导入依赖
本次测试需要三个依赖,分别是mybatis、mysql-connector-java 和 junit
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
-
构建 mybatis 系统的核心配置文件 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>
<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/database?useSSL=true&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--此处的UserMapper.xml对应于第6步-->
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
-
创建获取 SqlSession 的工具类 MybatisUtils
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;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
String resource = "mybatis-config.xml";
try {
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
-
创建数据库表以及对应的实体类
- 创建数据库表
CREATE TABLE `user` (
`id` int(10) NOT NULL,
`name` varchar(20) NOT NULL,
`pwd` varchar(30) DEFAULT '123456',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
在创建表之后插入几条测试记录
- 创建对应的实体类
public class User {
private int id;
private String name;
private String pwd;
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
-
创建操作数据库的接口 UserDao
import java.util.List;
public interface UserDao {
List<User> getUserList();
}
- 使用 xml 映射语句 UserMapper.xml
<?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和resultType处需要填写全路径名,id的值是接口 UserDao 的方法名-->
<mapper namespace="package_name.UserDao">
<select id="getUserList" resultType="package_name.User">
select * from user;
</select>
</mapper>
- 使用 junit 进行测试
@Test
public void test() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
控制台输出结果:
完
参考资料:
[1] 狂神说Java.Mybatis最新完整教程IDEA版通俗易懂.bilibili,2019-10-02:https://www.bilibili.com/video/BV1NE411Q7Nx?p=2
[2] mybatis,2021-04-26:https://mybatis.org/mybatis-3/zh/getting-started.html