Mybatis_06 动态SQL

重点SQL动态语句

1、if标签
	<!--SQL语句:select * from user where 1=1 and username=? and sex=?-->
	
	select * from user where 1=1
	<if test="username !=null">
		and username = #{username}
	</if>
	<if test="sex">
		and sex=#{sex}
	</if>
2、where标签
	<!--  SQL语句:Preparing: select * from user WHERE username = ? and sex=?  -->
	select * from user
	<where>
		<if test="username !=null">
			and username = #{username}
		</if>
		<if test="sex">
			and sex=#{sex}
		</if>
	</where>
	
3、foreach标签
	SQL语句:select 字段 from user where id in {?}
		<foreach>标签用于遍历集合,他是属性:
			collection:代表要遍历的集合元素,注意是不要写成#{}
			open:代表语句的开始部分。
			close:代表结束部分
			item:代表遍历集合的每个元素,生成的变量名
			speractor:代表分隔符。
	示例代码:
		<!--SQL语句:Preparing: select * from user WHERE id in ( ? , ? , ? , ? , ? ) -->
		select * from user 
		<where>
			<if test="ids!=null and ids.size>0">
				<foreach collection="ids" open="and id in (" close=")" item="id" separator=",">
					#{id}
					<!-- item="id"  #{id}这两个参数必须一样 是同一个值-->
				</foreach>
			</if>
		</where>
	
4、封装重复的SQL语句
	定义:
		<!-- 了解的内容,抽取重复的SQL语句 -->
		<sql id="defaultUser">select * from user</sql>		
	使用:
		<!-- 根据id查询用户 -->
		<select id="findById" parameterType="INT" resultType="com.song.domain.User">
			<include refid="defaultUser"></include> where id=#{uid}
		</select>

1、实体类User、QueryVo

public class User implements Serializable{
	
	private Integer id;
	private String username;
	private	Date birthday;
	private String sex;
	private String address;
}

public class QueryVo {
	private User user;
}

2、编写持久层接口

/**
	 * 根据传入的参数条件查询:
	 * 		有可能有用户名,有可能有密码,有可能有地址,
	 * 		也有可能都有,也有可能都没有。
	 */
	List<User> findUserByCondition(User user);
	/**
	 * 根据Queryvo中提供的id集合,查询用户信息
	 * @param vo
	 * @return
	 */
	List<User> findUserInIds(QueryVo vo);

3、编写IUserDao.xml

<!-- 根据条件查询 -->
	<select id="findUserByCondition" resultType="com.song.domain.User" parameterType="com.song.domain.User">
		select * from user
		<where>
			<if test="username !=null">and username = #{username}</if>
			<if test="sex">and sex=#{sex}</if>
		</where>
		<!--  Preparing: select * from user WHERE username = ? and sex=?  -->
		
		 <!-- 	select * from user where 1=1
				<if test="username !=null">
					and username = #{username}
				</if>
				<if test="sex">
					and sex=#{sex}
				</if>
		 -->
	</select>
	
	<!-- 根据Queryvo中的集合 -->
	<select id="findUserInIds" resultType="com.song.domain.User" parameterType="com.song.domain.QueryVo">
		select * from user 
		<where>
			<if test="ids!=null and ids.size>0">
				<foreach collection="ids" open="and id in (" close=")" item="id" separator=",">
					#{id}
					<!-- item="id"  #{id}这两个参数必须一样 是同一个值-->
				</foreach>
			</if>
		</where>
	</select>

4、编写测试类


	/**
	 * 测试查询所有
	 */
	@Test
	public void testFindByCondition(){
		User u = new User();
		u.setUsername("小马宝莉");
		u.setSex("女");
		List<User> users = userDao.findUserByCondition(u);
		for(User user:users) {
			System.out.println(user);
		}
	}
	/**
	 * 测试foreach标签的使用所有
	 */
	@Test
	public void testFindInIds(){
		QueryVo vo = new QueryVo();
		List<Integer> list = new ArrayList<Integer>();
		list.add(41);
		list.add(43);
		list.add(45);
		list.add(46);
		list.add(48);
		vo.setIds(list);
		List<User> users = userDao.findUserInIds(vo);
		for(User user:users) {
			System.out.println(user);
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值