Study notes @玩转Spring全家斗
The content of the course comes from the geek time playing Spring Family Bucket, the link is as follows
https://time.geekbang.org/course/intro/100023501
Sixth day 1/2
MybatisGenerator
It is the official generator provided by MyBatis. It generates the POJO and Mapper interfaces used in the data table, and can also generate an xml file of SQL Mapper, and other
Instructions
命令行
java -jar mybatis-generator-core-x.x.x.jar -configfile generatorConfig.xml
Maven Plugin(mybatis-generator-maven-plugin)
mvn mybatis-generator:generate
${basedir}/src/main/resources/generatorConfig.xml
Eclipse Plugin
Java program
Ant Task等
Code
generatorConfig.xml file
<?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="H2Tables" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.FluentBuilderMethodsPlugin" />
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />
<jdbcConnection driverClass="org.h2.Driver"
connectionURL="jdbc:h2:mem:testdb"
userId="sa"
password="">
</jdbcConnection>
<javaModelGenerator targetPackage="geektime.spring.data.mybatis.model"
targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="geektime.spring.data.mybatis.mapper"
targetProject="./src/main/resources/mapper">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="MIXEDMAPPER"
targetPackage="geektime.spring.data.mybatis.mapper"
targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="t_coffee" domainObjectName="Coffee" >
<generatedKey column="id" sqlStatement="CALL IDENTITY()" identity="true" />
<columnOverride column="price" javaType="org.joda.money.Money" jdbcType="BIGINT"
typeHandler="geektime.spring.data.mybatis.handler.MoneyTypeHandler"/>
</table>
</context>
</generatorConfiguration>
MybatisGeneratorDemoApplication
package geektime.spring.data.mybatis;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@SpringBootApplication
@Slf4j
@MapperScan("geektime.spring.data.mybatis.mapper")
public class MybatisGeneratorDemoApplication implements ApplicationRunner {
@Autowired
private CoffeeMapper coffeeMapper;
public static void main(String[] args) {
SpringApplication.run(MybatisGeneratorDemoApplication.class, args);
}
@Override
public void run(ApplicationArguments args) throws Exception {
generateArtifacts();
}
private void generateArtifacts() throws Exception {
List<String> warnings = new ArrayList<>();
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(
this.getClass().getResourceAsStream("/generatorConfig.xml"));
DefaultShellCallback callback = new DefaultShellCallback(true);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
remind
If you want to use MyBatis Generator and modify the fine-tuning yourself, it is recommended to put the content of the fine-tuning in different positions, for example, there are two sets of model, xml, and mapper.
MybatisPageHelper
As the name implies, it helps to do paging
Support common databases Oracle, Mysql, H2
PageHelper official website
https://pagehelper.github.io/
Generally use RowBounds (which can be used with MyBatis Generator) or more parameters
pagehelper.page-size-zero=true
will query all data when the page number is 0
Code
MybatisDemoApplication
package geektime.spring.data.mybatisdemo;
import com.github.pagehelper.PageInfo;
import geektime.spring.data.mybatisdemo.mapper.CoffeeMapper;
import geektime.spring.data.mybatisdemo.model.Coffee;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.List;
@SpringBootApplication
@Slf4j
@MapperScan("geektime.spring.data.mybatisdemo.mapper")
public class MybatisDemoApplication implements ApplicationRunner {
@Autowired
private CoffeeMapper coffeeMapper;
public static void main(String[] args) {
SpringApplication.run(MybatisDemoApplication.class, args);
}
@Override
public void run(ApplicationArguments args) throws Exception {
coffeeMapper.findAllWithRowBounds(new RowBounds(1, 3))
.forEach(c -> log.info("Page(1) Coffee {}", c));
coffeeMapper.findAllWithRowBounds(new RowBounds(2, 3))
.forEach(c -> log.info("Page(2) Coffee {}", c));
log.info("===================");
coffeeMapper.findAllWithRowBounds(new RowBounds(1, 0))
.forEach(c -> log.info("Page(1) Coffee {}", c));
log.info("===================");
coffeeMapper.findAllWithParam(1, 3)
.forEach(c -> log.info("Page(1) Coffee {}", c));
List<Coffee> list = coffeeMapper.findAllWithParam(2, 3);
PageInfo page = new PageInfo(list);
log.info("PageInfo: {}", page);
}
}
CoffeeMapper
package geektime.spring.data.mybatisdemo.mapper;
import geektime.spring.data.mybatisdemo.model.Coffee;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.session.RowBounds;
import java.util.List;
@Mapper
public interface CoffeeMapper {
@Select("select * from t_coffee order by id")
List<Coffee> findAllWithRowBounds(RowBounds rowBounds);
@Select("select * from t_coffee order by id")
List<Coffee> findAllWithParam(@Param("pageNum") int pageNum,
@Param("pageSize") int pageSize);
}