标签:1.0 技术分享 http map 定制 com result 配置 hash
<!--方案一:resultMapper 字段名与属性名不一致 -->
<resultMap type="Student" id="StudentMapper"> <result column="stuname2" property="stuname"/> </resultMap> <!-- 查询所有 --> <select id="findAll" resultMap="StudentMapper"> select * from student </select>
<!-- 方案二:as别名的方式 --> <select id="findAll" resultType="Student"> select stuname2 as stuname from student </select>
<?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="cn.happy.dao.IStudentDAO"> <!-- 方案二:as别名的方式 --> <select id="findAll" resultType="Student"> select stuname2 as stuname from student </select> </mapper>
public class MyTest { IStudentDAO dao; @Before public void initData() throws IOException{ SqlSession session = MybatisUtil.getSession(); //动态踢出实现类 //首先要改StudentDAO.xml改成<mapper namespace="cn.happy.dao.IStudentDAO"> dao=session.getMapper(IStudentDAO.class); } /** * selectALl学生 * @throws IOException */ @Test public void findAll() throws IOException{ List<Student> list = dao.findAll(); for (Student student : list) { System.out.println(student.getStuname()); /*System.out.println(student.getStuage());*/ } } }
//多条件查询封装成map public List<Student> findStudentMore(Map<String, Object> map);
<!-- 多条件查询--> <select id="findStudentMore" resultType="Student"> <!-- mysql数据库 --> <!-- select * from student where stuname like ‘%‘ #{stuname} ‘%‘ and stuage>#{stuage} --> <!-- orcl数据库 --> select * from student where stuname like ‘%‘||#{stuname}||‘%‘ and stuage>#{stuage} </select>
/** * 多条件查询 * @throws IOException */ @Test public void findStudentMore(){ Map<String, Object> maplist=new HashMap<String, Object>(); maplist.put("stuname", "123"); maplist.put("stuage", 11); List<Student> list = dao.findStudentMore(maplist); for (Student student : list) { System.out.println(student.getStuname()); } }
//多条件查询引号 public List<Student> findStudentByCondition(String name,int age);
<select id="findStudentByCondition" resultType="Student"> <!-- mysql数据库 select * from student where stuname like ‘%‘ #{0} ‘%‘ and stuage>#{1} orcl数据库 --> select * from student where stuname like ‘%‘||#{0}||‘%‘ and stuage>#{1} </select>
@Test public void findStudentByCondition() throws IOException{ String name="人"; int age=12; List<Student> list = dao.findStudentByCondition(name,age); for (Student student : list) { System.out.println(student.getStuname()); } }
public List<Student> findStudentByif(Student stu);
public class MyTest { IStudentDAO dao; @Before public void initData() throws IOException{ SqlSession session = MybatisUtil.getSession(); //动态踢出实现类 //首先要改StudentDAO.xml改成<mapper namespace="cn.happy.dao.IStudentDAO"> dao=session.getMapper(IStudentDAO.class); } /** * 多条件查询 * @throws IOException */ @Test public void findStudentByCondition() throws IOException{ String name="人"; int age=12; Student stu=new Student(); stu.setStuage(age); stu.setStuname(name); List<Student> list = dao.findStudentByif(stu); for (Student student : list) { System.out.println(student.getStuname()); } }
<!-- 多条件查询 --> <select id="findStudentif" resultType="Student"> select * from student where 1=1 <if test="stuname!=null"> and stuname like ‘%‘||#{stuname}||‘%‘ </if> <if test="stuage!=null"> and stuage>#{stuage} </if> </select>
<!-- 多条件查询 --> <select id="findStudentBychoose" resultType="Student"> select * from student <!-- where 1=1如果有where标签就不需要 --> <where> <if test="stuname!=null"> and stuname like ‘%‘||#{stuname}||‘%‘ </if> <if test="stuage!=null"> and stuage>#{stuage} </if> </where> </select>
<!--多条件查询where 1=1 如果有where标签就不需要--> <select id="findStudentByif" resultType="Student"> select * from student <where> <choose> <when test="stuname!=null"> and stuname like ‘%‘||#{stuname}||‘%‘ </when> <otherwise> </otherwise> </choose> </where> </select>
MyBatis的一系列问题的处理(遍历Map集合和智能标签和属性和字段不一样的解决办法)
标签:1.0 技术分享 http map 定制 com result 配置 hash
原文地址:http://www.cnblogs.com/yejiaojiao/p/6189699.html