目录
概念
MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
步骤
- 导入依赖jar包
- 核心配置文件mybatis-config.xml
- 创建实体类
- 映射文件UserMapper.xml
- 核心配置文件中添加映射文件
- 创建测试类
映射文件参数解析
别名:alias
在sqlMapConfig.xml配置,在映射文件中直接写对象名称即可,需要在核心配置文件配置
参数值:paramterType
指定参数类型,通常制定一个对象类型。
返回值:resultType
非常重要的东西,即完成ORM的映射关系所在。这里指定的cd.tedu.mybatis.domain.User代表把结果集转换成一个User对象实例。
返回值:resultMap
resultMap 用于对复杂对象结构时,对应的ResultMap结构名称
映射文件编写sql
提供了四种标签
- select
- delete
- insert
- alter
映射文件中的通配符
#
底层使用Preparement高效且安全
$
底层使用的是preparement存在sql注入问题
格式
#{字段名} 字段名不需要再加引号 外面也不需要引号
sql语句中存在特殊字符
<![CDATA[ ?? ]]>
直接使用转义字符
动态sql
提取字段
提取方式: <sql id="名字">字段名,字段名</sql>
查询字段比*查询效率高
使用字段
select <include refid="提取时设置的id"/>
if
在查询之前先进性判断
select id from user <if test="id!=null">where id=1</if>
where
去掉条件中可能多余的and或者or:
select id from user<where><if test="id=1">id=1</if><if test="id=3">id=3</if><where>
set
去掉最后可能多余的逗号:
<set>
<if test="tname != null">tname=#{tname},</if>
<if test="tsex != null">tsex=#{tsex},</if>
<if test="tbirthday != null">tbirthday=#{tbirthday},</if>
<if test="prof != null">prof=#{prof},</if>
<if test="depart != null">depart=#{depart}</if>
</set>
<set>
<if test="tname != null">tname=#{tname},</if>
<if test="tsex != null">tsex=#{tsex},</if>
<if test="tbirthday != null">tbirthday=#{tbirthday},</if>
<if test="prof != null">prof=#{prof},</if>
<if test="depart != null">depart=#{depart}</if>
</set>
WHERE tno=#{tno}
</update>
foreach
- collection="ids" collection为数组名字
- collection的值只能是数组 list 或map中的key
- item="id" 每个元素的值的名字
- separator="," 表示逗号分隔
- open="(" close=")" separator=","表示值以什么开头和以什么结尾
- 获取值${ids}
接口映射
创建接口
映射文件中namespace的值要与接口的全路径一致 创建抽象方法
方法的返回值与查询结果一致 方法名字与增删改查中的id值相同
主启动类添加MapperScan("接口所在包")
或者在接口上添加Mapper注解
创建测试类 注入接口直接调用方法即可
Mapper与MapperScan注解底层使用的是jdk动态代理
示例代码
创建核心配置文件yml
server:
port: 8090
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jt?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
#如果数据库密码以数字0开头 则必须使用""号包裹
#password: "01234"
#SpringBoot整合Mybatis配置 SpringBoot整合Mybatis
mybatis:
#定义别名包: 实现对象映射
type-aliases-package: com.jt.pojo
#加载映射文件一个接口对应一个映射文件
mapper-locations: classpath:/mybatis/*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
#不打印日志
debug: false
创建核心配置文件
<?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">
<!--规则: namespace必须与接口一一对应 -->
<mapper namespace="com.jt.mapper.UserMapper">
<!--CRUD
定义别名包: 添加了别名包之后可以简化resultType编辑.
-->
<select id="getAll" resultType="User">
select * from demo_user
</select>
<!--驼峰命名规则
表字段: user_id,user_name
对象的属性: userId,userName
resultType: 保证属性与字段名称必须一致.
Mybatis提供了驼峰命名规则:
规则: 字段user_id~~~去除_线~~~之后映射对象的属性
userId
-->
</mapper>
创建接口
package com.jt.mapper;
import com.jt.pojo.User;
import java.util.List;
//Mybatis接口如何交给Spring容器管理!!!!!
public interface UserMapper {
//查询所有demo_user表的数据
List<User> getAll();
}