MyBatis第二天

下面讲查询操作。

1、单条件模糊查询-第一种

<!-- 结果集(查询)的映射关系 -->
    <resultMap type="com.shxt.model.User" id="UserResultMap" autoMapping="true">
        <id column="id" property="userId"/>
        <result column="user_name" property="userName"/>
        <result column="age" property="age"/>
        <result column="sex" property="sex"/>
    </resultMap>
    <!-- 因为传过来的值是这样的    '可',所以如果写成'%#{user_name}%'会报错 -->
    <select id="like01" parameterType="string" resultMap="UserResultMap">
        select * from userinfo where user_name like #{user_name}
    </select>

测试代码:

    @Test
	public void test01() {
		List<User> list = this.sqlSession.selectList(User.class.getName()+".like01", "%可%");
		System.out.println(list);
	}

因为传过去的值是这样婶儿的:‘可’,两边带有单引号,如果xml文件中写的是‘%#{user_name}%’,则会由于匹配单引号的问题出现错误。

2、单条件模糊查询-第二种

 用的是SQL语句的一个拼接方法。

<select id="like02" parameterType="string" resultMap="UserResultMap">
        select * from userinfo where user_name like concat('%',#{user_name},'%')
</select>

concat('%',#{user_name},'%')

测试代码:

@Test
	public void test02() {
		List<User> list = this.sqlSession.selectList(User.class.getName()+".like02", "可");
		System.out.println(list);
	}

 3、多条件组合查询,使用恒等式解决方案

<!-- 3.多条件组合查询,使用恒等式解决方案 -->
    <!-- 因为有1=1这个恒等式,但是效率比较低,每次都要判断这个条件,所以,就有了效率较高的where标签 -->
    <select id="like03" parameterType="map" resultMap="UserResultMap">
        select * from userinfo where 1=1
        <if test="user_name != null and user_name !=''">
            and user_name like concat('%',#{user_name},'%')
        </if>
        <if test="sex != null and sex !=''">
            and sex=#{sex}
        </if>
    </select>

 为什么会写恒等式呢,是因为,若第一个标签存在内容,第二个标签也存在内容的话,就会在前面多出来一个and,所以使用了恒等式,这样就可以实现上述功能了。【但是效率偏低】 

测试代码:

@Test
	public void test03() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_name", "可");
		map.put("sex", "");

		List<User> list = this.sqlSession.selectList(User.class.getName()+".like03", map);
		System.out.println(list);
	}

 4、多条件组合查询,使用where标签

<!-- 4.多条件组合查询,使用where标签 -->
    <select id="like04" parameterType="map" resultMap="UserResultMap">
        select * from userinfo
        <where>
            <if test="user_name != null and user_name !=''">
                and user_name like concat('%',#{user_name},'%')
            </if>
            <if test="sex != null and sex !=''">
                and sex=#{sex}
            </if>
        </where>
    </select>

 使用where标签,如果有查询条件的话,则带上where,否则不带上。并且,会自动隐藏掉第一个标签内的and。

如:

测试代码:

@Test
	public void test04() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_name", "");
		map.put("sex", "");

		List<User> list = this.sqlSession.selectList(User.class.getName()+".like04", map);
		System.out.println(list);
	}

@Test
	public void test04() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_name", "可");
		map.put("sex", "");

		List<User> list = this.sqlSession.selectList(User.class.getName()+".like04", map);
		System.out.println(list);
	}

 

@Test
	public void test04() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_name", "");
		map.put("sex", "男生");

		List<User> list = this.sqlSession.selectList(User.class.getName()+".like04", map);
		System.out.println(list);
	}

@Test
	public void test04() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_name", "可");
		map.put("sex", "男生");

		List<User> list = this.sqlSession.selectList(User.class.getName()+".like04", map);
		System.out.println(list);
	}

5.多条件组合查询,使用trim标签,不推荐使用,需要知道几个属性的具体含义

        <!-- prefix:如果发现标签内有内容的时候,在内容的最前端,加入什么内容 -->
        <!-- suffix:如果发现标签内有内容的时候,在内容的最后端,加入什么内容 -->
        <!-- prefixOverrides:如果发现标签内有内容的时候,在内容的最前端,隐藏掉什么内容 -->
        <!-- suffixOverrides:如果发现标签内有内容的时候,在内容的最后端,隐藏掉什么内容 -->

<!-- 5.多条件组合查询,使用trim标签,不推荐使用,需要知道几个属性的具体含义 -->
    <select id="like05" parameterType="map" resultMap="UserResultMap">
        select * from userinfo
        <trim prefix="where " prefixOverrides="and |or ">
            <if test="user_name != null and user_name !=''">
                and user_name like concat('%',#{user_name},'%')
            </if>
            <if test="sex != null and sex !=''">
                and sex=#{sex}
            </if>
        </trim>
    </select>

意思是:如果标签内有内容的话,就要在内容的最前端加上“where ”,并且,隐藏掉"and "或者"or "。

下面讲更新操作。 

1、常用方法

<update id="update01" parameterType="com.shxt.model.User">
        update userinfo
        <set>
            <if test="userName != null and userName !=''">
                user_name=#{userName},
            </if>
            <if test="age != null">
                age=#{age},
            </if>
            <if test="sex != null and sex !=''">
                sex=#{sex}
            </if>
        </set>
        where id=#{userId}
</update>

测试代码:

@Test
	public void test06() {
		User user = new User();
		user.setUserId(5);
		user.setUserName("小红红");

		this.sqlSession.update(User.class.getName()+".update01",user);
		this.sqlSession.commit();
	}

2、

<update id="update02" parameterType="com.shxt.model.User">
        update userinfo
        <trim prefix="set " suffixOverrides=",">
            <if test="userName != null and userName !=''">
                user_name=#{userName},
            </if>
            <if test="age != null">
                age=#{age},
            </if>
            <if test="sex != null and sex !=''">
                sex=#{sex}
            </if>
        </trim>
        where id=#{userId}
</update>

下面讲添加操作。

1、动态添加语句

<!-- 8.动态添加语句 -->
    <insert id="add" parameterType="com.shxt.model.User">
        insert into userinfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <!-- 判断语句判断的是:持久化类是否传过来值了,所以是类中的属性 -->
            <if test="userName != null and userName !=''">
                user_name,<!-- 这个是表字段 -->
            </if>
            <if test="age != null">
                age,<!-- 这个是表字段 -->
            </if>
            <if test="sex != null and sex !=''">
                sex,<!-- 这个是表字段 -->
            </if>
        </trim>
        
        <!-- prefix:如果发现标签内有内容的时候,在内容的最前端,加入什么内容 -->
        <!-- suffix:如果发现标签内有内容的时候,在内容的最后端,加入什么内容 -->
        <!-- prefixOverrides:如果发现标签内有内容的时候,在内容的最前端,隐藏掉什么内容 -->
        <!-- suffixOverrides:如果发现标签内有内容的时候,在内容的最后端,隐藏掉什么内容 -->
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userName != null and userName !=''">
                #{userName},
            </if>
            <if test="age != null">
                #{age},
            </if>
            <if test="sex != null and sex !=''">
                #{sex},
            </if>
        </trim>
    </insert>

测试代码:

@Test
	public void test07() {
		User user = new User();
		user.setUserName("Mark");
		user.setAge(30);

		this.sqlSession.insert(User.class.getName()+".add",user);
		this.sqlSession.commit();
	}

2、foreach标签

<!-- 9.foreach标签 -->
    <!-- 如果你传递的是数组,那么久不要设置parameterType,因为比较难记忆 -->
    <select id="batch01" resultMap="UserResultMap">
        select * from userinfo where id in
        <!-- 如果你传递的是单个数组,只能设置为array -->
        <foreach collection="array" open="(" close=")" index="i" separator="," item="val">
        <!-- #{val} 等价于 #{array[${i}]} -->
            #{val}
        </foreach>
    </select>
    <!-- 10.foreach标签 -->
    <select id="batch02" parameterType="list" resultMap="UserResultMap">
        select * from userinfo where id in
        <!-- 如果你传递的是单个列表,只能设置为list -->
        <foreach collection="list" open="(" close=")" index="i" separator="," item="val">
            #{val}
           <!--  #{list[${i}]} -->
        </foreach>
    </select>

测试代码:

@Test
	public void test08() {
		int[] array = {1,3,10};
		List<User> list = this.sqlSession.selectList(User.class.getName()+".batch01",array);
		System.out.println(list);
	}

	@Test
	public void test09() {
		List<Integer> list = new ArrayList<>();
		list.add(1);
		list.add(3);
		list.add(10);

		List<User> userList = this.sqlSession.selectList(User.class.getName()+".batch02",list);
		System.out.println(userList);
	}

3、批量添加

<insert id="addBatch" parameterType="list">
        insert into userinfo (user_name, age, sex ) values
        <foreach collection="list" separator="," item="val" index="i">
        <!-- 下面的写法就要借鉴上面的写法++++++   #{val}==#{list[${i}]}  -->
        <!-- 所以获取 -->
        <!-- (#{list[${i}].userName}, #{list[${i}].age}, #{list[${i}].sex}) -->
         (#{val.userName}, #{val.age}, #{val.sex})
        </foreach>
    </insert>

测试代码:

@Test
	public void test10() {
		List<User> userList = new ArrayList<>();
		User user = new User();
		user.setUserName("刘备1");
		user.setAge(99);
		user.setSex("男");
		userList.add(user);

		user = new User();
		user.setUserName("刘备2");
		user.setAge(199);
		user.setSex("男");
		userList.add(user);

		user = new User();
		user.setUserName("刘备3");
		user.setAge(299);
		user.setSex("男");
		userList.add(user);

		System.out.println(userList);

		this.sqlSession.insert(User.class.getName()+".addBatch",userList);
		this.sqlSession.commit();
	}

现在看,还懂,以备下次看的时候不懂了,有的加注释了。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值