详解MyBatis的getMapper()接口、resultMap标签、Alias别名、 尽量提取sql列、动态操作

所属分类: 软件编程 / java 阅读数: 41
收藏 0 赞 0 分享

一、getMapper()接口

  解析:getMapper()接口 IDept.class定义一个接口,

     挂载一个没有实现的方法,特殊之处,借楼任何方法,必须和小配置中id属性是一致的

     通过代理:生成接口的实现类名称,在MyBatis底层维护名称$$Dept_abc,selectDeptByNo()

     相当于是一个强类型

Eg

  第一步:在cn.happy.dao中定义一个接口   

package cn.happy.dao;
import java.util.List;
import cn.happy.entity.Dept;
public interface IDeptDao {
//查看全部---------getAllDept要和小配置里面的id一样
public List<Dept> getAllDept();
}

  第二步:IDept.xml配置小配置

  解析:select里面的Id属性要和接口里面的接口方法名一样;mapper的namespace属性包名是cn.happy.dao.IDeptDao接口

<?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.IDeptDao">
<select id="getAllDept" resultType="cn.happy.entity.Dept">
select * from Dept 
</select>
</mapper>

  第三步:测试类

  解析:查看全部信息有两种方法

     1)session.selectList("cn.happy.dao.IDeptDao.getAllDept");-------实体类.小配置里面的Id名称============字符串

     2)IDeptDao mapper = session.getMapper(IDeptDao.class);相当于实现类,getMapper是一个强类型

// 01查看全部信息getMapper()接口类的方法名要和小配置的id一样
@Test
public void testSelectAll() {
SqlSession session = factory.openSession();
//用的是弱类型========实体类.小配置里面的Id名称============字符串
/*List<Dept> list = session.selectList("cn.happy.dao.IDeptDao.getAllDept");
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}*/
// 用getMapper方法HIbernate帮我们在内存中代理出一个接口的实现类======相当于强类型
//mapper是一个实现类对象
IDeptDao mapper = session.getMapper(IDeptDao.class);
List<Dept> list = mapper.getAllDept();
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}

  第四步:全文统一用一个大配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- Alias别名 小配置里面的type的属性值改成别名-->
<typeAliases>
<typeAlias type="cn.resultMap.enetity.Emp" alias="emp"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="sa" />
<property name="password" value="1" />
</dataSource>
</environment>
</environments>
<!--映射文件:描述某个实体和数据库表的对应关系 -->
<mappers>
<mapper resource="cn/resultMap/enetity/Emp.xml" />
</mappers>
</configuration>

二、resultMap标签

    解析:使用的场景是当实体类的属性与数据库不匹配的时候需要用到resultMap实体类和数据库的属性必须一致。(之前用的是实体类)

Eg检索所有员工,以及隶属部门

  第一步:创建一个接口

package cn.resultMap.dao;
import java.util.List;
import cn.resultMap.enetity.Emp;
public interface IEmpDao {
//检索所有员工,以及隶属部门
public List<Emp> getAllEmps();
}

   第二步:配置小配置里面的属性

  解析: 员工角度 多的一方,嵌入一的一方的各个属性请使用association 是关联(如果去掉association的话就是基础的resultMap)

<?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.resultMap.dao.IEmpDao">
<resultMap type="cn.resultMap.enetity.Emp" id="empMap">
<id property="empId" column="EMPID"/>
<result property="empName" column="EMPNAME"/>
<result property="empCity" column="EMPCITY"/>
<!-- 员工角度 多的一方,嵌入一的一方的各个属性请使用association -->
<association property="dept" javaType="cn.resultMap.enetity.Dept">
<result property="deptName" column="DEPTNAME"/>
<result property="deptNo" column="DEPTNO"/>
</association>
</resultMap>
<select id="getAllEmps" resultMap="empMap">
select e.*,d.* from Emp e,Dept d
where e.deptNo=d.deptNo
</select>
</mapper>

第三步:测试类

//resultMap:实体的属性名和表的字段名保证一致用resultMap
//如果报NullException查看小配置的映射关联resultMap是否配置
@Test
public void testAllEmp(){
SqlSession session=factory.openSession();
IEmpDao mapper = session.getMapper(IEmpDao.class);
List<Emp> allEmps = mapper.getAllEmps();
for (Emp emp : allEmps) {
System.out.println(emp.getEmpName()+"\t隶属部门"+emp.getDept().getDeptName());
}
session.close();
}

第四步:在大配置引入小配置

三、提取sql列

  解析:Sql标签简化代码量在小配置里面写

<!-- SQl标签的使用 -->
<sql id="columns">
d.deptNo,d.deptName
</sql>
<!-- SQl标签的使用 -->
<select id="getAllEmps" resultMap="empMap">
select e.*,<include refid="columns"/>from Emp e,Dept d
where e.deptNo=d.deptNo
</select>

四、Alias别名

    解析:在大配置上写,这样的话在小配置就可以引用别名了  

<!-- Alias别名 小配置里面的type的属性值改成别名-->
<typeAliases>
<typeAlias type="cn.resultMap.enetity.Emp" alias="emp"/>
</typeAliases>

五、动态操作

解析:用于实现动态SQL的元素主要有:

 if
    choose(when,otherwise)
    where 
    set 

Eg  查看在北京城市的人员

  第一步:接口

package cn.resultMap.dao;
import java.util.List;
import cn.resultMap.enetity.Emp;
public interface IEmpDao {
//检索所有员工,以及隶属部门
public List<Emp> getAllEmps();
}

  第二步:小配<?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.resultMap.dao.IEmpDao">
<resultMap type="cn.resultMap.enetity.Emp" id="empMap">
<id property="empId" column="EMPID"/>
<result property="empName" column="EMPNAME"/>
<result property="empCity" column="EMPCITY"/>
<!-- 员工角度 多的一方,嵌入一的一方的各个属性请使用association -->
<association property="dept" javaType="cn.resultMap.enetity.Dept">
<result property="deptName" column="DEPTNAME"/>
<result property="deptNo" column="DEPTNO"/>
</association>
</resultMap>
<select id="getAllEmps" resultMap="empMap">
select e.*,d.* from Emp e,Dept d
where e.deptNo=d.deptNo
</select>
<!--查询动态查询 -->
<select id="testAllEmpBuSelect" parameterType="cn.resultMap.enetity.Emp" resultType="cn.resultMap.enetity.Emp">
select * from Emp
<where>
<if test="empId!=null">
and empId=#{empId}
</if>
<if test="empName!=null">
and empName=#{empName}
</if>
<if test="empCity!=null">
and empCity=#{empCity}
</if>
</where>
</select>
</mapper>

第三步:测试

//动态查询
@Test
public void testSelect(){
SqlSession session=factory.openSession();
Emp emp=new Emp();
//emp.setEmpName("331");
emp.setEmpCity("sh");
List<Emp> list = session.selectList("cn.resultMap.dao.IEmpDao.testAllEmpBuSelect",emp);
for (Emp emps : list) {
System.out.println(emps.getEmpName());
}
session.close();
}

第四步:在大配置引入小配置

Eg    修改部门信息

  第一步:接口

  第二步:小配置

<?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.resultMap.dao.IDeptDao">
<resultMap type="cn.happy.entity.Dept" id="deptResultMap">
<id property="deptNo" column="deptNo"/>
<result property="deptName" column="deptName"/>
</resultMap>
<select id="getAllDept" resultMap="deptResultMap">
select d.*,e.* from Dept d,Emp e
where d.deptNo=e.deptNo and d.deptNo=#{deptNo}
</select>
<!--修改动态查询 -->
<select id="testUpdate" parameterType="int" resultType="cn.resultMap.enetity.Dept">
update dept
<set>
<if test="deptNo!=null">
deptNo=#{deptNo},
</if>
<if test="deptName!=null">
deptName=#{deptName},
</if>
</set>
where deptNo=#{deptNo}
</select>
</mapper> 

  第三步:测试 

/**
* 动态修改
* */
@Test
public void testUpdate(){
SqlSession session=factory.openSession();
Dept dept=new Dept();
dept.setDeptName("财务部");
dept.setDeptNo(1);
int count = session.update("cn.resultMap.dao.IDeptDao.testUpdate",dept);
session.commit();
System.out.println(count);
session.close();
}

以上所述是小编给大家介绍的详解MyBatis的getMapper()接口、resultMap标签、Alias别名、 尽量提取sql列、动态操作,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

Java的面向对象编程基本概念学习笔记整理

这篇文章主要介绍了Java的面向对象编程基本概念学习笔记整理,包括类与方法以及多态等支持面向对象语言中的重要特点,需要的朋友可以参考下
收藏 0 赞 0 分享

Eclipse下编写java程序突然不会自动生成R.java文件和包的解决办法

这篇文章主要介绍了Eclipse下编写java程序突然不会自动生成R.java文件和包的解决办法 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

基于Java实现杨辉三角 LeetCode Pascal's Triangle

这篇文章主要介绍了基于Java实现杨辉三角 LeetCode Pascal's Triangle的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中Spring获取bean方法小结

Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中获取Spring配置的bean呢?下面通过本文给大家介绍Java中Spring获取bean方法小结,对spring获取bean方法相关知识感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

如何计算Java对象占用了多少空间?

在Java中没有sizeof运算符,所以没办法知道一个对象到底占用了多大的空间,但是在分配对象的时候会有一些基本的规则,我们根据这些规则大致能判断出来对象大小,需要的朋友可以参考下
收藏 0 赞 0 分享

剖析Java中的事件处理与异常处理机制

这篇文章主要介绍了Java中的事件处理与异常处理机制,讲解Java是如何对事件或者异常作出响应以及定义异常的一些方法,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Java的Struts2框架的结构及其数据转移方式

这篇文章主要介绍了详解Java的Struts2框架的结构及其数据转移方式,Struts框架是Java的SSH三大web开发框架之一,需要的朋友可以参考下
收藏 0 赞 0 分享

Java封装好的mail包发送电子邮件的类

本文给大家分享了2个java封装好的mail包发送电子邮件的类,并附上使用方法,小伙伴们可以根据自己的需求自由选择。
收藏 0 赞 0 分享

在Java的Struts中判断是否调用AJAX及用拦截器对其优化

这篇文章主要介绍了在Java的Struts中判断是否调用AJAX及用拦截器对其优化的方法,Struts框架是Java的SSH三大web开发框架之一,需要的朋友可以参考下
收藏 0 赞 0 分享

java多线程Future和Callable类示例分享

JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都没有返回值,只有最后一种是带返回值的。今天我们就来研究下Future和Callab
收藏 0 赞 0 分享
查看更多