一、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="" />
</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();
int count = session.update("cn.resultMap.dao.IDeptDao.testUpdate",dept);
session.commit();
System.out.println(count);
session.close(); }

  第四步:大配置

      分享MyBatis!!!!!只是一部分哦!!!!!

MyBatis的getMapper()接口、resultMap标签、Alias别名、 尽量提取sql列、动态操作的更多相关文章

  1. mybatis学习 十四 resultMap标签 一对一(联合查询)

    1.使用 resultMap 实现关联单个对象(联合查询方式) <resultMap type="Student" id="stuMap1"> &l ...

  2. mybatis学习 十五 resultMap标签 一对多

    多次查询,非联合查询版本 <resultMap type="teacher" id="techMap"> <id column="i ...

  3. MyBatis之ResultMap标签

    ResultMap标签基本作用:建立SQL查询结果字段与实体属性的映射关系信息 在深入ResultMap标签前,我们需要了解从SQL查询结果集到JavaBean或POJO实体的过程. 1. 通过JDB ...

  4. Mybatis中parameterType、resultMap、statementType等等配置详解(标签中基本配置详解)

    一.(转自:https://blog.csdn.net/majinggogogo/article/details/72123185) 映射文件是以<mapper>作为根节点,在根节点中支持 ...

  5. mybatis学习 十三 resultMap标签 一对一

    1 .<resultMap>标签 写在mapper.xml中,由程序员控制SQL查询结果与实体类的映射关系. 在写<select>标签中,有一个resultType属性,此时s ...

  6. mybatis配置文件resultMap标签的使用

    本文为博主原创,未经允许不得转载: resultMap标签是为了映射select查询出来结果的集合,其主要作用是将实体类中的字段与 数据库表中的字段进行关联映射. 注意:当实体类中的字段与数据库表中的 ...

  7. mybatis resultmap标签type属性什么意思

    mybatis resultmap标签type属性什么意思? :就表示被转换的对象啊,被转换成object的类型啊 <resultMap id="BaseResultMap" ...

  8. Mybatis 高级结果映射 ResultMap Association Collection

    在阅读本文章时,先说几个mybatis中容易混淆的地方: 1. mybatis中的列不是数据库里的列而是查询里的列,可以是别名(如 select user_name as userName,这时col ...

  9. Mybatis核心配置文件中的标签介绍

    0. 标签顺序 Mybatis核心配置文件中有很多标签,它们谁谁写在前写在后其实是有顺序要求的: 从前到后: properties?,settings?,typeAliases?,typeHandle ...

随机推荐

  1. 获取bing.com的图片并在gnome3中设置自动切换

    发现 bing.com 上的图片很好看,因此打算每天把 bing.com 的图片下载下来,用作桌面. 需要做的是两个部分,爬取图片到目录和设置目录图片为桌面背景并可以自动切换. 第一部分,下载图片,使 ...

  2. Red Gate(SQLToolbelt)SQL Server的安装与注册(破解)

    Red Gate(SQLToolbelt)是SQL Server辅佐工具 1.SQL Compare 比较和同步SQL Server数据库结构 2.SQL Data Compare 比较和同步SQL ...

  3. Apk去掉签名以及重新签名的方法

    Android开发中很重要的一部就是用自己的密钥给Apk文件签名,不经过签名的Apk文件一般是无法安装的,就算装了最后也是失败. 网上流传的"勾选允许安装未知来源的应用"其实跟签不 ...

  4. fix orphaned user

    orphan user是某个数据库的user,只有user name而没有login,即,在存在于sys.database_principals 中, 而不存在于 sys.server_princip ...

  5. SSIS Execute SQL Task 用法

    Execute Sql Task组件是一个非常有用的Control Flow Task,可以直接执行SQL语句,例如,可以执行数据更新命令(update,delete,insert),也可以执行sel ...

  6. VMware 中如何打开U盘弹出U盘或者移动硬盘的(两种方法)

    1.U盘如下,插入后都是直接在win里面显示的 2.选择连接u盘 3.u盘就可以在虚拟机里面显示了 4.弹出则选择断开连接 扩展:如果无效:请参考这种方法 (给虚拟机分配一个临时硬盘,然后设置这个临时 ...

  7. Sql Server系列:日期和时间函数

    1. 获取系统当前日期函数GETDATE() GETDATE()函数用于返回当前数据库系统的日期和时间,返回值的类型为datetime. SELECT GETDATE() 2. 返回UTC日期的函数G ...

  8. 前端MVVM框架avalon揭秘 - 双向绑定原理

    avalon大家可能不熟悉,但是Knockout估计或多或少听过用过,那么说说KO的几个概念 监控属性(Observables)和依赖跟踪(Dependency tracking) 声明式绑定(Dec ...

  9. cronolog分割Tomcat catalina.out日志

    Linux上tomcat的日志输出在catalina.out里面,随着时间的推移,产生的日志文件会越来越大,其主要是调试中打印的一些信息占空间,比如说System.out和log等等.tomcat 的 ...

  10. 用bootstrap实现多张图片手动轮回

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABBoAAAJoCAIAAABHhBX4AAAgAElEQVR4nOzdZXdcV7rg8fmIM2vm3r