SpringBoot(七) 整合Mybatis

在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("没有数据");
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值