Spring Boot + Mybatis + Mybatis Generator 如何构建工程?

How to build project by Spring Boot + Mybatis + Mybatis Generator?

我按照mybatis官网一步一步搭建自己的项目,但是总是不能很好的运行,希望大家从头到尾给我一个完整的指导,非常感谢。

第 1 步。构建一个名为 booking 的新 spring 引导项目。

这一步基本就这样了,我就跳过了

第 2 步。将 mybatis-generator 添加到项目。

这可以帮助我们生成实体和映射器class mybatis 需要自动生成,这对我们节省时间非常有用。

  1. Add plugin config in pom.xml
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.5</version>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>6.0.6</version>
                </dependency>
            </dependencies>
        </plugin>
  1. Create generatorConfig.xml at base resources path.
<?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="MySqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/booking?useSSL=false"
                        userId="root"
                        password="123456">
            <property name="nullCatalogMeansCurrent" value="true" />
        </jdbcConnection>

        <javaModelGenerator targetPackage="com.clycle.booking.entity" targetProject="C:\Users\a243903\projects\booking\webapi\src\main\java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="com.clycle.booking.mapper"  targetProject="C:\Users\a243903\projects\booking\webapi\src\main\resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.clycle.booking.mapper"  targetProject="C:\Users\a243903\projects\booking\webapi\src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <table tableName="%">
        </table>

    </context>
</generatorConfiguration>
  1. Create maven Run/Debug Configuration to run this plugin.

它会自动生成所有实体,映射器class和映射器xml。 -Dmybatis.generator.overwrite=true, 表示当 运行 mybatis generator with maven.

时,它将覆盖现有的实体或映射器 class

第三步,将mybatis添加到该项目中

  1. Add mybatis dependency in pom.xml
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.1</version>
    </dependency>
  1. Create mybatis-config.xml at base resources path.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="logImpl" value="LOG4J" />
    </settings>

    <typeAliases>
        <package name="com.clycle.booking.entity" />
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC">
                <property name="" value="" />
            </transactionManager>
            <dataSource type="UNPOOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/booking" />
                <property name="username" value="root" />
                <property name="password" value="123456" />
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <package name="com.clycle.booking.mapper" />
        <!--<mapper resource="com/clycle/booking/mapper/ShopMapper.xml" />-->
    </mappers>
</configuration>
  1. Add @MapperScan for application main class.
@SpringBootApplication
@MapperScan({"com.clycle.booking.mapper"})
public class BookingApplication {

    public static void main(String[] args) {
        SpringApplication.run(BookingApplication.class, args);
    }
}
  1. Autowired mapper interface to operate your database.
@Autowired
private ShopMapper shopMapper;

@PostMapping(RoutePath.SHOP_LIST)
public List<Shop> GetList() {

    try {
        return shopMapper.selectAll();
    } catch (Exception ex) {
        return null;
    }
}

您可以下载这个项目:https://github.com/yyqian/spring-boot-mybatis-generator。 在我的电脑上一切正常。