SpringBoot_数据访问-整合MyBatis-配置版MyBatis
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了SpringBoot_数据访问-整合MyBatis-配置版MyBatis,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2689字,纯文字阅读大概需要4分钟。
内容图文
![SpringBoot_数据访问-整合MyBatis-配置版MyBatis](/upload/InfoBanner/zyjiaocheng/1237/f6264710d9f7418fb08d201e32d460cf.jpg)
1 .添加配置文件 mybatis-config.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> <!--驼峰命名--> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration> EmployeeMapper.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.baoxing.springboot.mapper.EmployeeMapper"> <!-- public Employee getEmpById(Integer id); publicvoid insertEmp(Employee employee); --> <select id="getEmpById" resultType="com.baoxing.springboot.bean.Employee"> select * from employee where id=#{id} </select> <insert id="insertEmp"> INSERT INTO employee(lastName,email,gender,d_id) VALUES (#{lastName},#{email},#{gender},#{did}) </insert> </mapper>
application.yml配置文件 主要看mybatis对应的配置 spring: datasource: username: root password: root url: jdbc:mysql: // localhost:3306/test driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource # schema: # - classpath:sql/department.sql # - classpath:sql/employee.sql # 数据源其他配置 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,去掉后监控界面sql无法统计,‘wall‘用于防火墙 filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 mybatis: config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml
EmployeeMapper类 @Mapper public interface EmployeeMapper { public Employee getEmpById(Integer id); public void insertEmp(Employee employee); }
Controller类 @RestController public class DepartmentController { @Autowired private DepartmentMapper departmentMapper; @Autowired private EmployeeMapper employeeMapper; @GetMapping(value = "/dept/{id}") public Department getDepartment(@PathVariable("id") Integer id){ return departmentMapper.getDeptById(id); } @GetMapping(value = "/dept") public Department insertDept(Department department){ departmentMapper.insertDept(department); return department; } @GetMapping(value = "emp/{id}") public Employee getEmp(@PathVariable("id")Integer id){ return employeeMapper.getEmpById( id); } }
原文:https://www.cnblogs.com/cbxBlog/p/9244871.html
内容总结
以上是互联网集市为您收集整理的SpringBoot_数据访问-整合MyBatis-配置版MyBatis全部内容,希望文章能够帮你解决SpringBoot_数据访问-整合MyBatis-配置版MyBatis所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。