mybatis中的智能标签之二
生活随笔
收集整理的這篇文章主要介紹了
mybatis中的智能标签之二
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
智能標簽foreach-Array, 智能標簽foreach-List數據組,智能標簽 foreach list自定義類型
接口:
/**
* 智能標簽foreach-Array
* @param num
* @return
*/
public List<Student> findByForeachArray(int[] num);
/**
* 智能標簽foreach-List
* @param list
* @return
*/
public List<Student> findByForeachList(List<Integer> list);
/**
* 智能標簽 foreach list自定義類型
* @param list
* @return
*/
public List<Student> findByForeachListStudent(List<Student> list);
xml文件(小配置) <!--智能標簽foreach-array-->
<select id="findByForeachArray" resultType="student">
SELECT *from Student
<where>
<if test="array.length>0">
id in
<foreach collection="array" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</if>
</where>
</select>
<!--智能標簽foreach-list數組-->
<select id="findByForeachList" resultType="student">
SELECT *from Student
<where>
<if test="list.size>0">
id in
<foreach collection="list" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</if>
</where>
</select>
<!--智能標簽foreach-list自定義類型-->
<select id="findByForeachListStudent" resultType="student">
SELECT *from Student
<where>
<if test="list.size>0">
id in
<foreach collection="list" open="(" close=")" separator="," item="it">
#{it.id}
</foreach>
</if>
</where>
</select>
測試類:
/**
* 智能標簽foreach-array
*/
@Test
public void findByForeachArray(){
SqlSession session = MyBatisUtil.getSession();
IStudentDAO mapper = session.getMapper(IStudentDAO.class);
int[] num={2,3,4};
List<Student> list = mapper.findByForeachArray(num);
for (Student item:list){
System.out.println(item.getName());
}
session.commit();
session.close();
}
/**
* 智能標簽foreach-list數組
*/
@Test
public void findByForeachList(){
SqlSession session = MyBatisUtil.getSession();
IStudentDAO mapper = session.getMapper(IStudentDAO.class);
List<Integer> list=new ArrayList<Integer>();
list.add(2);
list.add(9);
List<Student> lists = mapper.findByForeachList(list);
for (Student item:lists){
System.out.println(item.getName());
}
session.commit();
session.close();
}
/**
* 智能標簽foreach-list自定義類型
*/
@Test
public void findByForeachListStudent(){
SqlSession session = MyBatisUtil.getSession();
IStudentDAO mapper = session.getMapper(IStudentDAO.class);
List<Student> list=new ArrayList<Student>();
Student student=new Student();
student.setId(2);
Student students=new Student();
students.setId(9);
list.add(student);
list.add(students);
List<Student> lists = mapper.findByForeachListStudent(list);
for (Student item:lists){
System.out.println(item.getName());
}
session.commit();
session.close();
}
?
接口:
/**
* 智能標簽foreach-Array
* @param num
* @return
*/
public List<Student> findByForeachArray(int[] num);
/**
* 智能標簽foreach-List
* @param list
* @return
*/
public List<Student> findByForeachList(List<Integer> list);
/**
* 智能標簽 foreach list自定義類型
* @param list
* @return
*/
public List<Student> findByForeachListStudent(List<Student> list);
xml文件(小配置) <!--智能標簽foreach-array-->
<select id="findByForeachArray" resultType="student">
SELECT *from Student
<where>
<if test="array.length>0">
id in
<foreach collection="array" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</if>
</where>
</select>
<!--智能標簽foreach-list數組-->
<select id="findByForeachList" resultType="student">
SELECT *from Student
<where>
<if test="list.size>0">
id in
<foreach collection="list" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</if>
</where>
</select>
<!--智能標簽foreach-list自定義類型-->
<select id="findByForeachListStudent" resultType="student">
SELECT *from Student
<where>
<if test="list.size>0">
id in
<foreach collection="list" open="(" close=")" separator="," item="it">
#{it.id}
</foreach>
</if>
</where>
</select>
測試類:
/**
* 智能標簽foreach-array
*/
@Test
public void findByForeachArray(){
SqlSession session = MyBatisUtil.getSession();
IStudentDAO mapper = session.getMapper(IStudentDAO.class);
int[] num={2,3,4};
List<Student> list = mapper.findByForeachArray(num);
for (Student item:list){
System.out.println(item.getName());
}
session.commit();
session.close();
}
/**
* 智能標簽foreach-list數組
*/
@Test
public void findByForeachList(){
SqlSession session = MyBatisUtil.getSession();
IStudentDAO mapper = session.getMapper(IStudentDAO.class);
List<Integer> list=new ArrayList<Integer>();
list.add(2);
list.add(9);
List<Student> lists = mapper.findByForeachList(list);
for (Student item:lists){
System.out.println(item.getName());
}
session.commit();
session.close();
}
/**
* 智能標簽foreach-list自定義類型
*/
@Test
public void findByForeachListStudent(){
SqlSession session = MyBatisUtil.getSession();
IStudentDAO mapper = session.getMapper(IStudentDAO.class);
List<Student> list=new ArrayList<Student>();
Student student=new Student();
student.setId(2);
Student students=new Student();
students.setId(9);
list.add(student);
list.add(students);
List<Student> lists = mapper.findByForeachListStudent(list);
for (Student item:lists){
System.out.println(item.getName());
}
session.commit();
session.close();
}
?
轉載于:https://www.cnblogs.com/sujulin/p/7588772.html
總結
以上是生活随笔為你收集整理的mybatis中的智能标签之二的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU1559
- 下一篇: Oracle表空间状态