spring boot整合mybatis generator自动生成文件
Spring Boot使用mybatis generator自动生成文件
1、pom.xml中引入mybatis generato插件
<!-- 这段mybatis在创建Spring Boot中选择mybatis就会自动添加了-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- mybatis generator 自动生成代码插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!--指定配置文件的名称。默认值:${basedir}/src/main/resources/generatorConfig.xml-->
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<!--如果指定了该参数,如果生成的java文件存在已经同名的文件,新生成的文件会覆盖原有的文件。
如果没有指定该参数,如果存在同名的文件,MBG会给新生成的代码文件生成一个唯一的名字(例如: MyClass.java.1, MyClass.java.2 等等)。
重要: 生成器一定会自动合并或覆盖已经生成的XML文件。默认值:false-->
<overwrite>true</overwrite>
<!--如果指定该参数,执行过程会输出到控制台-->
<verbose>true</verbose>
</configuration>
</plugin>
2、在resource下新建generator文件夹下新建generatorConfig.xml文件
编写编写 mybatis-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">
<!--上面URL可能报错,但不影响-->
<generatorConfiguration>
<!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
<classPathEntry location="E:\apache-maven-3.3.9\repository\mysql\mysql-connector-java\5.1.41\mysql-connector-java-5.1.41.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8" userId="root" password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.example.demo.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
3、运行 mybatis-generator-maven-plugin
添加maven命令:mybatis-generator:generate -e
确定后点击运行即可生成实体类,dao层类、mapper.xml文件
运行成功
在springboot启动类上添加包扫描注解
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//配置mapper包扫描路径,将项目中对应的mapper类的路径加进来
@MapperScan("com.example.demo.dao")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}