MyBatis详解
总体思路
Mapper代理开发
目的:解决原生方式中的硬编码,简化后期执行sql
1,2,
3,
在mapper中定义方法,方法名就是sql映射文件中的sql语句的id并保存参数类型和返回值一致
4,获取Mapper接口代理对象并调用对应方法完成sql执行
MyBatis核心配置文件
<environments>
配置数据库连接环境信息,可以配置多个environment,通过default属性切换不同的environment
(配置文件完成)增删改查
三步:编写接口方法,编写sql,执行方法
在xml中使用resultmap来编写sql
<?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.itheima.mapper.BrandMapper">
<!-- id为下面select语句的唯一标识;resultType为返回结果值,需要我们包装成pojo类型-->
<!-- statement-->
<!-- 数据库表的字段名称实体类的属性名称不一样,则不能自动封装数据-->
<!-- 但是可以在sql语句里起别名来避免-->
<!-- resultmap可以给字段名起映射-->
<resultMap id="select_all" type="com.itheima.pojo.Brand">
<result column="brand_name" property="brandName"/>
<result column="company_name" property="companyName"/>
</resultMap>
<!-- 下面的resultType改成resultMap来作选择-->
<select id="selectAll" resultMap="select_all">
select *
from tb_brand;
</select>
<!-- <sql id="BrandAll">-->
<!-- id, brand_name as brandName, company_name as companyName, ordered, description, status-->
<!-- </sql>-->
<!-- <select id="selectAll" resultType="com.itheima.pojo.Brand">-->
<!-- select-->
<!-- <include refid="BrandAll"/>-->
<!-- from tb_brand;-->
<!-- </select>-->
<!-- <select id="selectAll" resultType="com.itheima.pojo.Brand">-->
<!-- select id, brand_name as brandName, company_name as companyName, ordered, description, status-->
<!-- from tb_brand;-->
<!-- </select>-->
<!-- <select id="selectAll" resultType="com.itheima.pojo.Brand">-->
<!-- select *-->
<!-- from tb_brand;-->
<!-- </select>-->
</mapper>
查询
查询所有
步骤:
- 编写接口方法:Mapper接口;参数();结果List< Brand>
- 编写sql语句:SQL映射文件
查看详情
和上面一样,只是有参数而已,而且参数是行id(int)
特殊字符处理,如“<”,可以用CDTATA区
也可以用转义字符(与web相同:<
即<
)
条件查询
多条件查询
相当于输入框有多个筛选栏
单条件查询
书写sql时可以采用switch case模式
<select id="selectByConditionSingle" resultMap="differences">
select *
from tb_brand
<where>
<choose>
<when test="status!=null">
status=#{status}
</when>
<when test="companyName!=null and companyName!=''">
company_name like #{companyName}
</when>
<when test="brandName!=null and brandName!=''">
brand_name like #{brandName}
</when>
</choose>
</where>
</select>
但是要单条件查询的话注解就不是很好用了,所有需要采用封装
在接口里写入
List<Brand> selectByConditionSingle(Brand brand);
然后到Test文件里封装对象
Brand brand = new Brand();
brand.setStatus(status);
brand.setCompanyName(companyName);
brand.setBrandName(brandName);
然后修改执行方法
List<Brand> condition = brandMapper.selectByConditionSingle(brand);
System.out.println(condition);
即可输出
添加
基础添加
老样子,写接口,写sql,调用方法
接口
void add(Brand brand);
<!-- 添加-->
<insert id="add">
insert into tb_brand (brand_name, company_name, ordered, description, status)
values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
</insert>
//4.1.执行方法
brandMapper.add(brand);
//4.2.提交事务
sqlSession.commit();//手动提交事务
也可以在第二步默认开事务
SqlSession sqlSession = sqlSessionFactory.openSession(true);
主键返回
有时候需要在提交后收到添加字段的id值,这时候可以在xml里给sql语句添加两个属性:useGeneratedKeys="true" keyProperty="id"
然后再在调用方法后面输出id即可
修改
修改全部字段
也是这个套路
修改动态字段
比如用户只是修改其中部分,我们就要用动态sql
和前面查询的动态sql类似,我们也用<set>
替代set
并添加<if>
来判断条件
删除
删除一个
老样子
<delete id="deleteById">
delete
from tb_brand
where id = #{id};
</delete>
批量删除
那就是搞一个id数组一块儿接收