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<Student> selectAll();
}

Mapper.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.jdbc_springboot.mapper.StudentMapper">
  <resultMap id="BaseResultMap" type="com.example.jdbc_springboot.pojo.Student">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="sname" jdbcType="VARCHAR" property="sname" />
  </resultMap>
  <select id="selectAll" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select id,sname from student
  </select>
</mapper>

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

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC
    "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

</configuration>

5,编程controller 并 测试

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

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

 

 

完。 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值