# 第01章 项目环境准备
为了看清楚MyBatis的整个初始化过程,先创建一个简单的Java项目(也可以是JavaWeb或者Spring/SpringBoot的项目,此处为了便捷采用Java maven项目),目录结构如下图所示:
建表语句:
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for ota_package
-- ----------------------------
DROP TABLE IF EXISTS `ota_package`;
CREATE TABLE `ota_package` (
`package_id` varchar(100) NOT NULL,
`ota_status` varchar(255) DEFAULT NULL,
`package_name` varchar(255) DEFAULT NULL,
`package_desc` varchar(255) DEFAULT NULL,
`package_size` double(12,0) DEFAULT NULL,
`software_num` varchar(255) DEFAULT NULL,
`current_software_version` varchar(255) DEFAULT NULL,
`target_software_version` varchar(255) DEFAULT NULL,
`create_at` datetime DEFAULT NULL,
`update_at` datetime DEFAULT NULL,
`create_by` varchar(255) DEFAULT NULL,
`update_by` varchar(255) DEFAULT NULL,
PRIMARY KEY (`package_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of ota_package
-- ----------------------------
INSERT INTO `ota_package` VALUES ('09d97dba-d79c-11e8-9a63-b8975aa5f439', 'CheckSuccess', '测试升级包', '测试', 54237, '911171-M11', 'SW:1.2.3', 'SW:1.2.4', '2018-10-24 22:49:41', '2018-10-24 22:49:41', 'xiaochaochao', 'wangchao');
INSERT INTO `ota_package` VALUES ('09dc0443-d79c-11e8-9a63-b8975aa5f439', 'Finish', '测试升级包', '测试', 54237, '911173-M11', 'SW:1.2.3', 'SW:1.2.4', '2018-10-24 22:49:41', '2018-10-24 22:49:41', 'yangmei', 'wangchao');
INSERT INTO `ota_package` VALUES ('09dc05d1-d79c-11e8-9a63-b8975aa5f439', 'Abort', '测试升级包', '测试', 54237, '911375-BH01', 'SW:1.2.3', 'SW:1.2.4', '2018-10-24 22:49:41', '2018-10-24 22:49:41', 'xiaochaochao', 'wangchao');
INSERT INTO `ota_package` VALUES ('09dc0644-d79c-11e8-9a63-b8975aa5f439', 'Upgrading', '测试升级包', '测试', 54237, '911371-M11', 'SW:1.2.3', 'SW:1.2.4', '2018-10-24 22:49:41', '2018-10-24 22:49:41', 'yangmei', 'wangchao');
INSERT INTO `ota_package` VALUES ('09dc06a7-d79c-11e8-9a63-b8975aa5f439', 'DownloadSuccess', '测试升级包', '测试', 54237, '911771-M11', 'SW:1.2.3', 'SW:1.2.4', '2018-10-24 22:49:41', '2018-10-24 22:49:41', 'wangchao', 'yangmei');
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
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
# 1.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wangchao</groupId>
<artifactId>MybatisTest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<!-- 将Mapper.xml编译到class文件中 -->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*Mapper.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 1.2 OtaPackage升级包模型类
/**
* 升级包模型
*
* @author wangchao
* @since 2019-02-14
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class OtaPackage implements Serializable {
private static final long serialVersionUID = 1L;
private String packageId;
private String otaStatus;
private String packageName;
private String packageDesc;
private Double packageSize;
private String softwareNum;
private String currentSoftwareVersion;
private String targetSoftwareVersion;
private Date createAt;
private Date updateAt;
private String createBy;
private String updateBy;
}
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
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
# 1.3 OtaPackageMapper.java
/**
* 升级包Mapper 接口
*
* @author wangchao
* @since 2019-02-12
*/
public interface OtaPackageMapper {
/**
* 查询所有的升级包
*
* @return
*/
List<OtaPackage> selectOtaPackageList();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1.4 OtaPackageMapper.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.clare.mapper.OtaPackageMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.clare.model.OtaPackage">
<id column="package_id" property="packageId" />
<result column="ota_status" property="otaStatus" />
<result column="package_name" property="packageName" />
<result column="package_desc" property="packageDesc" />
<result column="package_size" property="packageSize" />
<result column="software_num" property="softwareNum" />
<result column="current_software_version" property="currentSoftwareVersion" />
<result column="target_software_version" property="targetSoftwareVersion" />
<result column="create_at" property="createAt" />
<result column="update_at" property="updateAt" />
<result column="create_by" property="createBy" />
<result column="update_by" property="updateBy" />
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
package_id, ota_status, package_name, package_desc, package_size, software_num, current_software_version, target_software_version, create_at, update_at, create_by, update_by
</sql>
<select id="selectOtaPackageList" resultMap="BaseResultMap">
select * from ota_package
</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
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
# 1.5 db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true&characterEncoding=utf8
username=root
password=123456
1
2
3
4
2
3
4
# 1.6 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>
<properties resource="db.properties">
<!--<property name="username" value="dev_user"/>-->
<!--<property name="password" value="F2Fa3!33TYyg"/>-->
</properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- mapper路径配置 -->
<mappers>
<mapper resource="com/clare/mapper/OtaPackageMapper.xml"/>
</mappers>
</configuration>
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
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
# 1.7 MyBatisBootstrap.java
public class MyBatisBootstrap {
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
OtaPackageMapper otaPackageMapper = sqlSession.getMapper(OtaPackageMapper.class);
List<OtaPackage> otaPackages = otaPackageMapper.selectOtaPackageList();
for (OtaPackage otaPackage : otaPackages) {
System.out.println("packageId:"+otaPackage.getPackageId());
}
}catch (Exception e){
e.printStackTrace();
}finally {
sqlSession.close();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
结果: