SpringBoot从安装到实战(七)整合MyBatis并使用MyBatis Generator(已解决生成xml不覆盖以及生成其他数据库的同名表问题)
SpringBoot从安装到实战(七)整合MyBatis并使用MyBatis Generator(已解决生成xml不覆盖以及生成其他数据库的同名表问题)
前言
此教程会与教程第五篇 起冲突,若读过前篇的读者可创建新的项目来避免冲突。
MyBatis
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录。
MyBatis Generator
MyBatis Generator (MBG) is a code generator for MyBatis MyBatis. It will generate code for all versions of MyBatis. It will introspect a database table (or many tables) and will generate artifacts that can be used to access the table(s). This lessens the initial nuisance of setting up objects and configuration files to interact with database tables. MBG seeks to make a major impact on the large percentage of database operations that are simple CRUD (Create, Retrieve, Update, Delete). You will still need to hand code SQL and objects for join queries, or stored procedures.
MyBatis生成器(MBG)是MyBatis MyBatis的代码生成器。它将为MyBatis的所有版本生成代码。它将对一个(或多个)数据库表进行内部检查,并将生成可用于访问表的工件。这减轻了设置对象和配置文件以与数据库表进行交互的麻烦。MBG试图对简单CRUD(创建,检索,更新,删除)的大部分数据库操作产生重大影响。您仍将需要手工编写SQL和对象代码以进行联接查询或存储过程。
MyBatis 与hibernate的区别
1.添加依赖以及MyBatis Generator插件
pom.xml
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency>
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <!-- 添加一个mysql的依赖,防止等会找不到driverClass --> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> <scope>runtime</scope> </dependency> </dependencies> <!-- mybatisGenerator 的配置 --> <configuration> <!-- generator 工具配置文件的位置 --> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <!-- 是否覆盖 --> <!-- 此处要特别注意,如果不加这个设置会导致每次运行都会在原目录再次创建--> <overwrite>true</overwrite> </configuration> </plugin>
2.新建application.yml配置MyBatis
注意
mapper-locations可以指定复数存放mapper.xml位置
3.新建generatorConfig.xml配置MyBatis Generator
在resources目录下创建。
<?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> <context id="MySQLTables" targetRuntime="MyBatis3" defaultModelType="flat"> <!-- 重要!!! 用于设置当使用mybatis generator 时,覆盖旧有mapper.xml而不是合并 --> <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" /> <!-- 给每个实体类重载tostring方法 --> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/> <!-- 公共设置 --> <commentGenerator> <!-- 是否取消自动生成时的注释 --> <property name="suppressAllComments" value="true"/> <!-- 是否取消在注释中加上时间 --> <property name="suppressDate" value="true"/> </commentGenerator> <!-- 链接数据库的配置 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/tacocloud?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false" userId="root" password="852025"> <!-- 只允许生成当前数据库的表 --> <property name="nullCatalogMeansCurrent" value="true"/> </jdbcConnection> <!-- 关于生成实体类的设置 --> <!-- targetPackage 生成代码的目标目录 --> <!-- targetProject 目录所属位置 --> <javaModelGenerator targetPackage="tacos.mbg.model" targetProject="src/main/java"> <!-- 在targetPackge的基础上根据schema再生成一层package 默认flase --> <property name="enableSubPackages" value="true"/> <!-- 是否在get方法中 对String类型的字段做空的判断 --> <property name="trimStrings" value="true"/> <!-- 是否生成一个包含所有字段的构造器 --> <property name="constructorBased" value="false"/> <!-- 是否创建一个不可变类--> <property name="immutable" value="false"/> </javaModelGenerator> <!--关于生成映射文件的设置--> <sqlMapGenerator targetPackage="mbg.mapper" targetProject="src/main/resources"> <!--同上--> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--关于生成dao层的设置--> <javaClientGenerator type="mapper" targetPackage="tacos.mbg.mapper" targetProject="src/main/java"> <!--同上--> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--需要生成的代码对应的表名--> <table tableName="ingredient"></table> <table tableName="taco"></table> <table tableName="taco_ingredients"></table> <table tableName="taco_order"></table> <!-- <table tableName="user"></table> --> <table tableName="taco_order_tacos"></table> </context> </generatorConfiguration>
4.配置Mybatis
新建MybatisConfig类
package tacos; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Configuration; @Configuration @MapperScan("tacos.mbg.mapper") public class MybatisConfig { }
5.运行generator插件
右键项目->Run As->Maven Build
填写Goals输入框,运行。
成功
可以看到generator已经帮我们自动建好model,mapper,mapper.xml
6.测试
package tacos.mapper; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit.jupiter.SpringExtension; import tacos.mbg.mapper.IngredientMapper; import tacos.mbg.model.IngredientExample; @SpringBootTest @ExtendWith(SpringExtension.class) public class IngredientMapperTest { @Autowired private IngredientMapper ingredientMapper; @Test public void test() { IngredientExample ingredientExample=new IngredientExample(); ingredientMapper.selectByExample(ingredientExample).forEach(x->System.out.println(x.toString())); } }
测试成功
排错
如果出现了错误的话,请按以下顺序排查
- pom.xml文件下mybatis.generator版本是否为1.3.7
- generatorConfig.xml是否存在UnmergeableXmlMappersPlugin
- generatorConfig.xml的jdbcConnection是否有配置nullCatalogMeansCurrent
- application.yml是否正确配置好mapper.xml的映射路径
- 配置类是否有注解@MapperScan(“tacos.mbg.mapper”)
完整目录
文章参考
MyBatis Generator 生成器把其他数据库的同名表生成下来的问题
SpringBoot2.0整合Mybatis之逆向工程(eclipse环境)
- 同时安装ASA10以及ASA11两个数据库导致不能使用SQLSERVER2005 management studio的问题解决
- 数据库sql server安装使用遇到的问题以及解决方法
- 使用maven的mybatis-generator生成实例和mapping过程以及可能出现问题解决
- Android项目生成aar包或者共享库文件供其他项目使用以及问题解决
- 解决使用Oracle库利用MyBatis配置文件逆向生成的实体类时mapping.xml里有重复的节点问题
- linux(centOS6) 安装Mysql 5.6.19数据库步骤、 问题以及相应的解决办法
- DedeAMPZ整合包的安装使用、卸载方法等常见问题解决办法
- 实战解决使用ghost安装系统出现的各种问题
- 使用JDBC连接数据库遇到的问题,以及查找到的解决方法
- MyBatis使用foreach进行批量插入遇到的问题以及解决方法
- Linux 使用rpm方式安装最新mysql(5.7.27)步骤以及常见问题解决
- SQL server 2014安装以及解决连接数据库失败问题
- Springboot中使用redis,配置redis的key value生成策略(解决编码问题)
- Linux 使用rpm方式安装最新mysql(5.7.16)步骤以及常见问题解决
- mybatis-generator自动生成Mapper以及其他生成类的使用
- SpringBoot使用xml配置文件整合mybatis
- centos6使用rpm 安装nginx(可以不联网)、配置静态资源访问、遇到的问题以及解决方法
- 实战解决使用ghost安装系统出现的各种问题
- jedis,spring-redis-data 整合使用,版本问题异常以及解决。
- 关于mysql-5.7.9-winx64以及其他系列安装的问题解决