为什么使用动态代理:因为三层架构,界面层调用业务逻辑层的接口,要调用接口下的实现类。实现类为.xml文件,无法调用,使用动态代理可以解决
前提:将前面的Mybatis的配置配好
可以定义代码片断,可以进行逻辑判断,可以进行循环处理(批量处理),使条件判断更为简单.
1)<sql>:用来定义代码片断,可以将所有的列名,或复杂的条件定义为代码片断,供使用时调用.
2)<include>:用来引用<sql>定义的代码片断.
<!-- 定义代码片段-->
<sql id="allColumns">
id,
username,
birthday,
sex,
address
</sql>
//引用定义好的代码片断
<select id="getAll" resultType="users" >
select <include refid="allColumns"></include>
from users
</select>
可以对以前的代码进行优化
3)字段替代
接口里增加
//模糊用户名和地址查询
List<Users> getByNameOrAddress(
@Param("columnName") String columnName,
@Param("columnValue") String columnValue);
UserMapper里增加
<select id="getByNameOrAddress" resultType="users">
select
<include refid="allColumns">
</include>
from users
where ${columnName} like concat('%', #{columnValue}, '%')
</select>
4)<if>:进行条件判断
test条件判断的取值可以是实体类的成员变量,可以是map的key,可以是@Param注解的名称.
接口里增加
//按照指定的条件进行多条件查询
List<Users> getByCondition(Users users);
UserMapper里增加
<select id="getByCondition" parameterType="users" resultType="users">
select
<include refid="allColumns">
</include>
from users
<where>
<if test="userName != null and userName != ''">
and username like concat('%', #{userName}, '%')
</if>
<if test="birthday != null">
and birthday = #{birthday}
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if>
<if test="address != null and address != ''">
and address like concat('%', #{address}, '%')
</if>
</where>
</select>
测试类
@Test
public void testGetByCondition(){
Users users = new Users();
// users.setSex("1");
// users.setUserName("小");
// users.setAddress("南");
List<Users> list=mapper.getByCondition(users);
list.forEach(useruser -> System.out.println(useruser));
for (Users users1 : list) {
System.out.println(users1);
}
}
5)<set>:有选择的进行更新处理,至少更新一列.能够保证如果没有传值进来,则数据库中的数据保持不变
接口里增加
//有选择的更新
int updateBySet(Users users);
UserMapper里增加
<update id="updateBySet" parameterType="users">
update users
<set>
<if test="userName != null and userName != ''">
username=#{userName},
</if>
<if test="birthday != null">
birthday=#{birthday},
</if>
<if test="sex != null and sex != ''">
sex=#{sex},
</if>
<if test="address != null and address != ''">
address=#{address}
</if>
</set>
where id = #{id}
</update>
测试类
@Test
public void testUpdateSet(){
Users users = new Users();
users.setId(6);
users.setUserName("lla");
int update = mapper.updateBySet(users);
System.out.println(update);
sqlSession.commit();
}
6)<foreach>:用来进行循环遍历,完成循环条件查询,批量删除,批量增加,批量更新.
接口里增加
//查询多个指定id的用户信息
List<Users> getByIds(Integer []arr);
UserMapper里增加
<!--//查询多个指定id的用户信息
List<Users> getByIds(Integer []arr);-->
<select id="getByIds" resultType="users">
select
<include refid="allColumns">
</include>
from users
where id in
<!--
collection:用来指定入参的类型,如果是List集合,则为list,如果是Map集合,则为map,如果是数组,则为array.
item:每次循环遍历出来的值或对象
separator:多个值或对象或语句之间的分隔符
open:整个循环外面的前括号
close:整个循环外面的后括号
-->
<foreach collection="array" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select>
测试类
@Test
public void testGetByIds(){
Integer []array={2,4,6};
List<Users> byIds = mapper.getByIds(array);
byIds.forEach(users -> System.out.println(users));
}
批量删除与批量增加
接口里增加
//批量删除
int deleteBatch(Integer []arr);
//批量增加
int insertBatch(List<Users> list);
UserMapper里增加
<!--//批量删除
int deleteBatch(Integer []arr);-->
<delete id="deleteBatch">
delete
from users
where id in
<foreach collection="array" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
<!-- //批量增加
int insertBatch(List<Users> list);
private Integer id; 0
private String userName; null or ""
private Date birthday; null
private String sex; null
private String address; null
-->
<insert id="insertBatch">
insert into users(username, birthday, sex, address)
values
<foreach collection="list" item="u" separator=",">
(#{u.userName},#{u.birthday},#{u.sex},#{u.address})
</foreach>
</insert>
测试
@Test
public void testDeleteBatch(){
Integer []array={2,4};
int i = mapper.deleteBatch(array);
sqlSession.commit();
System.out.println(i);
}
@Test
public void testInsertBatch() throws ParseException {
Users u1 = new Users("aa", sf.parse("2002-02-02"), "2", "朝阳1");
Users u2 = new Users("bb", sf.parse("2002-02-02"), "2", "朝阳2");
Users u3 = new Users("cc", sf.parse("2002-02-02"), "2", "朝阳3");
Users u4 = new Users("dd", sf.parse("2002-02-02"), "2", "朝阳4");
List<Users> list=new ArrayList<>();
list.add(u1);
list.add(u2);
list.add(u3);
list.add(u4);
int i = mapper.insertBatch(list);
sqlSession.commit();
System.out.println(i);
}
还有一个批量更新在视频的95,想了解可以了解