項(xiàng)目地址:https://github.com/mybatis/mybatis-3.git
添加Maven依賴
<!-- Spring Boot 使用 -->
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.4</version>
</dependency>
<!--pagehelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.1</version>
</dependency>
TestExampleMapper
public interface TestExampleMapper extends Mapper<TestExample> {
}
實(shí)體類注解
public class TestExample {
@Id
private Integer id;
@Column
private String name;
private String logo;
//省略setter和getter方法
}
簡單用法示例
@Resource
private TestExampleMapper mapper;
Condition condition = new Condition(TestExample .class);
condition .createCriteria().andGreaterThan("id", 100);
List<TestExample > exampleList = mapper.selectByCondition(condition );
application.properties 配置
#mybatis
mybatis.type-aliases-package=tk.mybatis.springboot.model
mybatis.mapper-locations=classpath:mapper/*.xml
#mapper
#mappers 多個接口時逗號隔開
mapper.mappers=tk.mybatis.springboot.util.MyMapper
mapper.not-empty=false
mapper.identity=MYSQL
#pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
application.yml 配置
mybatis:
type-aliases-package: tk.mybatis.springboot.model
mapper-locations: classpath:mapper/*.xml
mapper:
mappers:
- tk.mybatis.springboot.util.MyMapper
not-empty: false
identity: MYSQL
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
mybatis-spring方式配置
@Configuration
public class MybatisConfigurer {
@Bean
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setTypeAliasesPackage("com.duia.home.model,com.duia.home.common.dto");
//配置分頁插件淆院,詳情請查閱官方文檔
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("pageSizeZero", "true");//分頁尺寸為0時查詢所有紀(jì)錄不再執(zhí)行分頁
properties.setProperty("reasonable", "true");//頁碼<=0 查詢第一頁右蕊,頁碼>=總頁數(shù)查詢最后一頁
properties.setProperty("supportMethodsArguments", "true");//支持通過 Mapper 接口參數(shù)來傳遞分頁參數(shù)
pageHelper.setProperties(properties);
//添加插件
factory.setPlugins(new Interceptor[]{pageHelper});
//添加XML目錄
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
return factory.getObject();
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
mapperScannerConfigurer.setBasePackage("com.duia.home.dao");
//配置通用Mapper近范,詳情請查閱官方文檔
Properties properties = new Properties();
properties.setProperty("mappers", "com.duia.home.core.Mapper");
//insert绍载、update是否判斷字符串類型!='' 即 test="str != null"表達(dá)式內(nèi)是否追加 and str != ''
properties.setProperty("notEmpty", "false");
properties.setProperty("IDENTITY", "MYSQL");
mapperScannerConfigurer.setProperties(properties);
return mapperScannerConfigurer;
}
}