MyBatis 第一个程序

使用 MyBatis 操作数据库的第一个程序。

一、数据库

user 表

表结构如下:

表数据如下:

二、项目结构

三、项目文件

1. pom.xml

  • 添加依赖
  • 手动配置,以在编译时导出 java 目录下的 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
37
38
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
<!--MySQL驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!--MyBatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>

2. mybatis-config.xml

配置数据库相关信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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/idb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="dao/UserMapper.xml"/>
</mappers>
</configuration>

3. User.java

User 实体类

1
2
3
4
5
6
7
8
9
10
11
12
public class User {
private int id;
private String username;
private String password;

// 构造方法

// Getter and Setter

// 重写 toString() 方法

}

4. MyBatisUtils.java

  • 构建 SqlSessionFactory
  • 提供获取 SqlSession 实例的类方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class MyBatisUtils {

private static SqlSessionFactory sqlSessionFactory;

static {
try {
// 获取 SqlSessionFactory 对象
String resource = "mybatis-config.xml";
InputStream resourceInputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceInputStream);
} catch (IOException e) {
e.printStackTrace();
}
}

// 获取 SqlSession 实例
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}

5. UserMapper.java

抽象类,定义数据操作方法

1
2
3
public interface UserMapper {
public List<User> getUserList();
}

6. UserMapper.xml

定义 SQL 映射

1
2
3
4
5
6
7
8
9
<?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="dao.UserMapper">
<select id="getUserList" resultType="pojo.User">
select * from user
</select>
</mapper>

四、运行测试

1. UserMapperTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class UserMapperTest {
@Test
public void testGetUserList() {
// 获取 sqlSession 对象
SqlSession sqlSession = MyBatisUtils.getSqlSession();
// 获取 mapper 实例
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 调用方法,执行 SQL
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
// 关闭 sqlSession
sqlSession.close();
}
}

2. 运行结果

参考