SpringBoot集成TKMapper
创建SpringBoot工程
导入依赖
<!-- 通用Mapper插件文档地址:https://gitee.com/free/Mapper/wikis/Home -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
创建mapper和bean
这里我使用了
这个插件,非常好用https://gejun123456.github.io/MyBatisCodeHelper-Pro/#/

生成完就是这个样子
mapper:
@org.apache.ibatis.annotations.Mapper
public interface StudentMapper extends Mapper<Student> {
}
bean:
import javax.persistence.*;
@Table(name = "student")
public class Student {
@Id
@Column(name = "id")
@GeneratedValue(generator = "JDBC")
private Integer id;
@Column(name = "`name`")
private String name;
/**
* @return id
*/
public Integer getId() {
return id;
}
/**
* @param id
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return name
*/
public String getName() {
return name;
}
/**
* @param name
*/
public void setName(String name) {
this.name = name;
}
}
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.danzle.customannotation.mapper.StudentMapper">
<resultMap id="BaseResultMap" type="com.danzle.customannotation.entity.Student">
<!--@mbg.generated generated on Fri Aug 21 17:06:33 CST 2020.-->
<!--@Table student-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
</resultMap>
<sql id="Base_Column_List">
<!--@mbg.generated generated on Fri Aug 21 17:06:33 CST 2020.-->
id, `name`
</sql>
</mapper>
加入配置
spring:
datasource:
url: jdbc:mysql://xxx:3306/zsh?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false
username: root
password: xxx
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.danzle.customannotation.entity
configuration:
map-underscore-to-camel-case: true
mapper:
identity: mysql
not-empty: true
enum-as-simple-type: true
server:
port: 9999
启动类上加入@MapperScan_("xxx.mapper") 注意:要导入_import tk.mybatis.spring.annotation.MapperScan; 这个包下的
custom-annotation.zip 附上源码资料
本文详细介绍如何在SpringBoot项目中集成TKMapper,包括创建工程、导入依赖、配置数据源、编写Mapper接口和实体类、生成XML配置文件等步骤,为快速搭建基于MyBatis的持久层提供实用指南。


6721

被折叠的 条评论
为什么被折叠?



