idea通过数据库直接生成mybatis的xml和实体文件,很简单,一共分三步
1.配置generatorConfig.xml
2.配置pom
3.配置命令 mybatis-generator:generate -e
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
1.配置generatorConfig.xml
以下会自动生成entity,mapper,和xml文件
mapper上是没有@Mapper注解的呦
<!--<?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="F:aaaRepositoryjava
epositorymysqlmysql-connector-java8.0.17mysql-connector-java-8.0.17.jar"/>
<context id="mysql">
<commentGenerator>
<property name="suppressDate" value="true" />
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection
connectionURL="jdbc:mysql://localhost:3306/aaa?serverTimezone=GMT%2B8"
driverClass="com.mysql.cj.jdbc.Driver" password="root" userId="root" />
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="com.aaa.bbb.entity" targetProject="src/main/java" >
<property name="constructorBased" value="false"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
<javaClientGenerator targetPackage="com.aaa.bbb.mapper" targetProject="src/main/java" type="XMLMAPPER" >
</javaClientGenerator>
<table tableName="ccc" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"> </table>
</context>
</generatorConfiguration>
“context” 的内容必须匹配按照下面的顺序配置 ?号的可以没有
(connectionFactory|jdbcConnection),
javaTypeResolver,
javaModelGenerator,
sqlMapGenerator,
javaClientGenerator,
table+
2.配置pom
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
-
命令 mybatis-generator:generate -e
运行上边的命令就行了,实体是驼峰形式的,也可以修改配置和数据库一样public class Ccc{
private Integer id;private String identifyCode; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getIdentifyCode() { return identifyCode; } public void setIdentifyCode(String identifyCode) { this.identifyCode = identifyCode; }
}