在SpringBoot中整合Mybatis:
1.在pom.xml中添加mybatis依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2.连接数据库
在application.properties中添加数据源,同时添加对mybatis的配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/news?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
#mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.fyl.entity
#驼峰命名规范:
mybatis.configuration.map-underscore-to-camel-case=true
3.使用Mybatis
SpringBoot扫描Mapper接口包的方法
1.使用@Mapper接口类上,此方法需要在每个Mapper接口上都添加
2.在启动类使用@MapperScan可以指定要扫描mapper类的包的路径
@SpringBootApplication
@MapperScan("com.example.myspringboot.mapper")
public class MyspringbootApplication {
public static void main(String[] args) {
SpringApplication.run(MyspringbootApplication.class, args);
}
}
使用@MapperScan扫描多个包
@MapperScan("com.example.myspringboot.mapper","com.fyl.test")
注意:整合mysql是出现这个错误
错误原因:MySQL这个jar包依赖类型是runtime:即只有运行时生效,所以这里报错,不影响代码运行
解决办法:将runtime修改为Compile即可
4.mapper.xml文件位置
两种方式:
一种是放在resources文件夹下:
配置: mybatis.mapper-locations=classpath:mappers/*.xml
另一种是在java文件加下和接口在一块:
配置: mybatis.mapper-locations=classpath*:com/fyl/navigation/mappers/*.xml
注意:要在pom.xml文件中配置
<resources><!--资源路径插件-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
5.MybatisConfig配置类:
@Configuration
@MapperScan(basePackages = {"com.greentranboot.dao"}, sqlSessionFactoryRef = "sqlSessionFactoryGreentranDB")
public class MybatisConfig {
@Autowired
@Qualifier("qingchaDB")
private DataSource dataSource;
@Bean
public SqlSessionFactory sqlSessionFactoryGreentranDB() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
Resource[] resources = pathMatchingResourcePatternResolver.getResources("classpath:com/greentranboot/dao/mappers/*.xml");
factoryBean.setMapperLocations(resources);
//添加插件
factoryBean.setPlugins(new Interceptor[]{pageHelper()});
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryGreentranDB());
return template;
}
@Bean
public PageHelper pageHelper(){
//分页插件
PageHelper pageHelper=new PageHelper();
Properties properties=new Properties();
properties.setProperty("reasonable","true");
properties.setProperty("supportMethodsArguments","true");
properties.setProperty("returnPageInfo","check");
properties.setProperty("params","count=countSql");
pageHelper.setProperties(properties);
return pageHelper;
}
注:mybatis分页插件使用:
(1)SpringBoot中配置
#mybatis 分页插件
pagehelper.helper-dialect=mysql
pagehelper.param=count=countSql
pagehelper.reasonable=fase
pagehelper.support-methods-arguments=true
(2) 配置类配置
@Bean
public PageHelper pageHelper(){
//分页插件
PageHelper pageHelper=new PageHelper();
Properties properties=new Properties();
properties.setProperty("reasonable","true");
properties.setProperty("supportMethodsArguments","true");
properties.setProperty("returnPageInfo","check");
properties.setProperty("params","count=countSql");
pageHelper.setProperties(properties);
return pageHelper;
}
(3)使用插件
PageHelper.startPage(pageNum,pageSize);
List<WebsiteSearchVO> websiteSearchVOList = websiteMapper.websiteSearch(wname,cid);
PageInfo<WebsiteSearchVO> pageInfo=new PageInfo<>(websiteSearchVOList);
if(websiteSearchVOList!=null){
return ServerResponse.createBySuccess(pageInfo);
}else{
return ServerResponse.createByErrorMessage("没有数据");
}