2026/4/18 10:12:04
网站建设
项目流程
建设网站坂田,joomla 网站 html 空,软装设计公司名字,谷歌云wordpress绑定域名什么是MyBatis?• MyBatis是⼀款优秀的 持久层 框架#xff0c;⽤于简化JDBC的开发。• MyBatis本是 Apache的⼀个开源项⽬iBatis#xff0c;2010年这个项⽬由apache迁移到了google code#xff0c;并 且改名为MyBatis 。2013年11⽉迁移到Github.• 官⽹#xff1a;MyBati…什么是MyBatis?• MyBatis是⼀款优秀的 持久层 框架⽤于简化JDBC的开发。• MyBatis本是 Apache的⼀个开源项⽬iBatis2010年这个项⽬由apache迁移到了google code并 且改名为MyBatis 。2013年11⽉迁移到Github.• 官⽹MyBatis中⽂⽹ 在上⾯我们提到⼀个词持久层• 持久层指的就是持久化操作的层, 通常指数据访问层(dao), 是⽤来操作数据库的.简单来说 MyBatis 是更简单完成程序和数据库交互的框架也就是更简单的操作和读取数据库⼯具MyBatis入门mybatis操作数据库的步骤1. 准备工作创建springboot工程数据库表准备实体类等2. 引入mybatis相关依赖配置mybatis数据库连接信息3. 通过注解或者XML文件编写SQL语句4. 测试准备工程为了了导入mybatis必须加入mybatis framework和数据区driver如果是MySQL则加入MySQLdriver。项⽬⼯程创建完成后⾃动在pom.xml⽂件中导⼊Mybatis依赖和MySQL驱动依赖配置数据库连接字符串为了通过mybatis连接数据库需要数据库配置以下相关参数1. MySQL启动类2. 用户名3. 密码4. 数据库连接字符串我自己的配置信息 spring: datasource: url: jdbc:mysql://localhost:3306/mybatis_test?allowPublicKeyRetrievaltrueuseSSLfalseserverTimezoneAsia/ShanghaiuseUnicodetruecharacterEncodingutf-8 username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver mybatis: configuration: map-underscore-to-camel-case: true mapper-locations: classpath:mapper/UserInfoMapperXML.xml持久层代码持久层代码放在mapper中这也就是我们Dao层的另一种写法。其中resource文件夹中的mapper为src文件夹中mapper的xml文件映射。一个数据库操作接口对应着XML文件中的一条sql语句。我们在后面详细介绍。两种Mybatis的操作方法1. 使用注解通过注解直接操作数据库package com.spring.mybatis.mapper; import com.spring.mybatis.model.UserInfo; import org.apache.ibatis.annotations.*; import org.springframework.stereotype.Component; import java.util.List; Mapper public interface UserInfoMapper { Select(select * from user_info) ListUserInfo selectAll(); Select(select * from User_info where id#{id}) UserInfo selectById(int id); Options(useGeneratedKeys true, keyProperty id) Insert(insert into user_info(username, password, age) values(#{username}, #{password}, #{age})) Integer insertUser(UserInfo userInfo); Delete(delete from user_info where id#{id}) Integer deleteUser(int id); Update(update user_info set delete_flag #{delete_flag},phone#{phone} where id#{id}) Integer updateUser(int id, int delete_flag, String phone); }例如Select用于操作select语句Delete用于操作delete语句。在注解的括号中直接写入SQL语句放在接口中定义的抽象方法上。对于调用这个接口的其他类层只需要实例化这个接口然后调用相关抽象方法即可。package com.spring.mybatis.service; import com.spring.mybatis.mapper.UserInfoMapper; import com.spring.mybatis.model.UserInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; Service //service层 public class UserService { Autowired private UserInfoMapper userInfoMapper; //实例化mapper接口 public ListUserInfo getAllUsers() { //调用接口中的方法 return userInfoMapper.selectAll(); } public UserInfo selectById(int id) { //调用接口中的方法 return userInfoMapper.selectById(id); } public Integer insertUser(UserInfo userInfo) { //调用接口中的方法 System.out.println(userInfo.getId()); return userInfoMapper.insertUser(userInfo); } }2. XML文件XML文件实现Mybatis操作原理如下interface方法定义和xml方法实现相互映射共同组成数据持久层mapperimport com.example.demo.model.UserInfo; import org.apache.ibatis.annotations.Mapper; import java.util.List; Mapper public interface UserInfoXMlMapper { ListUserInfo queryAllUser(); }操作首先配置连接字符串和Mybatisapplication.yml⽂件, 配置内容如下# 数据库连接配置 spring: datasource: url: jdbc:mysql://127.0.0.1:3306/mybatis_test? characterEncodingutf8useSSLfalse username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver # 配置 mybatis xml 的⽂件路径在 resources/mapper 创建所有表的 xml ⽂件 mybatis: mapper-locations: classpath:mapper/**Mapper.xml #这里是xml文件的位置随后添加mapper接口import com.example.demo.model.UserInfo; import org.apache.ibatis.annotations.Mapper; import java.util.List; Mapper public interface UserInfoXMlMapper { ListUserInfo queryAllUser(); }随后添加UserInfoXMLMapper.xml文件放在resource文件夹中的mapper文件夹初始化xml文件夹配置?xml version1.0 encodingUTF-8? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom.spring.mybatis.mapper.UserInfoMapperXML /mapper注意xml文件位置要和yml配置文件中定义的路径相对应Mybatis的操作技巧1. 打印日志在yml配置文件中配置如下代码即可打印日志mybatis: configuration: # 配置打印 MyBatis⽇志 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl看到SQL的执行内容参数传递和执行结果2. 参数传递一般而言SQL语句中的内容不能写成固定值因为如果这样写每次查找都要修改值例如查找id4的用户,对应的SQL就是:select * from user_info where id4。但是这样的话, 只能查找id4 的数据, 所以SQL语句中的id值不能写成固定数值解决方案在queryById方法中添加⼀个参数(id)将方法中的参数传给SQL语句Select(select username, password, age, gender, phone from user_info where id #{id} ) //此处用的是传值而非固定值 UserInfo queryById(Integer id);也可以通过 Param , 设置参数的别名, 如果用 Param 设置别名, #{...}的属性名必须和 Param 设置的⼀样Select(select username, password, age, gender, phone from user_info where id #{userid} ) UserInfo queryById(Param(userid) Integer id);利用对象传递参数如果设置了 Param 属性, #{...} 需要使⽤ 参数.属性 来获取Insert(insert into user_info (username, password, age, gender, phone) values (#{userInfo.username},#{userInfo.password},#{userInfo.age},# {userInfo.gender},#{userInfo.phone})) //因为使用了Param在SQL获取参数时需要加上userInfo. Integer insert(Param(userInfo) UserInfo userInfo);使用对象传递参数的好处好处1自动匹配参数Insert(insert into user_info (username, password, age, gender, phone) values (#{username},#{password},#{age},#{gender},#{phone})) Integer insert(UserInfo userInfo); 直接传入UserInfo类自动匹配参数好处2返回主键Insert 语句默认返回的是 受影响的⾏数。但有些情况下, 数据插⼊之后, 还需要有后续的关联操作, 需要获取到新插⼊数据的id如果想要拿到自增id, 需要在Mapper接口的方法上添加⼀个Options的注解Options(useGeneratedKeys true, keyProperty id) Insert(insert into user_info (username, age, gender, phone) values (# {userInfo.username},#{userInfo.age},#{userInfo.gender},#{userInfo.phone})) Integer insert(Param(userInfo) UserInfo userInfo);useGeneratedKeys这会令 MyBatis 使⽤ JDBC 的 getGeneratedKeys ⽅法来取出由数据库内 部⽣成的主键⽐如像 MySQL 和 SQL Server 这样的关系型数据库管理系统的⾃动递增字 段默认值false.keyProperty指定能够唯⼀识别对象的属性MyBatis 会使用 getGeneratedKeys 的返回值或 insert 语句的 selectKey 子元素设置它的值默认值未设置unset注意事项如果SQL中的字段和我们类属性名称不一致将会发生匹配不上字段的情况。如下图框中的三个参数长相不同无法相互匹配解决方案1. 起别名直接在SQL语句中起别名Select(select id, username, password, age, gender, phone, delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from user_info) public ListUserInfo queryAllUser();2. 结果映射Select(select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info) //此处进行结果映射id resultMap指的是这条结果映射的id名为resultMap其他地方如果需要同样的映射可以直接调用 Results(id resultMap,value { Result(column delete_flag,property deleteFlag), Result(column create_time,property createTime), Result(column update_time,property updateTime) }) ListUserInfo queryAllUser(); Select(select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info where id #{userid} ) //此处直接调用上文定义的结果映射id ResultMap(value resultMap) UserInfo queryById(Param(userid) Integer id);3. 配置文件开启驼峰命名推荐mybatis: configuration: map-underscore-to-camel-case: true #配置驼峰⾃动转换3. #{} 和 ${}直接说差别#{}为预编译SQL${}为直接字符串拼接SQL即时SQL预编译SQL可以提高效率编译⼀次之后会将编译后的SQL语句缓存起来后面再次执行这条语句时不会再次编译 (只是输⼊的参数不同), 省去了解析优化等过程, 以此来提⾼效率预编译SQL可以防止SQL注入。比如sql 注入代码 or 11如果使用${}就会被sql注入而#{}就不会。4. 切换数据库连接池数据库连接池负责分配、管理和释放数据库连接它允许应用程序重复使用⼀个现有的数据库连接而不是再重新建⼀个.常⻅的数据库连接池 C3P0DBCPDruidHikariMybatis操作数据库进阶1. 动态SQLif标签注册分为两种字段必填字段和非必填字段那如果在添加用户的时候有不确定的字段传入程序应该如何实现呢这个时候就需要使⽤动态标签 来判断了如添加的时候性别 gender 为非必填字段具体实现如下insert idinsertUserByCondition INSERT INTO userinfo ( username, password, age, if testgender ! null gender, /if phone) VALUES ( #{username}, #{age}, if testgender ! null #{gender}, /if #{phone}) /inserttrim标签之前的插入用户功能只是有⼀个 gender 字段可能是选填项如果有多个字段⼀般考虑使用标签结合标签对多个字段都采取动态生成的方式。标签中有如下属性• prefix整个语句块以prefix的值作为前缀• suffix整个语句块以suffix的值作为后缀• prefixOverrides整个语句块要去除掉的前缀• suffixOverrides整个语句块要去除掉的后缀还有如where标签set标签foreach标签include标签等在这里不再详细讲解。总之这些标签可以让SQL语句更加灵活适应不同的操作条件。