【代码】MyBatis面向接口开发快速入门

前言

MyBatis面向接口开发快速入门

引入依赖

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
</dependencies>

创建实体类

  • 根据数据表结构创建实体类

src/main/java/com/pojo/User.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.pojo;

public class User {

private Integer id;
private String name;

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;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}

创建一个接口

  • 在接口中定义用于执行sql语句的抽象方法

src/main/java/com/Mapper/UserMapper.java

1
2
3
4
5
6
7
8
package com.Mapper;

import com.pojo.User;
import java.util.List;

public interface UserMapper {
List<User> selectAll();
}

创建sql映射xml

  • 映射xml文件的路径要与接口路径相同,但不要求存在相同目录下,只要路径值相同,编译后maven会将映射xml和接口放到相同目录下
  • <mapper></mapper>中的namespace属性值要与接口的全局限定名相同

src/main/resources/com/Mapper/UserMapper.xml

1
2
3
4
5
6
7
8
9
10
<?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">

<mapper namespace="com.Mapper.UserMapper">
<select id="selectAll" resultType="com.pojo.User">
select * from user
</select>
</mapper>

创建MyBatis配置

  • <mappers></mappers>的配置要写映射xml文件的位置

src/main/resources/mybatis-config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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">

<!-- MyBatis的全局配置文件 -->
<configuration >
<!-- 1.配置环境,可配置多个环境(比如:develop开发、test测试) -->
<environments default="develop">
<environment id="develop">

<!-- 1.1.配置事务管理方式:JDBC/MANAGED
JDBC:将事务交给JDBC管理(推荐)
MANAGED:自己管理事务
-->
<transactionManager type="JDBC"></transactionManager>

<!-- 1.2.配置数据源,即连接池 JNDI/POOLED/UNPOOLED
JNDI:已过时
POOLED:使用连接池(推荐)
UNPOOLED:不使用连接池
-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///demo?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>

<!-- 2.导入Mapper配置文件,如果mapper文件有多个,可以通过多个mapper标签导入 -->
<mappers>
<mapper resource="com/Mapper/UserMapper.xml"/>
</mappers>
</configuration>

测试类

com/Demo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Demo {

public static void main(String[] args) throws IOException {
// 1. 读取mybatis的核心配置文件(mybatis-config.xml)
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
// 2. 通过配置信息获取一个SqlSessionFactory工厂对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory fac = sqlSessionFactoryBuilder.build(in);
// 3. 通过工厂获取一个SqlSession对象
SqlSession session = fac.openSession();

// 4. 通过反射获取代理接口
UserMapper userMapper = session.getMapper(UserMapper.class);
// 5. 通过代理接口调用映射xml的sql语句
List<User> users = userMapper.selectAll();
// 输出测试
System.out.println(users);
}
}

完成

参考文献

哔哩哔哩——黑马程序员