This page looks best with JavaScript enabled

SpringBoot项目集成mybatis generator

 ·  ☕ 2 min read

主要针对单表的增删改查, 只需要很少量的简单配置,就可以完成大量的表到Java对象的生成工作,能快捷的创建好Dao,entry,xml 加快了开发速度,拥有零出错和速度快的优点,让开发人员解放出来更专注于业务逻辑的开发。

父pom.xml里增加插件

mybatis-generator插件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<build>
	<plugins>
		<!-- mybatis generator 自动生成代码插件 -->
		<plugin>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-maven-plugin</artifactId>
			<version>1.3.7</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>5.1.37</version>
				</dependency>
			</dependencies>
		</plugin>
	</plugins>
</build>

新增generatorConfig.xml配置文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?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"/>
        <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.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/couurse"
                        userId="course"
                        password="root">
        </jdbcConnection>

        <!-- domain类的位置 -->
        <javaModelGenerator targetProject="src\main\java"
                            targetPackage="com.course.server.domain"/>

        <!-- mapper xml的位置 -->
        <sqlMapGenerator targetProject="src\main\resources"
                         targetPackage="mapper"/>

        <!-- mapper类的位置 -->
        <javaClientGenerator targetProject="src\main\java"
                             targetPackage="com.course.server.mapper"
                             type="XMLMAPPER" />
		<!-- 需要生成的表名 -->
<!--        <table tableName="test" domainObjectName="Test"/>-->
<!--        <table tableName="chapter" domainObjectName="Chapter"/>-->
        <table tableName="member_course" domainObjectName="MemberCourse"/>
    </context>
</generatorConfiguration>

Maven 启动命令

mybatis-generator:generate -e

Edit Configuration.. 中配置

单表查询-Example使用

引入mybatis分页插件pagehelper

1
2
3
4
5
6
<!-- mybatis分页插件pagehelper -->
<dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper-spring-boot-starter</artifactId>
  <version>1.2.10</version>
</dependency>

查询老师数据库总信息

1
2
3
4
5
6
PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
TeacherExample teacherExample = new TeacherExample();

List<Teacher> teachers = teacherMapper.selectByExample(teacherExample);
PageInfo<Teacher> pageInfo = new PageInfo<>(teachers);
pageDto.setTotal(pageInfo.getTotal());

管理老师信息

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    /**
    * 新增
    */
    private void insert(Teacher teacher) {
        teacher.setId(UuidUtil.getShortUuid());
        teacherMapper.insert(teacher);
    }

    /**
    * 更新
    */
    private void update(Teacher teacher) {
        teacherMapper.updateByPrimaryKey(teacher);
    }

    /**
    * 删除
    */
    public void delete(String id) {
    teacherMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查找
     */
    public TeacherDto findById(String id) {
        Teacher teacher = teacherMapper.selectByPrimaryKey(id);
        return CopyUtil.copy(teacher, TeacherDto.class);
    }

CopyUtil类 用于类的拷贝

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class CopyUtil {

    public static <T> List<T> copyList(List source, Class<T> clazz) {
        List<T> target = new ArrayList<>();
        if (!CollectionUtils.isEmpty(source)){
            if (!CollectionUtils.isEmpty(source)){
                for (Object c: source) {
                    T obj = copy(c, clazz);
                    target.add(obj);
                }
            }
        }
        return target;
    }

    public static <T> T copy(Object source, Class<T> clazz) {
        if (source == null) {
            return null;
        }
        T obj = null;
        try {
            obj = clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        BeanUtils.copyProperties(source, obj);
        return obj;
    }
}
Support the author with
alipay QR Code
wechat QR Code