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

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

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

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

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

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

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

<?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>

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

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集合取值和使用索引号

(一)用map集合获取值

  1在接口IStudentDAO定制一个方法

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

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

 <!-- 多条件查询-->
<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>

3在测试类中

/**
* 多条件查询
* @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());
} }

(二)使用索引

1在接口IStudentDAO定制一个方法

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

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

<select id="findStudentByCondition" resultType="Student">
<!-- mysql数据库
select * from student where stuname like '%' #{} '%' and stuage>#{}
orcl数据库 -->
select * from student where stuname like '%'||#{}||'%' and stuage>#{} </select>

3在测试类中

@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());
} }

  小总结:

      

四、智能标签

他们共同用到的是如下

    1定义一个方法在接口中

 public List<Student> findStudentByif(Student stu);

   2测试类

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());
} }

  3在小配置中的配置

if标签

<!-- 多条件查询 -->
<select id="findStudentif" resultType="Student">
select * from student where = <if test="stuname!=null">
and stuname like '%'||#{stuname}||'%'
</if>
<if test="stuage!=null">
and stuage>#{stuage}
</if> </select>

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

<!-- 多条件查询 -->
<select id="findStudentBychoose" resultType="Student">
select * from student <!-- where =1如果有where标签就不需要 -->
<where>
<if test="stuname!=null">
and stuname like '%'||#{stuname}||'%'
</if>
<if test="stuage!=null">
and stuage>#{stuage}
</if>
</where>
</select>

choose标签

    <!--多条件查询where =  如果有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>

foreach标签

(一)数组

  1在接口中

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

  2在小配置中配置

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

  3在测试类

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

(二)自定义泛型

  1在接口中

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

  2在小配置中配置

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

  3在测试类中

//自定义泛型
@Test
public void findByListGeneric() throws IOException{
List<Student> rlist=new ArrayList<Student>();
Student stu1=new Student();
stu1.setStuno(3); Student stu2=new Student();
stu2.setStuno(11); rlist.add(stu2);
rlist.add(stu1);
List<Student> findByListGeneric = dao.findByListGeneric(rlist);
for (Student student : findByListGeneric) {
System.out.println(student.getStuname());
}

(三)List集合

  1在接口

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

  2在小配置中配置

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

  3在测试类

//list集合
@Test
public void findByList() throws IOException{
List<Integer> rlist=new ArrayList<Integer>();
rlist.add(3);
rlist.add(11);
List<Student> findByList = dao.findByList(rlist);
for (Student student : findByList) {
System.out.println(student.getStuname());
} }

五、sql片段

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

<!-- sql片段 -->
<sql id="sqlclum" >
select stuno,stuname,stuage,studate
</sql>
<!-- list集合 -->
<select id="findByList" resultType="Student">
<!-- select * from student --><!-- where =1如果有where标签就不需要 -->
<include refid="sqlclum"/> from student
<if test="list.size>0">
where stuno in
<foreach collection="list" open="(" close=")" separator=","
item="stulist">
#{stulist}
</foreach>
</if> </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. Android studio使用gradle动态构建APP(不同的包,不同的icon、label)

    最近有个需求,需要做两个功能相似的APP,大部分代码是一样的,只是界面不一样,以前要维护两套代码,比较麻烦,最近在网上找资料,发现可以用gradle使用同一套代码构建两个APP.下面介绍使用方法: 首 ...

  2. 敏捷转型历程 - Sprint3 一团糟的演示会

    我: Tech Leader 团队:团队成员分布在两个城市,我所在的城市包括我有4个成员,另外一个城市包括SM有7个成员.另外由于我们的BA离职了,我暂代IT 的PO 职位.PM和我在一个城市,但他不 ...

  3. Linux下用netstat查看网络状态、端口状态(转)

    转:http://blog.csdn.net/guodongdongnumber1/article/details/11383019 在linux一般使用netstat 来查看系统端口使用情况步.  ...

  4. linux字符串url编码与解码

    编码的两种方式 echo '手机' | tr -d '\n' | xxd -plain | sed 's/\(..\)/%\1/g' echo '手机' |tr -d '\n' |od -An -tx ...

  5. Linux.NET实战手记—自己动手改泥鳅(上)

    各位读者大家好,不知各位读者有否阅读在下的前一个系列<Linux.NET 学习手记>,在前一个系列中,我们从Linux中Mono的编译安装开始,到Jexus服务器的介绍,以及如何在Linu ...

  6. C#跨平台手机应用开发工具Xamarin尝试 与Eclipse简单对比

    Xamarin 支持使用C#开发基于Android.IOS.WindowsPhone应用开发,最大特点C#+跨平台,详细说明问度娘. 安装 研究 想体验研究的点击查看页面 Xamarin For Vi ...

  7. 探索c#之Async、Await剖析

    阅读目录: 基本介绍 基本原理剖析 内部实现剖析 重点注意的地方 总结 基本介绍 Async.Await是net4.x新增的异步编程方式,其目的是为了简化异步程序编写,和之前APM方式简单对比如下. ...

  8. 如何选择靠谱的app外包公司,2017最新资讯

    企业在外包开发app之前最纠结的问题就是如何选择一个靠谱的app外包公司.因为一个靠谱的app外包公司非常重要,完全决定了app外包开发的周期.bug.售后服务等各方面问题,可以说app开发外包团队一 ...

  9. jQuery +ajax +json+实现分页

    正文 首先我们创建一般处理程序,来读取数据库中内容,得到返回值. 创建文件,GetData.ashx. 我这里是用的存储过程,存储过程会再下面粘出来,至于数据只是实例,你们可根据需求自行读取数据 代码 ...

  10. Atitit. 破解  拦截 绕过 网站 手机 短信 验证码  方式 v2 attilax 总结

    Atitit. 破解  拦截 绕过 网站 手机 短信 验证码  方式 v2 attilax 总结 1. 验证码的前世今生11.1. 第一代验证码 图片验证码11.2. 第二代验证码  用户操作 ,比如 ...