在咱们开启一个新项目的研发后,一般要编写不少的entity/pojo/dto/mapper/dao...
, 大多研发兄弟们都会抱怨,为何我要重复写CRUD
? 咱们为了不编写一些没必要要的重复代码,这节给你们介绍介绍使用一个开源工具,来帮助咱们从这种简单枯燥的编码中解救出来。隆重有请: MyBatis通用Mapper4html
通用Mapper均可以极大的方便开发人员。能够随意的按照本身的须要选择通用方法,还能够很方便的开发本身的通用方法。
极其方便的使用MyBatis单表的增删改查。
支持单表操做,不支持通用的多表联合查询。
通用 Mapper 支持 Mybatis-3.2.4 及以上版本。
Tips:
各位技术同仁必定要有版本意识哦~
Let's code!java
参考上一节中的Module建立mybatis-generator-tool
.mysql
<?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">
<parent>
<artifactId>expensive-shop</artifactId>
<groupId>com.life-runner</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mybatis-generator-tool</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<!--springboot 构建可执行fat jars必须的插件,如不添加,在生产环境会有问题-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!-- 设置配置文件路径 -->
<configurationFile>
${basedir}/src/main/resources/generator/generatorConfig.xml
</configurationFile>
<!--容许覆盖-->
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<!-- mysql8 驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!--通用 Mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>复制代码
${basedir}/src/main/resources/generator/generatorConfig.xml
, 咱们须要在项目src=>main=>resource
目录下建立generator
文件夹,在文件夹下建立文件generatorConfig.xml
,内容以下: <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--引入数据库配置内容-->
<properties resource="generator/config.properties"/>
<context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<!--配置是否使用通用 Mapper 自带的注释扩展,默认 true-->
<!--<property name="useMapperCommentGenerator" value="false"/>-->
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<!--设置Mapper生成的basic,可自定义-->
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
<!--大小写转换敏感-->
<property name="caseSensitive" value="true"/>
<!--引入lombok注解-->
<property name="lombok" value="Getter,Setter,ToString"/>
<!--分隔符定义-->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
</plugin>
<!-- 设置数据库配置 -->
<jdbcConnection driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.url}"
userId="${jdbc.user}"
password="${jdbc.password}">
</jdbcConnection>
<!-- 对应生成的pojo所在包 -->
<javaModelGenerator targetPackage="com.liferunner.pojo" targetProject="src/main/java"/>
<!-- 对应生成的mapper所在目录 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
<!-- 配置mapper对应的java映射 -->
<javaClientGenerator targetPackage="com.liferunner.mapper" targetProject="src/main/java" type="XMLMAPPER"/>
<!-- 数据库表 -->
<table tableName="carousel"></table>
<table tableName="category"></table>
<table tableName="items"></table>
<table tableName="items_comments"></table>
<table tableName="items_img"></table>
<table tableName="items_param"></table>
<table tableName="items_spec"></table>
<table tableName="order_items"></table>
<table tableName="order_status"></table>
<table tableName="orders"></table>
<table tableName="shop_users"></table>
<table tableName="user_address"></table>
<table tableName="users"></table>
</context>
</generatorConfiguration>复制代码
咱们能够看到一行配置内容:
,这里是为了将咱们的数据库链接、帐号等信息外置,配置内容以下:git
jdbc.driverClass = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/expensiveshop?characterEncoding=UTF-8&useSSL\
=false&useUnicode=true&serverTimezone=UTC
jdbc.user = root
jdbc.password = 12345678复制代码
能够看到这里设置的内容就是下属代码中用到的。github
...
<jdbcConnection driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.url}"
userId="${jdbc.user}"
password="${jdbc.password}">
</jdbcConnection>
...复制代码
配置信息你们能够参考:传送门spring
mybatis-generator-tool>mvn mybatis-generator:generate
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< com.life-runner:mybatis-generator-tool >---------------
[INFO] Building mybatis-generator-tool 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- mybatis-generator-maven-plugin:1.3.6:generate (default-cli) @ mybatis-generator-tool ---
[INFO] Connecting to the Database
[INFO] Introspecting table carousel
[INFO] Introspecting table category
...
[INFO] Generating Record class for table carousel
[INFO] Generating Mapper Interface for table carousel
[INFO] Generating SQL Map for table carousel
...
[INFO] Saving file CarouselMapper.xml
...
[INFO] Saving file Carousel.java
[INFO] Saving file Users.java
...
[WARNING] Table configuration with catalog null, schema null, and table shop_users did not resolve to any tables
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.374 s
[INFO] Finished at: 2019-11-05T15:40:07+08:00
[INFO] ------------------------------------------------------------------------
复制代码
能够看到执行成功,虽然这里执行成功,可是当咱们打开文件的时候会发现:sql
package com.liferunner.pojo;
import java.util.Date;
import javax.persistence.*;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
@Table(name = "Carousel")
public class Carousel {
/**
* ����id �û�id
*/
@Id
private String id;
/**
* �û��� �û���
*/
private String imageUrl;
...
}复制代码
这里出现了乱码问题,这又是怎么回事呢?不要紧,let's bing... 传送门,能够看到有265000条结果,那就说明咱们的问题已经有太多的人遇到了,随便点开一个:能够看到红框里面的内容咱们缺失了,在
expensive-shopmybatis-generator-toolsrcmainresourcesgeneratorgeneratorConfig.xml
中添加上
,从新执行生成命令,能够看到咱们的乱码就没有了。数据库
@Getter
@Setter
@ToString
@Table(name = "`carousel`")
public class Carousel {
/**
* 主键
*/
@Id
@Column(name = "`id`")
private String id;
/**
* 图片 图片地址
*/
@Column(name = "`image_url`")
private String imageUrl;
...复制代码
Tips:apache
在这一环节先剧透一个bug,不然我担忧在后续你们遇到的时候,由于它确实是和Common Mapper生成相关的。segmentfault
咱们点开生成的Users.java
,能够看到以下所示:
@Getter
@Setter
@ToString
@Table(name = "users")
public class Users {
@Column(name = "USER")
private String user;
@Column(name = "CURRENT_CONNECTIONS")
private Long currentConnections;
@Column(name = "TOTAL_CONNECTIONS")
private Long totalConnections;
}复制代码
但是咱们的Users
表不是这样的呀,这是怎么回事???让咱们分析分析:1.既然没有用到咱们本身的Users表,可是又确实经过生成器生成了,那么很明显确定是Mysql数据库中表,这是确定的。2.那么问题就来了,它从哪里冒出来的?找它,盘它。3.究竟是哪一个数据库中的呢?sys?informationschema?performanceschema?4.挨个查询,果真:能够看到,在
performance_schema
数据库中有一个users
表,那么究竟是不是咱们生成出来的呢?执行SHOW CREATE TABLE users
, 结果如上图,字段和生成出来的是一致的!5.抓住它了,怎么盘它???
很简单,修改jdbc:mysql://localhost:3306/expensiveshop?nullCatalogMeansCurrent=true&characterEncoding=UTF-8&useSSL
=false&useUnicode=true&serverTimezone=UTC,新增上加粗部分就能够了。
nullCatalogMeansCurrent
字面意思很简单,就是说若是是null catalog,我就选择current.由于mysql不支持catalog
,咱们须要告知mybatis
这个特性,设置为true
就好了。
按照SQL标准的解释,在SQL环境下Catalog和Schema都属于抽象概念,主要用来解决命名冲突问题。
从概念上说,一个数据库系统包含多个Catalog,每一个Catalog又包含多个Schema,而每一个Schema又包含多个数据库对象(表、视图、序列等),反过来说一个数据库对象必然属于一个Schema,而该Schema又必然属于一个Catalog,这样咱们就能够获得该数据库对象的彻底限定名称从而解决命名冲突的问题了
从实现的角度来看,各类数据库系统对Catalog和Schema的支持和实现方式千差万别,针对具体问题须要参考具体的产品说明书,比较简单而经常使用的实现方式是使用数据库名做为Catalog名,Oracle使用用户名做为Schema名.
可查阅Mysql官网说明:传送门
本节咱们讲解了如何生成咱们想要的,简单和重要又重复的工做咱们能够经过工具实现啦,下一次咱们将开始实际业务的编码实现.
奔跑的人生 | 博客园 | segmentfault | spring4all | csdn | 掘金 | OSChina | 简书 | 头条 | 知乎 | 51CTO