Search code examples
spring-bootmybatismybatis-generator

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


I follow mybatis official website to build my project step by step, but it always can not work well, so I hope you could give me a fully guide from beginning to the end, many thanks.


Solution

  • Step 1. Build a new spring boot project named booking.

    This step is basically, I will skip it.

    Step 2. Add mybatis-generator to project.

    This could help us to generate entity and mapper class mybatis needed automatically, it's very useful for us to save our time.

    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.

    enter image description here

    It will generate all entity, mapper class and mapper xml automatically. -Dmybatis.generator.overwrite=true, means it will overwrite existing entity or mapper class when run mybatis generator with maven.

    Step 3. Add mybatis to this project.

    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;
        }
    }