Mybatis
Mybatis是一个持久化框架,相比jdbc减少很多繁杂重复的代码量,将SQL语句从代码中分离,降低了耦合性。
一、基本配置
1.创建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>
<!-- 配置标准列名匹配Java驼峰命名 -->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- 配置别名 -->
<typeAliases>
<package name="com.baiduyun.pojo"/>
</typeAliases>
<environments default="test">
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://192.168.1.25:3306/class_2?useSSL=false&useUnicode=true&characterEncoding=UTF8&allowPublicKeyRetrieval=true&serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--注册接口配置文件-->
<mappers>
<mapper resource="mappers/StudentMapper.xml"/>
</mappers>
</configuration>
2.创建接口,定义方法
public interface StudentMapper {
Student selectById(Integer stuId);
}
3.创建接口配置文件StudentMapper.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.baiduyun.mapper.StudentMapper">
<select id="selectById" resultType="student">
select
<include refid="defaultColumns" />
from t_student where stu_id=#{stuId}
</select>
</mapper>
4.写一个测试类测试
import java.io.FileReader;
public class Test {
public static void main(String[] args) {
// 加载配置文件
FileReader reader = new FileReader(Test.class.getClassLoader().getResource("mybatis.xml").getPath());
// 获取sqlSession对象
SqlSession session = SqlSessionFactoryBuilder.build(reader).openSession();
// 获取Mapper接口对象并调用方法
StudentMapper mapper = session.getMapper(StudentMapper.class);
mapper.selectbyId(1);
}
}
二、使用mybatis进行基本crud操作
①基本查询
<select id="一般用方法名称,也可自行定义" result="返回结果元素的类型">
select * from 表名
</select>
②条件查询
<select id="一般用方法名称,也可自行定义" result="返回结果元素的类型">
select * from 表名 where 列名=值 / 列名<![CDATA[>]]>值 / 列名<![CDATA[<]]>值
</select>
③基本添加
<insert id="一般用方法名称,也可自行定义">
insert into 表名 values(#{val1},#{val2},...)
</insert>
④基本删除
<delete id="一般用方法名称,也可自行定义">
delete from 表名 where 列名=#{val1}
</delete>
⑤基本修改
<update id="一般用方法名称,也可自行定义">
update 表名 set 列名=#{val1},列名2=#{val2},... where ${}=#{val]
</update>
CDATA
标签的作用:类似Java中的转义字符,在xml中转义一些敏感字符,如<
或>
#{}
的作用:用于代表设置参数的值,值的位置用?
占位可以避免SQL注入
${}
的作用:与#{}差不多也是一个占位符,只不过是直接将值不做处理直接传入,有SQL注入问题
三、在mybatis中实现一对一关系
在实现一对一关系之前,需要先了解resulMap
在使用mybatis进行查询时会发现JavaBean中的某些属性没有赋值,这是类属性名与表列名不一致导致的,mybatis不知道该把值传给谁,所以默认没有做任何处理
使用resultMap定义映射关系
<!--使用resultMap告诉Mybatis不一致的表列名对应的属性名-->
<resultMap id="studentMap">
<result property="类属性名" column="对应的表列名"/>
</resultMap>
引用映射关系
<select id="seletAll" resultMap="studentMap">
select *from 表名称
</select>
以上就是resultMap的基本用法,下面使用resultMap处理一对一关系
定义resultMap映射关系
<resultMap id="studentMap">
<result property="类属性名" column="对应的表列名"/>
...
<!--
select属性:设置获取数据需调用的查询方法id
-->
<association property="类属性名" column="对应的表列名" select="一个查询语句的id"/>
</resultMap>
之后在select标签的resultMap属性引用id即可,到此就在Mybatis中实现了一对一关系,这也是Mybatis的强大之处
4.动态SQL
动态添加
<insert>
insert into 表名(列名1,列名2,...) values
<foreach collection="参数类型" item="item" separator=",">
(#{item.属性1},#{item.属性2},#{item.属性3},...)
</foreach>
</insert>
foreach标签属性解析
collection:设置参数类型
1).如果传参为list集合,值为 list;
2).如果传参为数组,值为 array
item:表示每一个迭代元素的别名
separator:表示每一个迭代元素之间的分隔符
open:以什么开始
close:以什么结束
separator:以什么分隔
动态条件查询
<select>
select *from 表名
<where>
<if test="条件表达式">
列名=#{xx}
</if>
<if test="条件表达式">
列名=#{xx}
</if>
</where>
</select>
批量删除
<delete>
delete from 表名 where id in
<foreach collection="list" item="" open="(" separator="," close=")">
#{item}
</foreach>
</delete>