前面使用全注解的方式,我们写了一个Mybatis的类,我们对数据库进行增删改查的操作,在实际开发中呢,我们也经常使用配置文件的方式,我们需要一个Mybatis的全局配置文件,包括写一个映射文件,那我们就看一下配置文件的方式是怎么用呢,我来创建一个新的mapper,操作Employee这张表,它是一个interface,首先我们不管是配置文件还是注解的方式,都应该使用@Mapper,或者用@MapperScan的方式,一定要把这个接口扫描到这个容器中,这个是必不可少的,用SQL映射文件来对应这个mapper,我们mybatis目录文件,我们再来创建一个mapper,我们在mapper下存所有的映射文件,来写一个SQL映射文件,我们就得创建一个XML文件,这个SQL映射文件怎么写呢,我们看一下官方文档,mybatis都托管到github里面,mybatis的相关资料,我们来搜索mybatis,我们就是mybatis3,mybatis/mybatis-3https://github.com/mybatis/mybatis-3第一个就是mybatis的官方文档MyBatis SQL mapper framework for Java http://mybatis.github.io/mybatis-3/这里有一个Getting started快速开始,示例里面有全局配置文件<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments><mappers><mapper resource="org/mybatis/example/BlogMapper.xml"/></mappers>
</configuration>mybatis-config.xml全局配置文件粘过来,<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration></configuration>SQL映射文件也在下边<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.example.BlogMapper"><select id="selectBlog" resultType="Blog">select * from Blog where id = #{id}</select>
</mapper>SQL映射文件,首先在namespace上,跟接口绑定,我们把接口的全类名copy过来,这个接口有两个方法,把这两个方法用配置的方式配置在映射文件里面,第一个是查询我们使用select标签,id就是我们的方法名,方法的返回是resultType,指明方法的类型,应该用全类名,然后我们来写SQL语句,插入没有返回值,mybatis.config-location=classpath:mybatis/mybatis-config.xmlmybatis.mapper-locations=classpath:mybatis/mapper/*.xmlhttp://localhost:8080/emp/1{"id":1,"name":"张三1","gender":"男","title":"软件开发工程师","email":"zhangsan1@126.com",
"salary":4000.0,"deptId":1}
#debug=true
#server.port=8081#server.context-path=/boot02spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=truelogging.level.com.learn=trace
#logging.file=D:/springboot.log
logging.file=springboot.log
#logging.path=/spring/log
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd} ==== [%thread] %-5level ==== %logger{50} ==== %msg%n
#spring.resources.static-locations=classpath:/hello,classpath:/learnspring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/day20
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
package com.learn.mapper;import org.apache.ibatis.annotations.Mapper;import com.learn.entities.Employee;//@Mapper或者@MapperScan将接口扫描装配到容器中
@Mapper
public interface EmployeeMapper {public Employee getEmpById(Integer id);public void insertEmp(Employee employee);// @Select("select * from employee where id=#{id}")
//  public Employee getEmpById(Integer id);
//
//  @Update("update employee set NAME=#{name},email=#{email},gender=#{gender},deptId=#{deptId} where id=#{id}")
//  public void updateEmp(Employee employee);
//
//  @Delete("delete from employee where id=#{id}")
//  public void deleteEmpById(Integer id);
//
//  @Insert("insert into employee(NAME,email,gender,deptId) values(#{name},#{email},#{gender},#{deptId})")
//  public void insertEmployee(Employee employee);
//
//  @Select("select * from employee where NAME=#{name}")
//  public Employee getEmpByLastName(String lastName);}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.learn.mapper.EmployeeMapper"><select id="getEmpById" resultType="com.learn.entities.Employee">select * from employee where id = #{id}</select><insert id="insertEmp">insert into employee(NAME,email,gender,deptId) values(#{name},#{email},#{gender}#{deptId})</insert></mapper>
package com.learn.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import com.learn.entities.Employee;
import com.learn.mapper.EmployeeMapper;@RestController
public class EmpController {@AutowiredEmployeeMapper employeeMapper;@GetMapping("/emp/{id}")public Employee getEmp(@PathVariable("id") Integer id) {return employeeMapper.getEmpById(id);}}
我们看到这个数据是查到了,我们这一块did没有值,是因为驼峰命名法没有使用起来,我们就可以来全局配置文件里来配置了,参照mybatis的官方文档,来到Configuration XML里面,我们看一下他的设置项http://www.mybatis.org/mybatis-3/configuration.html拿到mapUnderscoreToCamelCase,拿到它,然后设置为true<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings><setting name="mapUnderscoreToCamelCase" value="true"></settings>
</configuration>来重启一下,这样全局配置文件和SQL映射文件,也就能用了,我们发现这个dId就有值了,我们后来的配置就可以写在配置文件里面了,那我们之前的注解版的departmentMapper能不能使用呢,我们再来发一个department请求http://localhost:8080/dept/1{"id":1,"deptName":"应用开发部","principal":"李经理","functional":"负责公司软件业务的开发"}你发现也是能够使用的,注解版你可以为他来写Mapper文件,也可以使用注解版,都行,我们可以来混合使用的,主要的核心步骤呢,就是需要指定这一个mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml配置文件版,指定全局配置文件的位置,指定SQL映射文件的位置,这两个一指定呢,mybatis的使用就和以前一模一样了

SpringBoot_数据访问-整合MyBatis(二)-配置版MyBatis相关推荐

  1. SpringBoot_数据访问-整合MyBatis(二)-注解版MyBatis

    前面已经创建数据表和JAVABEAN,如何用mybatis来对数据进行增删改查,我们先说mybatis注解版的使用,我来写上一个mapper,操作我们这个数据库,我们放在mapper包下,我们操作de ...

  2. SpringBoot_数据访问-整合MyBatis(一)-基础环境搭建

    前面整合了基本的JDBC,也配置了druid数据源,那接下来就整合mybatis,整合mybatis呢,数据访问使用的是mybatis,那mybatis要怎么用呢,下一步选择我们需要的模块,先把web ...

  3. SpringBoot_数据访问-整合Druid配置数据源监控

    然后实际在开发的时候,我们很少用到这个数据源,比如我们用c3p0,或者开发常用的druid,这是我们阿里的数据源产品,虽然Hikarui的性能比druid要好一点,由于druid有安全监控的整个解决方 ...

  4. 第 3-2 课:SpringBoot如何优雅地使⽤ MyBatis XML 配置版

    MyBatis 是现如今最流⾏的 ORM 框架之⼀,我们先来了解⼀下什么是 ORM 框架. ORM 框架 对象关系映射(Object Relational Mapping,ORM)模式是⼀种为了解决⾯ ...

  5. Spring Boot入门系列(六)Spring Boot如何使用Mybatis XML 配置版【附详细步骤】

    前面介绍了Spring Boot 中的整合Thymeleaf前端html框架,同时也介绍了Thymeleaf 的用法.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/z ...

  6. SpringBoot_数据访问-简介

    SpringBoot与数据访问,SpringBoot如何与关系型数据进行交互,我们将会使用JDBC技术,包括持久层框架Mybatis,以及Spring Data JPA,这三个都是和关系型数据库进行交 ...

  7. 打造自己的数据访问层(二)

    上一篇打造自己的数据访问层(一)中,我们已了解了.NET对数据库操作的基本原理,并就Ado.net对象的使用提出了几点疑问: 1.如何由系统来判断数据库型. 2.如何消除这些重复代码. 而上篇中也提出 ...

  8. idata 数据访问组件库 (2021版)

    idata数据访问组件库(RX11)版本:  2022-09-20 下载: idata 数据组件库 for RAD Studio RX10.3.x (260) 发布于:2020-02-20    使用 ...

  9. SpringBoot_数据访问-JDBC自动配置原理

    整合最基本的JDBC和数据源,第一个MYSQL,导入mysql驱动的,第二个我们使用原生的JDBC,后面使用Mybatis和JPA再选相应的内容就行了,为了演示方便我也把WEB模块选中,我们在pom文 ...

最新文章

  1. 二、多并发实现接口压力测试
  2. 合唱队形(递增再递减的最长子序列)
  3. centos 安装redis
  4. Athentech Perfectly Clear中文版
  5. 公司禁用U盘和移动硬盘的方法
  6. 小程序 国际化_在国际化您的应用程序时忘记的一件事
  7. why I need a flow learn note.
  8. Elasticsearch整理笔记(四)
  9. swf文件关键字查找_牛鹭学院:学员笔记|文件(夹)的出生、成长到死亡
  10. dateframe取某列数据_数据清洗amp;预处理入门完整指南
  11. Android和ipad同步短信,Android通信录和短信转移到iphone的方法-QQ同步助手
  12. html纵向的跑马灯效果,单行文字垂直/水平跑马灯效果
  13. unity摄像头实物识别_MAD Gaze推出人脸识别AR智能眼镜+AI安防方案赋能智慧城市...
  14. 使用elasticsearch文件搜索系统助力亚马逊解决方案架构师认证考试
  15. Python repr()函数
  16. 了解衡量网络性能的四大指标:带宽、时延、抖动、丢包
  17. 苹果手机3D-Touch这个功能,其实是吃鸡神器!
  18. K8S有状态静态Pod经典示例
  19. 基于微信小程序开发的知乎答题王小游戏
  20. 哔哩哔哩视屏下载的几种方法

热门文章

  1. 设计模式 -- (14)中介者模式
  2. dell c6220II lsi阵列卡
  3. js 如何获取class的元素 以及创建方法getElementsByClassName
  4. 更改整个目录文件的所有权限
  5. 一步步创建 边栏 Gadget(一)
  6. 浅析VS2010反汇编 VS 反汇编方法及常用汇编指令介绍 VS2015使用技巧 调试-反汇编 查看C语言代码对应的汇编代码...
  7. BZOJ-1968: [Ahoi2005]COMMON 约数研究 (思想)
  8. 微软开源 rDSN 分布式系统开发框架
  9. ubuntu 16.04 搭建 python 开发环境
  10. Android经常使用的五种弹出对话框