SpringBoot整合Mybatis Generator自动生成代码


Posted in Java/Android onAugust 23, 2021

Mybatis是目前主流的ORM框架,相比于hibernate的全自动,它是半自动化需要手写sql语句、接口、实体对象,后来推出的Generator自动生成代码,可以帮我们提高开发效率。

本文目的:SpringBoot 整合 Mybatis Generator自动生成dao、entity、mapper.xml实现单表增删改查。

1.创建SpringBoot项目

File→New→Project… 选择Spring Initializr,选择JDK版本,默认初始化URL

SpringBoot整合Mybatis Generator自动生成代码

填写项目名称,java版本,其他描述信息

SpringBoot整合Mybatis Generator自动生成代码

选择项目存放路径

SpringBoot整合Mybatis Generator自动生成代码

选择web、mybatis、mysql依赖

SpringBoot整合Mybatis Generator自动生成代码

Next?>Finish完成项目创建

2. mybatis-generator-maven插件的配置

打开项目的pom.xml文件添加

<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<configuration>
					<verbose>true</verbose>
					<overwrite>true</overwrite>
				</configuration>
			</plugin>

完整pom.xml

<?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">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.xyz</groupId>
	<artifactId>mybatis</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>mybatis</name>
	<description>Spring Boot 整合 Mybatis</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.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-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.2.5</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<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>
				<configuration>
					<verbose>true</verbose>
					<overwrite>true</overwrite>
				</configuration>
			</plugin>
		</plugins>
	</build>


</project>

3. 项目结构构建

在项目目录下(这里是mybatis)添加controller、service、dao、entity包,在resources下添加mapper包存放映射文件。

SpringBoot整合Mybatis Generator自动生成代码

4. application.yml配置

#端口号配置
server:
  port: 8088
spring:
#模板引擎配置
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML
    encoding: UTF-8
    cache: false
    servlet:
      content-type: text/html
#静态文件配置
  resources:
    static-locations: classpath:/static,classpath:/META-INF/resources,classpath:/templates/
#jdbc配置
  datasource:
    url: jdbc:mysql://localhost:3306/video?useUnicode=true&characterEncoding=utf8
    username: xyz
    password: xyz
    driver-class-name: com.mysql.jdbc.Driver
#mybatis配置
mybatis:
#映射文件路径
  mapper-locations: classpath:mapper/*.xml
#模型所在的保命
  type-aliases-package: com.xyz.mybatis.entity

5. generatorConfig.xml配置

在resources文件下创建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>

    <!--classPathEntry:数据库的JDBC驱动,换成你自己的驱动位置 可选 -->
    <classPathEntry location="E:\IdeaProjects\mysql-connector-java-5.1.47.jar"/>

    <!-- 一个数据库一个context,defaultModelType="flat" 大数据字段,不分表 -->
    <context id="MysqlTables" targetRuntime="MyBatis3Simple" defaultModelType="flat">

        <!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表;一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖 -->
        <property name="autoDelimitKeywords" value="true"/>

        <!-- 生成的Java文件的编码 -->
        <property name="javaFileEncoding" value="utf-8"/>

        <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; -->
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <!-- 格式化java代码 -->
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>

        <!-- 格式化XML代码 -->
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>

        <!-- 注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->
            <property name="suppressDate" value="false"/> <!-- 是否生成注释代时间戳-->
        </commentGenerator>

        <!-- jdbc连接-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/video?serverTimezone=UTC" userId="xyz"
                        password="xyz"/>

        <!-- 类型转换 -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成实体类地址 -->
        <javaModelGenerator targetPackage="com.xyz.mybatis.entity" targetProject="src/main/java">
            <!-- 是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false"/>
            <!-- 从数据库返回的值去掉前后空格 -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成map.xml文件存放地址 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- 生成接口dao -->
        <javaClientGenerator targetPackage="com.xyz.mybatis.dao" targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <!-- table可以有多个,每个数据库中的表都可以写一个table,tableName表示要匹配的数据库表,也可以在tableName属性中通过使用%通配符来匹配所有数据库表,只有匹配的表才会自动生成文件 enableSelectByPrimaryKey相应的配置表示是否生成相应的接口 -->
        <table tableName="t_manager" enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"
               enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"
               enableDeleteByPrimaryKey="true">
            <property name="useActualColumnNames" value="true"/>
        </table>

    </context>
</generatorConfiguration>

注意:
classPathEntry location=“E:\IdeaProjects\mysql-connector-java-5.1.47.jar”,建议用5.X系列的,否则可能生成的接口会缺少

6. 在idea中添加一个mybatis generator maven插件启动选项,点击Run,选择Edit Configuration… 点击加号"+"添加,选择maven,填写名称(这里用mybatis generator),命令行:mybatis-generator:generate -e

SpringBoot整合Mybatis Generator自动生成代码

SpringBoot整合Mybatis Generator自动生成代码

SpringBoot整合Mybatis Generator自动生成代码

7. 选择 Mybatis Generator 启动,自动在dao、entity、mapper包下生成代码

SpringBoot整合Mybatis Generator自动生成代码

注意:
利用Mybatis Generator自动生成代码,对于已经存在的文件会存在覆盖和在原有文件上追加的可能性,不宜多次生成。如需重新生成,需要删除已生成的源文件。

到此这篇关于SpringBoot整合Mybatis Generator自动生成代码的文章就介绍到这了,更多相关SpringBoot Mybatis Generator自动生成代码内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Java/Android 相关文章推荐
java中重写父类方法加不加@Override详解
Jun 21 Java/Android
Java基础-封装和继承
Jul 02 Java/Android
Java反应式框架Reactor中的Mono和Flux
Jul 25 Java/Android
dubbo服务整合zipkin详解
Jul 26 Java/Android
使用Java去实现超市会员管理系统
Mar 18 Java/Android
JavaWeb实现显示mysql数据库数据
Mar 19 Java/Android
零基础学java之带返回值的方法的定义和调用
Apr 10 Java/Android
Android自定义双向滑动控件
Apr 19 Java/Android
Java Spring Boot 正确读取配置文件中的属性的值
Apr 20 Java/Android
springcloud整合seata
May 20 Java/Android
Spring JPA 增加字段执行异常问题及解决
Jun 10 Java/Android
tree shaking对打包体积优化及作用
Jul 07 Java/Android
Java面试题冲刺第十九天--数据库(4)
Java获取e.printStackTrace()打印的信息方式
Aug 07 #Java/Android
Java移除无效括号的方法实现
Aug 07 #Java/Android
简述Java中throw-throws异常抛出
Aug 07 #Java/Android
Java比较两个对象中全部属性值是否相等的方法
Aug 07 #Java/Android
Java org.w3c.dom.Document 类方法引用报错
Aug 07 #Java/Android
springboot+WebMagic+MyBatis爬虫框架的使用
Aug 07 #Java/Android
You might like
PHP 创建文件(文件夹)以及目录操作代码
2010/03/04 PHP
PHP5.3以上版本安装ZendOptimizer扩展
2015/03/27 PHP
浅谈php中urlencode与rawurlencode的区别
2016/09/05 PHP
thinkPHP框架对接支付宝即时到账接口回调操作示例
2016/11/14 PHP
深入理解JavaScript系列(3) 全面解析Module模式
2012/01/15 Javascript
js操纵dom生成下拉列表框的方法
2014/02/24 Javascript
node.js中的path.basename方法使用说明
2014/12/09 Javascript
JavaScript中实现依赖注入的思路分享
2015/01/15 Javascript
js简单工厂模式用法实例
2015/06/30 Javascript
jQuery中的AjaxSubmit使用讲解
2016/09/25 Javascript
js控制台输出的方法(详解)
2016/11/26 Javascript
javascript实现无法关闭的弹框
2016/11/27 Javascript
详解jQuery选择器
2016/12/21 Javascript
js实现弹窗暗层效果
2017/01/16 Javascript
解决vue2.0路由跳转未匹配相应用路由避免出现空白页面的问题
2018/08/24 Javascript
vue路由守卫+登录态管理实例分析
2019/05/21 Javascript
[44:10]2018DOTA2亚洲邀请赛 4.5 淘汰赛 EG vs VP 第一场
2018/04/06 DOTA
[33:19]完美世界DOTA2联赛PWL S2 PXG vs InkIce 第一场 11.26
2020/11/30 DOTA
从零学python系列之新版本导入httplib模块报ImportError解决方案
2014/05/23 Python
Python的字典和列表的使用中一些需要注意的地方
2015/04/24 Python
详解 Python中LEGB和闭包及装饰器
2017/08/03 Python
Python实现带参数与不带参数的多重继承示例
2018/01/30 Python
对python 调用类属性的方法详解
2019/07/02 Python
python文件读写代码实例
2019/10/21 Python
python实现拉普拉斯特征图降维示例
2019/11/25 Python
Python建造者模式案例运行原理解析
2020/06/29 Python
python利用paramiko实现交换机巡检的示例
2020/09/22 Python
行政专员的岗位职责
2014/03/10 职场文书
人力资源管理求职信
2014/08/07 职场文书
幼儿园感恩节活动方案
2014/10/06 职场文书
走群众路线学习心得体会
2014/10/31 职场文书
2015年党日活动总结范文
2015/03/25 职场文书
Python爬虫实战之爬取携程评论
2021/06/02 Python
Linux7.6二进制安装Mysql8.0.27详细操作步骤
2021/11/27 MySQL
MySQL中B树索引和B+树索引的区别详解
2022/03/03 MySQL
关于MySQL临时表为什么可以重名的问题
2022/03/22 MySQL