一、字段名与属性名(数据库的名字)不一样怎么办?

  方案一:在小配置中配置一个resultMapper

  1. <!--方案一:resultMapper 字段名与属性名不一致 -->
  1. <resultMap type="Student" id="StudentMapper">
  2. <result column="stuname2" property="stuname"/>
  3. </resultMap>
  4.  
  5. <!-- 查询所有 -->
  6. <select id="findAll" resultMap="StudentMapper">
  7. select * from student
  8. </select>

  方案二:在小配置中的查询语句用as

  1. <!-- 方案二:as别名的方式 -->
  2. <select id="findAll" resultType="Student">
  3. select stuname2 as stuname from student
  4. </select>

二、Mapper动态代理剔除实现类

第一步改动的地方是小配置的<mapper namespace="cn.happy.dao.IStudentDAO">写到接口

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="cn.happy.dao.IStudentDAO">
  6.  
  7. <!-- 方案二:as别名的方式 -->
  8. <select id="findAll" resultType="Student">
  9. select stuname2 as stuname from student
  10. </select>
  11.  
  12. </mapper>

第二步是在测试类调用接口的时候用getMapper获取一个接口实现类

  1. public class MyTest {
  2. IStudentDAO dao;
  3. @Before
  4. public void initData() throws IOException{
  5. SqlSession session = MybatisUtil.getSession();
  6. //动态踢出实现类
  7. //首先要改StudentDAO.xml改成<mapper namespace="cn.happy.dao.IStudentDAO">
  8. dao=session.getMapper(IStudentDAO.class);
  9. }
  10.  
  11. /**
  12. * selectALl学生
  13. * @throws IOException
  14. */
  15.  
  16. @Test
  17. public void findAll() throws IOException{
  18. List<Student> list = dao.findAll();
  19. for (Student student : list) {
  20. System.out.println(student.getStuname());
  21. /*System.out.println(student.getStuage());*/
  22. }
  23.  
  24. }
  25. }

三、用Map集合取值和使用索引号

(一)用map集合获取值

  1在接口IStudentDAO定制一个方法

  1. //多条件查询封装成map
  2. public List<Student> findStudentMore(Map<String, Object> map);

2在小配置中id=“findStudentMore”要和接口的方法法名要一样

  1. <!-- 多条件查询-->
  2. <select id="findStudentMore" resultType="Student">
  3. <!-- mysql数据库 -->
  4. <!-- select * from student where stuname like '%' #{stuname} '%' and stuage>#{stuage} -->
  5. <!-- orcl数据库 -->
  6. select * from student where stuname like '%'||#{stuname}||'%' and stuage>#{stuage}
  7. </select>

3在测试类中

  1. /**
  2. * 多条件查询
  3. * @throws IOException
  4. */
  5.  
  6. @Test
  7. public void findStudentMore(){
  8.  
  9. Map<String, Object> maplist=new HashMap<String, Object>();
  10. maplist.put("stuname", "123");
  11. maplist.put("stuage", 11);
  12. List<Student> list = dao.findStudentMore(maplist);
  13. for (Student student : list) {
  14. System.out.println(student.getStuname());
  15. }
  16.  
  17. }

(二)使用索引

1在接口IStudentDAO定制一个方法

  1. //多条件查询引号
  2. public List<Student> findStudentByCondition(String name,int age);

2在小配置中id=“findStudentMore”要和接口的方法法名要一样

  1. <select id="findStudentByCondition" resultType="Student">
  2. <!-- mysql数据库
  3. select * from student where stuname like '%' #{} '%' and stuage>#{}
  4. orcl数据库 -->
  5. select * from student where stuname like '%'||#{}||'%' and stuage>#{}
  6.  
  7. </select>

3在测试类中

  1. @Test
  2. public void findStudentByCondition() throws IOException{
  3. String name="人";
  4. int age=12;
  5. List<Student> list = dao.findStudentByCondition(name,age);
  6. for (Student student : list) {
  7. System.out.println(student.getStuname());
  8. }
  9.  
  10. }

  小总结:

      

四、智能标签

他们共同用到的是如下

    1定义一个方法在接口中

  1. public List<Student> findStudentByif(Student stu);

   2测试类

  1. public class MyTest {
  2. IStudentDAO dao;
  3. @Before
  4. public void initData() throws IOException{
  5. SqlSession session = MybatisUtil.getSession();
  6. //动态踢出实现类
  7. //首先要改StudentDAO.xml改成<mapper namespace="cn.happy.dao.IStudentDAO">
  8. dao=session.getMapper(IStudentDAO.class);
  9. }
  10.  
  11. /**
  12. * 多条件查询
  13. * @throws IOException
  14. */
  15.  
  16. @Test
  17. public void findStudentByCondition() throws IOException{
  18. String name="人";
  19. int age=12;
  20. Student stu=new Student();
  21. stu.setStuage(age);
  22. stu.setStuname(name);
  23. List<Student> list = dao.findStudentByif(stu);
  24. for (Student student : list) {
  25. System.out.println(student.getStuname());
  26. }
  27.  
  28. }

  3在小配置中的配置

if标签

  1. <!-- 多条件查询 -->
  2. <select id="findStudentif" resultType="Student">
  3. select * from student where =
  4.  
  5. <if test="stuname!=null">
  6. and stuname like '%'||#{stuname}||'%'
  7. </if>
  8. <if test="stuage!=null">
  9. and stuage>#{stuage}
  10. </if>
  11.  
  12. </select>

where标签     注意如果有<where>标签就不需要where 1=1

  1. <!-- 多条件查询 -->
  2. <select id="findStudentBychoose" resultType="Student">
  3. select * from student <!-- where =1如果有where标签就不需要 -->
  4. <where>
  5. <if test="stuname!=null">
  6. and stuname like '%'||#{stuname}||'%'
  7. </if>
  8. <if test="stuage!=null">
  9. and stuage>#{stuage}
  10. </if>
  11. </where>
  12. </select>

choose标签

  1. <!--多条件查询where = 如果有where标签就不需要-->
  2. <select id="findStudentByif" resultType="Student">
  3. select * from student
  4. <where>
  5. <choose>
  6. <when test="stuname!=null">
  7. and stuname like '%'||#{stuname}||'%'
  8.  
  9. </when>
  10. <otherwise>
  11. </otherwise>
  12. </choose>
  13.  
  14. </where>
  15. </select>

foreach标签

(一)数组

  1在接口中

  1. //查询是一个3,11的stuno标号 ids Araay数组
  2. public List<Student> findByArray(int[] ids);

  2在小配置中配置

  1. <!-- array数组 -->
  2. <select id="findByArray" resultType="Student">
  3. select * from student <!-- where =1如果有where标签就不需要 -->
  4. <if test="array.length>0">
  5. where stuno in
  6.  
  7.         <!-- item是自定义的 -->
  8. <foreach collection="array" open="(" close=")" separator="," item="myid">
  9. #{myid}
  10. </foreach>
  11. </if>
  12.  
  13. </select>

  3在测试类

  1. //数组
  2. @Test
  3. public void findStudentBychoose() throws IOException{
  4. int[] ids={3,11};//自定义数组
  5. List<Student> findByArray = dao.findByArray(ids);
  6. for (Student student : findByArray) {
  7. System.out.println(student.getStuname());
  8. }
  9.  
  10. }

(二)自定义泛型

  1在接口中

  1. //查询3,11 ids List自定义泛型
  2. public List<Student> findByListGeneric(List<Student> list);

  2在小配置中配置

  1. <!-- list集合 -->
  2. <select id="findByListGeneric" resultType="Student">
  3. select * from student <!-- where =1如果有where标签就不需要 -->
  4. <if test="list.size>0">
  5. where stuno in
  6. <foreach collection="list" open="(" close=")" separator="," item="stu">
  7. #{stu.stuno}
  8. </foreach>
  9. </if>
  10.  
  11. </select>

  3在测试类中

  1. //自定义泛型
  2. @Test
  3. public void findByListGeneric() throws IOException{
  4. List<Student> rlist=new ArrayList<Student>();
  5. Student stu1=new Student();
  6. stu1.setStuno(3);
  7.  
  8. Student stu2=new Student();
  9. stu2.setStuno(11);
  10.  
  11. rlist.add(stu2);
  12. rlist.add(stu1);
  13. List<Student> findByListGeneric = dao.findByListGeneric(rlist);
  14. for (Student student : findByListGeneric) {
  15. System.out.println(student.getStuname());
  16. }

(三)List集合

  1在接口

  1. //查询3,11 ids List
  2. public List<Student> findByList(List<Integer> list);

  2在小配置中配置

  1. <select id="findByList" resultType="Student">
  2. select * from student <!-- where =1如果有where标签就不需要 -->
  3. <if test="list.size>0">
  4. where stuno in
  5. <foreach collection="list" open="(" close=")" separator="," item="stulist">
  6. #{stulist}
  7. </foreach>
  8. </if>
  9.  
  10. </select>

  3在测试类

  1. //list集合
  2. @Test
  3. public void findByList() throws IOException{
  4. List<Integer> rlist=new ArrayList<Integer>();
  5. rlist.add(3);
  6. rlist.add(11);
  7. List<Student> findByList = dao.findByList(rlist);
  8. for (Student student : findByList) {
  9. System.out.println(student.getStuname());
  10. }
  11.  
  12. }

五、sql片段

  1连接上一个foreach智能标签在小配置中(用include标签)

  1. <!-- sql片段 -->
  2. <sql id="sqlclum" >
  3. select stuno,stuname,stuage,studate
  4. </sql>
  1. <!-- list集合 -->
  2. <select id="findByList" resultType="Student">
  3. <!-- select * from student --><!-- where =1如果有where标签就不需要 -->
  4. <include refid="sqlclum"/> from student
  5. <if test="list.size>0">
  6. where stuno in
  7. <foreach collection="list" open="(" close=")" separator=","
  8. item="stulist">
  9. #{stulist}
  10. </foreach>
  11. </if>
  12.  
  13. </select>

MyBatis的一系列问题的处理(遍历Map集合和智能标签和属性和字段不一样的解决办法 和sql片段)(三)的更多相关文章

  1. 遍历Map集合:java.util.Map.Entry、KeySet两种方式

    遍历Map集合的两种方式: 1.用KeySet Map.keySet(),返回一个存放所有key的set集合,通过遍历集合,根据key值取出所有的value值. Map<String,Strin ...

  2. 键盘录入一个文件夹路径,统计该文件夹(包含子文件夹)中每种类型的文件及个数,注意:用文件类型(后缀名,不包含.(点),如:"java","txt")作为key, 用个数作为value,放入到map集合中,遍历map集合

    package cn.it.zuoye5; import java.io.File;import java.util.HashMap;import java.util.Iterator;import ...

  3. (1)集合 ---遍历map集合

    Map接口     实现Map接口的类用来存储键(key)-值(value) 对.Map 接口的实现类有HashMap和TreeMap等.Map类中存储的键-值对通过键来标识,所以键值不能重复. Ha ...

  4. 遍历Map集合的几种方式

    import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entr ...

  5. 用来遍历map集合的方法

    map集合是以键值对进行存储值的,所以遍历map集合无非就是获取键和值,根据实际需求,进行获取键和值. 1.无非就是通过map.keySet()获取到值,然后根据键获取到值. for(String s ...

  6. Java之五种遍历Map集合的方式

    摘要:在java中所有的map都实现了Map接口,因此所有的Map都可以用以下的方式去遍历. 在java中所有的map都实现了Map接口,因此所有的Map都可以用以下的方式去遍历.这篇文章主要给大家介 ...

  7. Java中遍历Map集合的四种方法

    在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...

  8. struts2标签 遍历map集合

    首先我们来构造几个map集合.    假设如下代码 都是在ssh配置环境下搭建好,(至少struts2开发环境搭建好) (1).java 代码          下面的student对象包含的字段为 ...

  9. 遍历Map集合的四中方法

    ->有这样一个Map集合 Map<String, String> map = new HashMap<String, String>(); map.put(", ...

随机推荐

  1. [Top-Down Approach]Take Notes

    Computer Networking - A Top-Down Approach Six Edition Learn HTTP Using Browser and Proxy 2016-03-20 ...

  2. js月份,日期加一天

    js没有直接可以用的函数,所以只能自己写,其中需要涉及到每个月天数的判断,如果是2月份的话,还要涉及到闰年的判断 var addDate = { //日期,在原有日期基础上,增加days天数,默认增加 ...

  3. centos6.X使用Apache+Mono搭建asp.net 环境

    mark 一下时间  2016年1月19日09:42:49 mono是指由Novell公司(由Xamarin发起,并由Miguel de lcaza领导的,一个致力于开创·NET在Linux上使用的开 ...

  4. Vue.js——60分钟browserify项目模板快速入门

    概述 在之前的一系列vue.js文章,我们都是用传统模式引用vue.js以及其他的js文件的,这在开发时会产生一些问题. 首先,这限定了我们的开发模式是基于页面的,而不是基于组件的,组件的所有代码都直 ...

  5. [DeviceOne开发]-手势动画示例分享

    一.简介 这是iOS下的效果,android下完全一致.通过do_GestureView组件和do_Animation组件,deviceone能很容易实现复杂的跨平台纯原生动画效果,这个示例就是通过手 ...

  6. Fedora 24中的日志管理

    Introduction Log files are files that contain messages about the system, including the kernel, servi ...

  7. IOCP Internals

    Buffer Type Buffer I/O 针对Buffer I/O的请求,系统会为其分配一个非换页内存作为缓存区,其大小等同于I/O请求的缓存区大小.对于写操作,I/O管理器在创建IRP时,将请求 ...

  8. Speedment -- 利用lambda编写SQL

    众所周知Java8中加入了lambda语法,这一特性也帮助Java开发者极大的简化了开发.Speedment是一个利用lambda表达式操作数据库的框架,相比Java世界中现在非常流行的mybatis ...

  9. C# 线程同步的三类情景

    C# 已经提供了我们几种非常好用的类库如 BackgroundWorker.Thread.Task等,借助它们,我们就能够分分钟编写出一个多线程的应用程序. 比如这样一个需求:有一个 Winform ...

  10. ABP框架 - 数据过滤

    文档目录 本节内容: 简介 预定义过滤 ISoftDelete 何时可用? IMustHaveTenant 何时可用? IMayHaveTenant 何时可用? 禁用过滤 关于using声明 关于多租 ...