我的mybatis入门(二)

Mybatis中包装类的使用

个人理解的包装类可以封装多个bean对象,在UserMapper.xml文件中统一写为parameterType=“QueryVo”,方便管理(菜鸟个人理解,欢迎大神指正、轻喷)

Mybatis的if标签和where标签

  1. if标签
    对传递的字段进行条件判断(比如说传递的字段是否为null等)
  2. where标签
    在多条件判断的时候,以前都在where后边先写一个1=1,避免出现where and name=#{name}的错误sql。where标签有两个作用:1.拼接where字段。2.去掉多条件sql首个条件前边的sql(只能去掉前边的and,不能去掉后边的and)

Mybatis foreach标签

对传递的集合遍历

以一个例子,对包装类、if、where、foreach做一个简单实用说明

UserMapper.xml内容
<!-- 测试if和where语句  -->
	<select id="queryUsersByIdAndName" parameterType="QueryVo" resultType="User">
		select * from user2
		<!-- where标签作用,1.添加where字段, 2.处理前边and(解决前边条件为空的情况,避免写1=1) -->
		<where>
			<!-- 不要忘记加user. -->
			<if test="user.age != null and user.age != ''">
				and age = #{user.age}
			</if>
			<if test="user.name != null and user.name != ''">
				and name = #{user.name}
			</if>
		</where>
	</select>
	
	<!-- 测试for循环与sql片段-->
	<sql id="commensql">
		select * from user2	
	</sql>
	<select id="queryUsersByIds" parameterType="QueryVo" resultType="User">
		<include refid="commensql"/>
		<where>
			id in
			<!-- collection:遍历的集合,这里是QueryVo的ids属性 -->
			<!-- item:遍历的项目,可以随便写,,但是和后面的#{}里面要一致 -->
			<!-- open:在前面添加的sql片段 -->
			<!-- close:在结尾处添加的sql片段 -->
			<!-- separator:指定遍历的元素之间使用的分隔符 -->
			<!-- 注意:如果是数组,collection必须是array,
			如果是集合,collection必须是list
			如果是包装类,collection必须对应属性名 -->
			<foreach collection="ids" item="id" separator="," open="(" close=")">
				#{id}
			</foreach>
		</where>
	</select>
UserMapper.java添加方法
	/**
	 * 1.多条件查询,测试where和if语句
	 * */
	public List<User> queryUsersByIdAndName(QueryVo vo);
	
	
	/**
	 * 1.测试使用包装类	2.测试foreach查询
	 * */
	public List<User> queryUsersByIds(QueryVo vo);
QueryVo内容
public class QueryVo implements Serializable {

	/**
	 * 用户bean
	 * */
	private User user;
	
	/**
	 * id的集合
	 * */
	private List<Integer> ids;

	// get set方法省略
}
方法调用
/**
	 * 多条件查询用户
	 * */
	@Test
	public void getUserByIdAndName() throws Exception {
		// 基本操作
		InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// sqlSession获取Mapper
		UserMapper mapper = sqlSession.getMapper(UserMapper.class);
		QueryVo vo=new QueryVo();
		User u=new User();
		u.setAge(12);
//		u.setName("张三");
		vo.setUser(u);
		List<User> user = mapper.queryUsersByIdAndName(vo);
		for(int i=0 ;i<user.size();i++){
			System.out.print(user.get(i));
		}
	}
	
	/**
	 * 查询多个id的信息
	 * */
	@Test
	public void getUserByIds() throws Exception {
		// 基本操作
		InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// sqlSession获取Mapper
		UserMapper mapper = sqlSession.getMapper(UserMapper.class);
		QueryVo vo=new QueryVo();
		List<Integer> ids=new ArrayList<>();
		ids.add(1);
		ids.add(2);
		ids.add(3);
		vo.setIds(ids);
		List<User> user = mapper.queryUsersByIds(vo);
		for(int i=0 ;i<user.size();i++){
			System.out.print(user.get(i));
		}
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值