微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

 

 

完。 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐