通用mabatis-generator可以由mysql表自动生成model类,mapper映射文件和mapper接口,参考:MyBatis通用Mapper和PageHelper

1.依赖

<!-- mybatis -->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>2.1.5</version>
</dependency>
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper</artifactId>
    <version>3.3.9</version>
</dependency>

2.插件

<!-- mybatis-generator -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
    <configurationFile>src/main/resources/generator-config.xml</configurationFile>
    <overwrite>true</overwrite>
    <verbose>true</verbose>
</configuration>
<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.47</version>
    </dependency>
    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper</artifactId>
        <version>3.3.9</version>
    </dependency>
</dependencies>
</plugin>

3.在application.properties中配置数据库相关参数

# mysql
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useLegacyDatetimeCode=false&serverTimezone=Hongkong&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=xxxx
spring.datasource.driver=com.mysql.jdbc.Driver

4.配置generator-config.xml,其中通用MyMapper参考:Mybatis学习笔记——通用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>
    <!-- 引用外部配置文件-->
    <properties resource="application.properties" />

    <context id="context" targetRuntime="MyBatis3Simple">
        <!-- MapperPlugin -->
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <!-- 该配置会使生产的Mapper自动继承MyMapper -->
            <property name="mappers" value="com.example.demo.core.mapper.MyMapper" />
            <!-- caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true -->
            <property name="caseSensitive" value="false"/>
        </plugin>
        <!-- 去掉生成出来的代码的注解 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
            <property name="suppressDate" value="true" />
        </commentGenerator>
        <!-- 数据库信息 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="${spring.datasource.url}"
                        userId="root"
                        password="xxxx">
        </jdbcConnection>
        <!-- 生成Model类的包名和位置 -->
        <javaModelGenerator targetPackage="com.example.demo.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成Mapper映射文件的包名和位置 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成Mapper接口的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 要生成代码的表 tableName=%时为所有表生成 表名字段名都有默认规则生成 -->
        <table tableName="%">
            <!-- 指定生成的主键属性名 生成SQL语句的类型 -->
            <generatedKey column="id" sqlStatement="MySql"/>
        </table>
    </context>

</generatorConfiguration>

5.生成,点击plugin中的mabatis-generator:generate

Mybatis学习笔记——mybatis-generator_mysql

6.自动生成model,mapper和xml

Mybatis学习笔记——mybatis-generator_sql_02

7.添加 @MapperScan(basePackages = "com.example.demo.mapper") 注解

@SpringBootApplication
@MapperScan(basePackages = "com.example.demo.mapper")
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

8. 测试

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class UserMapperTest {

    @Resource
    private UserMapper mapper;

    @Test
    public void UserMapper() {
        User user = mapper.selectByPrimaryKey(1L);
        System.out.println(user.getId());
    }

}