SpringBoot整合Mybatis
1. 添加依赖
<!-- MyBatis 生成器 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
<!--Mysql数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
2. 写配置文件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>
<!--generator.properties里配置了连接的数据库信息-->
<properties resource="generator.properties"/>
<context id="MySqlContext" targetRuntime="MyBatis3" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<property name="javaFileEncoding" value="UTF-8"/>
<!-- 为模型生成序列化方法-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<!-- 为生成的Java模型创建一个toString方法 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<!--CommentGenerator里自定义了生成model的代码注释,默认添加的注释不易阅读-->
<commentGenerator type="com.macro.mall.tiny.mbg.CommentGenerator">
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
<property name="addRemarkComments" value="true"/>
</commentGenerator>
<!--配置数据库连接-->
<jdbcConnection driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.connectionURL}"
userId="${jdbc.userId}"
password="${jdbc.password}">
<!--解决mysql驱动升级到8.0后不生成指定数据库代码的问题-->
<property name="nullCatalogMeansCurrent" value="true" />
</jdbcConnection>
<!--指定生成model的路径-->
<javaModelGenerator targetPackage="com.macro.mall.tiny.mbg.model" targetProject="./src/main/java"/>
<!--指定生成mapper.xml的路径-->
<sqlMapGenerator targetPackage="com.macro.mall.tiny.mbg.mapper" targetProject="./src/main/resources"/>
<!--指定生成mapper接口的的路径-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.macro.mall.tiny.mbg.mapper"
targetProject="./src/main/java"/>
<!--若要生成全部表,则将tableName设为%-->
<table tableName="pms_brand">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
</context>
</generatorConfiguration>
3. 生成函数
运行main函数即会生成2个POJO文件,一个mapper.java文件,一个mapper.xml文件package com.macro.mall.tiny.mbg;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* 用于生产MBG的代码
* Created by macro on 2018/4/26.
*/
public class Generator {
public static void main(String[] args) throws Exception {
//MBG 执行过程中的警告信息
List<String> warnings = new ArrayList<String>();
//当生成的代码重复时,覆盖原代码
boolean overwrite = true;
//读取我们的 MBG 配置文件
InputStream is = Generator.class.getResourceAsStream("/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(is);
is.close();
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
//创建 MBG
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
//执行生成代码
myBatisGenerator.generate(null);
//输出警告信息
for (String warning : warnings) {
System.out.println(warning);
}
}
}
4. 生成的文件介绍
每张表会生成2个POJO,1个mapper.java以及1个mapper.xml,2个POJO中除了常规的POJO类之外还生成了用于设置查询条件的xxxExample类。生成的mapper.java文件如下,有一个计数方法、两个删除方法、两个插入方法、三个查询方法以及六个更新方法。查询:
selectByExample(PmsBrandExample example):通过特定限制条件查询信息,example用于生成一个Criteria对象来设置查询条件,Criteria是PmsBrandExample的一个静态内部类,可以通过它设置查询限制条件。
selectByExampleWithBLOBs(PmsBrandExample example):根据特定限制条件查询,返回值包含类型为text的列(默认查询并不会返回该列的信息),example用于生成一个Criteria对象来设置查询条件。
selectByPrimaryKey(Long id):根据主键查询。
插入:
只有两个方法,方法传入的参数都是POJO,返回值都是int类型的受影响的行数。不同之处在于insert会插入所有的信息,如果传入的对象某一属性为空,则插入空,如果数据库中设置了默认值,默认值就失效了。而insertSelective不同,他只会插入含有数据的属性,对于为空的属性,不予以处理,这样的话如果数据库中设置有默认值,就不会被空值覆盖了。
删除:
deleteByExample(PmsBrandExample example):根据条件删除。
deleteByPrimaryKey(Long id):根据主键删除。
更新:
updateByExampleSelective():根据特定的限制条件更新所有设置了值的列。
updateByExampleWithBLOBs():根据特定的限制条件进行更新所有列。
updateByExample():根据特定的限制条件进行更新除了text类型(数据库)的所有列。
updateByPrimaryKeySelective():通过ID更新所有设置了值的列。
updateByPrimaryKeyWithBLOBs():通过ID进行更新所有列。
updateByPrimaryKey():通过ID更新除了text类型(数据库)的所有列。
总结:
- Example和Primarykey用来指定要删除/更新/查询的行(根据给定条件/主键来选择)。
- 不加后缀、Selective后缀、WithBLOBs后缀用来限制要删除/更新/查询的列(更新除了text类型(数据库)的所有列/给了值得列/全部列)。
package com.macro.mall.tiny.mbg.mapper;
import com.macro.mall.tiny.mbg.model.PmsBrand;
import com.macro.mall.tiny.mbg.model.PmsBrandExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface PmsBrandMapper {
// 计数
long countByExample(PmsBrandExample example);
// 删除
int deleteByExample(PmsBrandExample example);
int deleteByPrimaryKey(Long id);
// 插入
int insert(PmsBrand record);
int insertSelective(PmsBrand record);
// 查询
List<PmsBrand> selectByExampleWithBLOBs(PmsBrandExample example);
List<PmsBrand> selectByExample(PmsBrandExample example);
PmsBrand selectByPrimaryKey(Long id);
// 更新
int updateByExampleSelective(@Param("record") PmsBrand record, @Param("example") PmsBrandExample example);
int updateByExampleWithBLOBs(@Param("record") PmsBrand record, @Param("example") PmsBrandExample example);
int updateByExample(@Param("record") PmsBrand record, @Param("example") PmsBrandExample example);
int updateByPrimaryKeySelective(PmsBrand record);
int updateByPrimaryKeyWithBLOBs(PmsBrand record);
int updateByPrimaryKey(PmsBrand record);
}
参考:
https://blog.csdn.net/qq_39056805/article/details/80585941

浙公网安备 33010602011771号