java SpringBoot(二十一)Mybatis generator自动生成mapper、model以及字段注释

  • Post author:
  • Post category:java



使用Mybatis generator可以自动生成mapper、model类以及mapper.xml,可以满足基本的crud功能。


目录结构:


使用:


1、导入依赖 以及maven插件

<!--集成druid连接池-->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.10</version>
		</dependency>
		<!-- MyBatis 生成器 -->
		<dependency>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-core</artifactId>
			<version>1.3.7</version>
		</dependency>
		<!--Mysql数据库驱动-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.15</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.4</version>
		</dependency>

maven插件:

<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.7</version>
				<dependencies>
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>8.0.15</version>
					</dependency>
				</dependencies>
				<configuration>
					<!--配置文件的位置-->
					<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
					<verbose>true</verbose>
					<overwrite>true</overwrite>
				</configuration>
				<executions>
					<execution>
						<id>Generate MyBatis Artifacts</id>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>

			</plugin>
		</plugins>
	</build>


2、编写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>
    <properties resource="generator.properties"/>
    <context id="MySqlContext" targetRuntime="MyBatis3" defaultModelType="flat">
        <!--        配置签指分隔符的属性-->
        <property name="beginningDelimiter" value="`"/>
        <!--        配置后置分隔符的属性-->
        <property name="endingDelimiter" value="`"/>
        <!--        设置要使用的Java文件的编码-->
        <property name="javaFileEncoding" value="UTF-8"/>
        <!-- 为模型生成序列化方法-->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
        <!-- 为生成的Java模型创建一个toString方法 -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
        <!--生成mapper.xml时覆盖原文件-->
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
        <!--可以自定义生成model的代码注释-->
<!--        <commentGenerator type="com.macro.mall.tiny.mbg.CommentGenerator">-->
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="false"/>
            <property name="suppressDate" value="false"/>
            <property name="addRemarkComments" value="false"/>
        </commentGenerator>
        <!--配置数据库连接-->
        <jdbcConnection driverClass="${jdbc.driverClass}"
                        connectionURL="${jdbc.connectionURL}"
                        userId="${jdbc.userId}"
                        password="${jdbc.password}">
            <!--解决mysql驱动升级到8.0后不生成指定数据库代码的问题-->
            <property name="nullCatalogMeansCurrent" value="true" />
        </jdbcConnection>
        <!--指定生成model的路径-->
        <javaModelGenerator targetPackage="com.zr.mall.demo.mbg.model" targetProject="src\main\java"/>
        <!--指定生成mapper.xml的路径-->
        <sqlMapGenerator targetPackage="com.zr.mall.demo.mbg.mapper" targetProject="src\main\resources"/>
        <!--指定生成mapper接口的的路径-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.zr.mall.demo.mbg.mapper"
                             targetProject="src\main\java"/>
        <!--生成全部表tableName设为%-->
        <table tableName="user">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
    </context>
</generatorConfiguration>


3、编写generator.properties 里边放上边需要读取的数据库连接信息

jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
jdbc.userId=root
jdbc.password=123456


4、创建文件夹


配置文件中指定了生成model、mapper、mapper.xml的路径,所以要创建好文件夹


5、配置maven插件


(1)、点击运行


(2)、配置插件


名称:generator


命令行:

mybatis-generator:generate -e


(3)、应用


6、配置数据库驱动


编辑application.yml

server:
  port: 8081 #spring 启动端口

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC #mysql 地址
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver #指定驱动

mybatis:
  mapper-locations: classpath:mapper/*xml #mapper扫描路径
  type-aliases-package: com.example.demo.bean #类型所在目录
  configuration:
    map-underscore-to-camel-case: true #开启驼峰字段转换


7、使用generator


选择上generator 点击右侧运行


8、运行成功,查看结果


全部自动生成


9、测试使用


编写Service层

UserService:

package com.zr.mall.demo.service;

import com.zr.mall.demo.mbg.model.User;

public interface UserService {

    User getUser(Long id);
}

UserServiceImpl:

package com.zr.mall.demo.service.impl;

import com.zr.mall.demo.mbg.mapper.UserMapper;
import com.zr.mall.demo.mbg.model.User;
import com.zr.mall.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User getUser(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }
}

controller:

调用:



版权声明:本文为qq_40771567原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。