Spring Boot MyBatis Generator
MyBatis(with MyBatis Generator), Spring Boot, MySQL
To work with them, we have a lot of steps, but it is easy to manage in the future.
- Prepare MySQL Database(table)
- Install MyBatis Generator in your Eclipse
- Prepare Maven for Spring Boot and MyBatis
- Prepare generatorConfig.xml, and mysql-connector-java
- Run MyBatis Generator to create mapper file and model file
- Create database configuration for Spring Boot and MyBatis
- Prepare application.yml for datasource
- Write database operation codes
Prepare MySQL Database(table)
MyBatis Generator creates model Java object from generatorConfig.xml and database table
Tablename test
id BIGINT(20) NN, UQ, AI email VARCHAR(255) NN, UQ
This is simple example
Install MyBatis Generator in your Eclipse(Spring Tool Suite)
I tried to use Spring Tool Suite.
MyBatis Generator is plugin
Please add “http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/” to your plugin URL and install “MyBatis Generator”
Prepare Maven for Spring Boot and MyBatis
Add following dependencies for it(pom.xml)
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.3</version> </dependency>
Prepare generatorConfig.xml, and mysql-connector-java
Please prepare your generatorConfig.xml in your src/main/resources
and need to copy mysql-connector-java.jar in same directory
<?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> <classPathEntry location="/Users/dj110/Development/spring/Project/src/main/resources/mysql-connector-java-5.1.37-bin.jar"/> <context id="testdb" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="password"> </jdbcConnection> <javaModelGenerator targetPackage="com.test.Project.model" targetProject="Project"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.test.Project.map" targetProject="Project"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.test.Project.map" targetProject="Project"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="user"> </table> </context> </generatorConfiguration>
“Project” is your project. There are 3 important part.
One is model, second is mapper, third is mapper xml. If possible, mapper(Interface), mapper xml should be same directory. This is easy way to handle them.
Let’s generate Mybatis files
Point “generateConfig.xml” -> “Right click” -> “Generate Mybatis”
Run Mybatis generator and create following files
|- com.test.Project.model |- User.java |- UserExample.java |- com.test.Project.map |- UserMapper.java |- UserMapper.xml
Create database configuration for Spring Boot and MyBatis
This is an Example
@Configuration @EnableTransactionManagement @MapperScan("com.test.Project.map") public class DBConfig { @Bean public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws Exception{ SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); factory.setDataSource(dataSource); return factory; } @Bean @Primary @Autowired protected PlatformTransactionManager createTransactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } }
Prepare application.yml for datasource
To create datasource, we need information for database.
application.yml should be under /src/main/resources/application.yml
This is example for it
spring: datasource: username: root password: password url: jdbc:mysql://localhost:3306/test driver-class-name: com.mysql.jdbc.Driver
Write database operation codes
Controller, Component, Service, ServiceImpl
We use MyBatis with Spring, we use a lot of Injection
Name | Description |
---|---|
Controller | Controller for request mapping and entrance |
Component | Basically business logic and helper for controller |
Service | Service interface to manage database |
ServiceImpl | Service actual implementation with Example class |
Under Construction!!!