# Mybatis配置文件
# 数据库连接配置:
spring:
datasource:
username: root
password: Dream@123
url: jdbc:mysql://101.133.154.18:3306/mybatis_demo?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
connection-timeout: 30000
maximum-pool-size: 20
max-lifetime: 1800000
minimum-idle: 10
idle-timeout: 600000
mybatis:
mapper-locations: classpath:/mybatis/mapper/*.xml
config-location: classpath:/mybatis/config/mybatis-config.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 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>
<!-- 暂时未使用 文档:https://mybatis.org/mybatis-3/zh/configuration.html#typeAliases -->
<typeAliases>
</typeAliases>
</configuration>
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# mapper.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">
<mapper namespace="com.szui.mybatis.dao.IEmployeeDAO">
<resultMap id="employeeMap" type="com.szui.mybatis.po.EmployeePO">
<id column="id" property="id" />
<result column="employee_number" property="employeeNumber"/>
<result column="employee_name" property="employeeName"/>
<result column="employee_level" property="employeeLevel"/>
<result column="employee_title" property="employeeTitle"/>
<result column="create_time" property="createTime"/>
<result column="update_time" property="updateTime"/>
</resultMap>
<insert id="insert" parameterType="com.szui.mybatis.po.EmployeePO">
INSERT INTO employee(employee_number, employee_name, employee_level, employee_title, create_time, update_time)
VALUES(#{employeeNumber}, #{employeeName}, #{employeeLevel}, #{employeeTitle}, now(), now())
</insert>
<insert id="insertList" parameterType="java.util.List">
INSERT INTO employee(employee_number, employee_name, employee_level, employee_title, create_time, update_time)
values
<foreach collection="list" item="item" separator=",">
(#{item.employeeNumber}, #{item.employeeName}, #{item.employeeLevel}, #{item.employeeTitle}, now(), now())
</foreach>
</insert>
<update id="update" parameterType="com.szui.mybatis.po.EmployeePO">
update employee set employee_level = #{employeeLevel},employee_title=#{employeeTitle}
where employee_number = #{employeeNumber}
</update>
<select id="queryEmployeePoByNumber" parameterType="java.lang.String" resultMap="employeeMap">
select employee_number,employee_name,employee_level,employee_title
from employee where employee_number = #{employeeNumber}
</select>
</mapper>
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
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