本文最后更新于:2023-03-13, 23:48:50
继上篇 Springboot整合Druid 之后,下面继续记录一下Springboot整合mybatis的过程。
1、引入依赖
| <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency>
|
2、建表
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| CREATE TABLE `wx_message` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID', `msg_id` bigint(20) NOT NULL unique COMMENT '消息ID', `user_id` varchar(64) NOT NULL DEFAULT '' COMMENT '用户ID(一个OpenID)', `msg_type` varchar(64) NOT NULL DEFAULT '' COMMENT '消息类型', `receive_msg` varchar(2048) NOT NULL DEFAULT '' COMMENT '收到的消息', `response_msg` varchar(2048) NOT NULL DEFAULT '' COMMENT '回复的消息', `create_date` datetime NOT NULL COMMENT '创建时间', PRIMARY KEY (`id`) ) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT ='公众号微信消息';
|
3、创建实体类
1 2 3 4 5 6 7 8 9 10
| public class WxMessage { private long id; private long msgId; private String msgType; private String userId; private String receiveMsg; private String responseMsg; private Date createDate; }
|
4、创建mapper接口
1 2 3 4 5 6
| public interface WxMessageMapper {
List<WxMessage> findAll();
int insert(WxMessage message); }
|
5、在resources目录下创建WxMessageMapper.xml
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?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.zahi.hrms.employee.mapper.WxMessageMapper"> <select id="findAll" resultType="WxMessage"> select * from wx_message </select>
<insert id="insert" parameterType="WxMessage"> insert into wx_message (msg_id, user_id, msg_type, receive_msg, response_msg, create_date) values (#{msgId}, #{userId}, #{msgType}, #{receiveMsg}, #{responseMsg}, #{createDate} ) </insert>
</mapper>
|
注意:
(1) 这个文件的名字和上面的mapper接口的类名一致
(2) namespace是上面mapper接口的全限定类名
(3) select / insert等标签的id要和mapper接口里的方法名一致
ps:到现在主要目录结构如下
6、在启动类里面添加MapperScan注解,扫描所有mapper接口
1 2 3 4 5 6 7
| @SpringBootApplication @MapperScan("com.zahi.hrms.employee.mapper") public class EmployeeApplication { public static void main(String[] args) { SpringApplication.run(EmployeeApplication.class, args); } }
|
7、在application.yml添加配置
1 2 3 4 5 6 7 8
| mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.zahi.hrms.employee.entity configuration: map-underscore-to-camel-case: true
|
ps:配置type-aliases-package之后,上面的WxMessageMapper.xml里面的resultType和parameterType就可以只写类名,不用写全限定类名了。
END