SpringBoot 整合 mybatis (配置文件版)

1,前面配置导包等基本操作参照 注解版:https://blog.csdn.net/qq_45315910/article/details/94755256

2, 编写application.yml文件,配置datasource(  config-location: mybatis.xml 文件位置
   mapper-locations: mapper.xml 文件位置)

 

spring:
  datasource:
     username: root
     password: 123456
     url: jdbc:mysql://192.168.84.128:3309/jdbc
     driverClassName: com.mysql.cj.jdbc.Driver
     initialSize: 5
     minIdle: 5
     maxActive: 20
     maxWait: 60000
     timeBetweenEvictionRunsMillis: 60000
     minEvictableIdleTimeMillis: 300000
     validationQuery: SELECT 1 FROM DUAL
     testWhileIdle: true
     testOnBorrow: false
     testOnReturn: false
     poolPreparedStatements: true
     filters: stat,wall,log4j
     maxPoolPreparedStatementPerConnectionSize: 20
     useGlobalDataSourceStat: true
     connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
     type: com.alibaba.druid.pool.DruidDataSource
mybatis: #配置文件版和注解版区别:主要是需要配置此处的mybatis
   configuration:
      map-underscore-to-camel-case: true # true 开启驼峰命名匹配规则
   config-location: classpath:mybatis/mybatis.xml
   mapper-locations: classpath:mybatis/mapper/*.xml

 

3,编写mapper接口,mapper.xml(pojo省略,其实可以使用逆向工程生成mapper.xml、pojo、mapper接口)

public interface StudentMapper {
        public List selectAll();
}

Mapper.xml:




  
    
    
  
  

4,编写mybatis.xml(其实是一个空文文件,需要内容自己添加)






5,编程controller 并 测试

@MapperScan("com.example.jdbc_springboot.mapper")//扫描Mapper包下的Mapper接口
@RestController
public class StudentController {
    @Autowired
    StudentMapper studentMapper;
    //查
    @RequestMapping("/queryAll")
    public List queryAll(){
        return studentMapper.selectAll();
    }
}

SpringBoot 整合 mybatis (配置文件版)_第1张图片

我的配置文件简单目录结构如下:

 SpringBoot 整合 mybatis (配置文件版)_第2张图片

 

完。 

你可能感兴趣的:(Springboot)