MyBatis--动态sql练习

StudentDAO类

public interface StudentDAO {
	//<if></if>标签练习
	List<Student> selectStudentByIf(Student student);
	//<where></where>标签练习
	List<Student> selectStudentByWhere(Student student);
	//<choose>标签练习
	List<Student> selectStudentByChoose(Student student);
	//<foreach>标签练习
	List<Student> selectStudentByForeachArray(int[] ids);
	List<Student> selectStudentByForeachList(List<Integer> ids);
	List<Student> selectStudentByForeachListObject(List<Student> students);
	int getCount();
	//<sql>标签练习,可以把重复的sql代码写在<sql>标签了,然后在需要的地方用<include标签>引入
	List<Student> selectStudentBySqlFragment();
}

StudentDAO.xml映射文件

<?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">
 
<mapper namespace="com.woniu.dao.StudentDAO">
	<!-- if标签 -->
	<select id="selectStudentByIf" resultType="Student" parameterType="Student">
		select * from student where 1=1
		<if test="sname!=null and sname!='' ">
			and sname like '%' #{sname} '%'
		</if>
		<if test="sage>0">
			and sage>#{sage}
		</if>
	</select>
	<!-- where标签  -->
	<select id="selectStudentByWhere" resultType="Student" parameterType="Student">
		select * from student
		<where>
			<if test="sname!=null and sname!='' ">
			and sname like '%' #{sname} '%'
		</if>
		<if test="sage>0">
			and sage>#{sage}
		</if>
		</where>
	</select>
	<!-- choose标签 -->
	<select id="selectStudentByChoose" resultType="Student" parameterType="Student">
		select * from student
		<where>
			<choose>
				<when test="sname!=null and sname!=''">				 
					and sname like '%' #{sname} '%'
				</when>
				<when test="sage>0">				
					and sage>#{sage}					
				</when>
				<otherwise>
				 and 1=2
				</otherwise>
			</choose>
		</where>
	</select>
	<!-- foreach标签 -->
	<select id="selectStudentByForeachArray" resultType="Student" >
		select * from student
		<if test="array!=null and array.length>0">
			where sid in
			<foreach collection="array" open="(" close=")" separator="," item="id">
				#{id}
			</foreach>
		</if>
	</select>
	
	<select id="selectStudentByForeachList" resultType="Student" >
		select * from student
		<if test="list!=null and list.size>0">
			where sid in
			<foreach collection="list" open="(" close=")" separator="," item="id">
				#{id}
			</foreach>
		</if>
	</select>
	
	<select id="selectStudentByForeachListObject" resultType="Student" >
		select * from student
		<if test="list!=null and list.size>0">
			where sid in
			<foreach collection="list" open="(" close=")" separator="," item="id">
				#{id.sid}
			</foreach>
		</if>
	</select>
	
	<select id="getCount" resultType="int" parameterType="Student">
		select count(*) from student
	</select>
	<!-- sql标签,include引入 -->
	<select id="selectStudentBySqlFragment" resultType="Student" parameterType="Student">
		select  <include refid="myslq" />student
	</select>
	<sql id="myslq">* from</sql>
</mapper>

StudentDAOImpTest.java测试类

public class StudentDAOImpTest {
	SqlSession sqlSession;
	StudentDAO studentDaoImp;
	
	@Before
	public void setUp() {
		sqlSession=MyBatisUtil.getSqlSession();
		//getMapper:底层就是使用动态代理,生成接口的实现类
		studentDaoImp = sqlSession.getMapper(StudentDAO.class);
	}
	
	@After
	public void tearDown() {
		if(sqlSession!=null) {
			sqlSession.close();
		}
	}
	//<if></if>标签练习
	@Test
	public void selectStudentByIf() {
		Student student = new Student();
		student.setSname("小明");
		System.out.println(studentDaoImp.selectStudentByIf(student));
	}
	//<where></where>标签练习
	@Test
	public void selectStudentByWhere() {
		Student student = new Student();
		student.setSname("小明");
		System.out.println(studentDaoImp.selectStudentByWhere(student));
	}
	//<choose>标签练习
	@Test
	public void selectStudentByChoose() {
		Student student = new Student();
		student.setSname("小明");
		System.out.println(studentDaoImp.selectStudentByChoose(student));
	}
	//<foreach>标签练习:int[]数组
	@Test
	public void selectStudentByForeachArray() {
		int[] ids = new int[3];
		ids[0]=1;
		ids[1]=2;
		ids[2]=3;
		System.out.println(studentDaoImp.selectStudentByForeachArray(ids));
	}
	//<foreach>标签练习:list集合,存基本数据类型
	@Test
	public void selectStudentByForeachList() {
		List<Integer> ids = new ArrayList();
		ids.add(1);
		ids.add(2);
		ids.add(3);
		ids.add(4);
		System.out.println(studentDaoImp.selectStudentByForeachList(ids));
	}
	//<foreach>标签练习:list集合,存对象
	@Test
	public void selectStudentByForeachListObject() {
		List<Student> ids = new ArrayList();
		ids.add(new Student(1,null,null,null,null));
		ids.add(new Student(2,null,null,null,null));
		ids.add(new Student(3,null,null,null,null));
		ids.add(new Student(4,null,null,null,null));
		System.out.println(studentDaoImp.selectStudentByForeachListObject(ids));
	}
	//查询所有学生数据
	@Test
	public void getCount() {
		System.out.println(studentDaoImp.getCount());
	}
	//<sql>标签练习,可以把重复的sql代码写在<sql>标签了,然后在需要的地方用<include标签>引入
	@Test
	public void selectStudentBySqlFragment() {
		System.out.println(studentDaoImp.selectStudentBySqlFragment());
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值