Mybatis 官方为我们提供了持久层代码的代码生成器,可以生成 mapper.xml
,Mapper
接口和实体类。
新建一个 SpringBoot
工程。
pom.xml
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
设置 maven
插件:
<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>
<version>1.4.0</version>
<configuration>
<configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
在 src/main/resources/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">
<generatorConfiguration>
<context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">
<property name="autoDelimitKeywords" value="true"/>
<!-- 反引号:如果表名或字段名是 MySQL 的关键字就自动加上反引号 -->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- 覆盖生成XML文件 -->
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
<!-- 生成的实体类添加 toString() 方法 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<!-- 不生成注释: 因为注释是英文 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/online_course?serverTimezone=UTC"
userId="root"
password="root">
</jdbcConnection>
<!-- domain类的位置 -->
<javaModelGenerator targetProject="src\main\java"
targetPackage="com.lsu.server.domain"/>
<!-- mapper xml的位置 -->
<sqlMapGenerator targetProject="src\main\resources"
targetPackage="mapper"/>
<!-- mapper类的位置 -->
<javaClientGenerator targetProject="src\main\java"
targetPackage="com.lsu.server.mapper"
type="XMLMAPPER" />
<!-- <table tableName="test" domainObjectName="Test"/>-->
<!-- <table tableName="chapter" domainObjectName="Chapter"/>-->
<table tableName="section" domainObjectName="Section"/>
<!-- <table tableName="course" domainObjectName="Course"/>-->
<!-- <table tableName="course_content" domainObjectName="CourseContent"/>-->
<!-- <table tableName="course_content_file" domainObjectName="CourseContentFile"/>-->
<!-- <table tableName="teacher" domainObjectName="Teacher"/>-->
<!-- <table tableName="file" domainObjectName="File"/>-->
<!-- <table tableName="user" domainObjectName="User"/>-->
<!-- <table tableName="resource" domainObjectName="Resource"/>-->
<!-- <table tableName="role" domainObjectName="Role"/>-->
<!-- <table tableName="role_resource" domainObjectName="RoleResource"/>-->
<!-- <table tableName="role_user" domainObjectName="RoleUser"/>-->
<!-- <table tableName="member" domainObjectName="Member"/>-->
<!-- <table tableName="sms" domainObjectName="Sms"/>-->
<!-- <table tableName="member_course" domainObjectName="MemberCourse"/>-->
</context>
<!-- mybatis-generator:generate -e 生成代码命令-->
</generatorConfiguration>
在标签 jdbcConnection
中设置必要的连接值,其余标签注释均有详细的介绍。
table
里的属性表示要生成的表名,这里设置了很多,需要生成哪一个表打开对应的注释即可。
输入以下命令执行任务:
mybatis-generator:generate -e
在 IDEA
中可以设置快捷方式:
会生成 4
个文件:
前 3
个文件就是我们平常自己写的文件,主要看一下最后一个 SectionExample
。
该类提供了很多方法,可以帮我们完成复杂查询,下面以一个例子说明:
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("wyw");
criteria.andUsernameIsNull();
example.setOrderByClause("username asc,email desc");
List<?>list = XxxMapper.selectByExample(example);
//相当于:select * from user where username = 'wyw' and username is null order by username asc,email desc
UserExample example = new UserExample();
example.or().andxxxx();
可以翻阅文档查看更多使用说明:https://mybatis.org/generator/generatedobjects/exampleClassUsage.html