<dependency>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-coreartifactId>
<version>${generator.version}version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>tk.mybatisgroupId>
<artifactId>mapper-spring-boot-starterartifactId>
<version>2.0.4version>
dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-maven-pluginartifactId>
<version>${generator.version}version>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xmlconfigurationFile>
<verbose>trueverbose>
<overwrite>trueoverwrite>
configuration>
<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
dependency>
<dependency>
<groupId>tk.mybatisgroupId>
<artifactId>mapperartifactId>
<version>${tk.mybatis.version}version>
dependency>
<dependency>
<groupId>com.chrmgroupId>
<artifactId>mybatis-generator-lombok-pluginartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-coreartifactId>
<version>${generator.version}version>
dependency>
dependencies>
plugin>
plugins>
build>
<dependency>
<groupId>com.chrmgroupId>
<artifactId>mybatis-generator-lombok-pluginartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
plugin>
<plugin type="com.chrm.mybatis.generator.plugins.LombokPlugin" >
<property name="hasLombok" value="true"/>
plugin>
<generatorConfiguration>
<properties resource="application.properties"/>
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
plugin>
<plugin type="com.chrm.mybatis.generator.plugins.LombokPlugin" >
<property name="hasLombok" value="true"/>
plugin>
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true"/>
<property name="javaFileEncoding" value="UTF-8"/>
commentGenerator>
<jdbcConnection driverClass="${spring.datasource.driver-class-name}"
connectionURL="${spring.datasource.url}"
userId="${spring.datasource.username}"
password="${spring.datasource.password}">
jdbcConnection>
<javaModelGenerator targetPackage="com.chuan.demo.dao.entity" targetProject="src/main/java"/>
<sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources"/>
<javaClientGenerator targetPackage="com.chuan.demo.dao.mapper" targetProject="src/main/java"
type="XMLMAPPER"/>
<table tableName="order_detail" domainObjectName="OrderDetail">
<generatedKey column="detail_id" sqlStatement="Mysql" identity="true"/>
table>
<table tableName="order_master" domainObjectName="OrderMaster">
<generatedKey column="order_id" sqlStatement="Mysql" identity="true"/>
table>
<table tableName="product_category" domainObjectName="ProductCategory">
<generatedKey column="category_id" sqlStatement="Mysql" identity="true"/>
table>
<table tableName="product_info" domainObjectName="ProductInfo">
<generatedKey column="product_id" sqlStatement="Mysql" identity="true"/>
table>
<table tableName="seller_info" domainObjectName="SellerInfo">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
table>
context>
generatorConfiguration>
@Table(name = "order_detail")
@Data
public class OrderDetail {
@Id
@Column(name = "detail_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String detailId;
@Column(name = "order_id")
private String orderId;
@Column(name = "product_id")
private String productId;
/**
* 商品名称
*/
@Column(name = "product_name")
private String productName;
/**
* 当前价格,单位分
*/
@Column(name = "product_price")
private BigDecimal productPrice;
/**
* 数量
*/
@Column(name = "product_quantity")
private Integer productQuantity;
/**
* 小图
*/
@Column(name = "product_icon")
private String productIcon;
/**
* 创建时间
*/
@Column(name = "create_time")
private Date createTime;
/**
* 修改时间
*/
@Column(name = "update_time")
private Date updateTime;
}
package com.chuan.demo.dao.mapper;
import com.chuan.demo.dao.entity.OrderDetail;
import tk.mybatis.mapper.common.Mapper;
public interface OrderDetailMapper extends Mapper<OrderDetail> {
}
<mapper namespace="com.chuan.demo.dao.mapper.OrderDetailMapper">
<resultMap id="BaseResultMap" type="com.chuan.demo.dao.entity.OrderDetail">
<id column="detail_id" jdbcType="VARCHAR" property="detailId" />
<result column="order_id" jdbcType="VARCHAR" property="orderId" />
<result column="product_id" jdbcType="VARCHAR" property="productId" />
<result column="product_name" jdbcType="VARCHAR" property="productName" />
<result column="product_price" jdbcType="DECIMAL" property="productPrice" />
<result column="product_quantity" jdbcType="INTEGER" property="productQuantity" />
<result column="product_icon" jdbcType="VARCHAR" property="productIcon" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
resultMap>
mapper>
相对于 mybatis generator + mybatis自动化生成的代码结构来说,本章的mybatis generator + tk.mybatis + Lombok更适合项目开发中的生产环境,可以大大节省开发时间,开发人员只需要关注业务的开发以及数据库表的设计就行了。
注意:当我们在springboot项目中使用tk.mybatis时,我们需要在springboot配置文件application.yml或者application.properties中配置XXXMapper.xml文件的位置,否则xml文件将无法使用。
mybatis:
mapper-locations: classpath:mappers/*.xml