maven 配置 mybatis

使用 idea 通过 maven 进行配置 mybatis

创建项目

首先打开 idea,然后创建空的 maven 项目

配置依赖

首先到 mvnrepository 中搜索 mysqlmybatisjunit,并将它们的相关配置代码复制到 pom.xml 中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

完成以后可以看见右上角出现了个小图标,点击一下,就会自动下载相关的依赖了

创建数据库

在配置 mybatis 前,应该先把数据库建好

1
2
3
4
5
6
7
8
9
10
CREATE SCHEMA `mybatis` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin ;
CREATE TABLE `mybatis`.`test` (
`id` VARCHAR(45) NOT NULL,
`name` VARCHAR(45) NOT NULL,
`age` INT NULL DEFAULT 18,
PRIMARY KEY (`id`),
UNIQUE INDEX `id_UNIQUE` (`id` ASC) VISIBLE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_bin;

配置 mybatis

src/main/resources 目录下新建一个 .xml 文件,名字任意,一般使用 mybatis-config.xml ,但我这里就使用 db-config.xml 作为名字了

首先写入 mybatis 的约束信息

1
2
3
<?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">

然后写入数据库连接参数的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<!-- mysql5 要改成 com.mysql.jdbc.Driver -->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<!-- 只需要写这个,其余的是怕出现其他奇奇怪怪的错误 -->
<!-- jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8 -->
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&amp;useSSL=false&amp;useUnicode=true&amp;serverTimezone=UTC"/>
<!-- mysql 用户名 -->
<property name="username" value="root"/>
<!-- mysql 密码 -->
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
</configuration>

也可以将连接信息单独提取出来,新建 db.properties ,这个名字也是任意的,将链接信息填入,这里的变量名也是任意的,但最好要知道是干什么的

1
2
3
4
5
# mysql
mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&useUnicode=true&serverTimezone=UTC
mysql.username=root
mysql.password=root

然后修改刚刚的 db-config.xml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<configuration>
<!-- 导入连接信息 -->
<properties resource="db.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<!-- 将 value 改成对应的变量 -->
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment>
</environments>
</configuration>

mybatis 程序

写完配置就可以进行编写 mybatis 程序了

创建 pojo 实体类

src/main/java 下建 pojo 包,我这里叫 exam.pojo ,并创建实体类,实体类的内容要与数据库对应

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
package exam.pojo;

public class TestPojo {
private String id;
private String name;
private int age;

public TestPojo() {
}

public TestPojo(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}

public void setId(String id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

public void setAge(int age) {
this.age = age;
}

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

创建 mapper

src/main/java下建 mapper包,并创建 mapper 接口

1
2
3
4
5
6
7
8
9
package exam.mapper;

import exam.pojo.TestPojo;

import java.util.List;

public interface TestMapper {
public TestPojo selectOne(String id);
}

src/main/java 或者 src/main/resources 下建 mapper 包,并创建 mapper 文件,我这里叫 TestMapper.xml

1
2
3
4
<?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">

导入约束

1
2
3
4
5
6
7
<mapper namespace="exam.mapper.TestMapper">
<select id="selectOne" parameterType="String" resultType="exam.pojo.TestPojo">
select *
from test
where id = #{id};
</select>
</mapper>

编写测试类

src/test/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
32
33
package exam.mapper;

import exam.pojo.TestPojo;
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.Reader;

public class MyTest {
@Test
public void selectOne() {
// 引入配置文件
String resource = "db-config.xml";
Reader reader = null;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
e.printStackTrace();
}
// 创建 SqlSessionFactoryBuilder
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(reader);
// 创建 SqlSession
SqlSession sqlSession = build.openSession();
TestMapper mapper = sqlSession.getMapper(TestMapper.class);
TestPojo testPojo = mapper.selectOne("123");
System.out.println(testPojo);
sqlSession.close();
}
}

不过在测试前先往数据库里加点数据

1
INSERT INTO `mybatis`.`test` (`id`, `name`, `age`) VALUES ('123', '张三', '22');

然后到 db-config.xml 中注册 TestMapper

<environments></environments> 下方加入以下内容

1
2
3
<mappers>
<mapper resource="exam/mapper/TestMapper.xml"/>
</mappers>

如果运行时报错找不到配置文件,需要到 pom.xml 中加入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>

运行测试类

点击这个绿色小按钮即可运行

输出结果表示成功,如果不成功就要留意一下是不是配置文件写错了