例子、

      <?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="com.atguigu.dao.EmployeeDao">
<!-- namespace="com.atguigu.dao.EmployeeDao":写dao接口的全类名 -->
<!-- 这个文件中能写的所有标签:
cache:和缓存有关
cache-ref:和缓存有关
parameterMap:参数map:废弃的。。。原本是来做复杂参数映射;
resultMap:结果映射:自定义结果集的封装规则;
sql:抽取可重用的sql; delete、update、insert、select:增删改查; databaseId:指定这个CRUD属于哪个数据库的; --> <!-- public int insertEmployee(Employee employee); -->
<!--
让MyBatis自动的将自增id赋值给传入的employee对象的id属性
useGeneratedKeys="true":原生jdbc获取自增主键的方法;
keyProperty="":将刚才自增的id封装给哪个属性
-->
<insert id="insertEmployee" useGeneratedKeys="true"
keyProperty="id">
INSERT INTO t_employee(empname,gender,email)
VALUES(#{empName},#{gender},#{email})
</insert> <!-- public int insertEmployee2(Employee employee); -->
<insert id="insertEmployee2">
<!--查询主键
order="BEFORE":
在核心sql语句之前先运行一个查询sql查到id;将查到的id赋值给javaBean的哪个属性;
-->
<selectKey order="BEFORE" resultType="integer" keyProperty="id">
select max(id)+1 from t_employee
</selectKey>
INSERT INTO t_employee(id,empname,gender,email)
VALUES(#{id},#{empName},#{gender},#{email})
</insert> <!--public int updateEmployee(Employee employee); -->
<update id="updateEmployee" >
UPDATE t_employee
SET
empname=#{empName},gender=#{gender},email=#{email}
WHERE id=#{id}
</update> <!--public boolean deleteEmployee(Integer id); -->
<delete id="deleteEmployee" >
DELETE FROM t_employee WHERE id=#{id}
</delete> <!-- 传参到底能传哪些 -->
<!-- public Employee getEmpById(Integer id); -->
<select id="getEmpById" resultType="com.atguigu.bean.Employee">
select * from t_employee
where id=#{haha}
</select>
<!-- Caused by: org.apache.ibatis.binding.BindingException:
Parameter 'id' not found.
Available parameters are [0, 1, param1, param2]
-->
<!--public Employee getEmpByIdAndEmpName(Integer id,String empName); -->
<select id="getEmpByIdAndEmpName" resultType="com.atguigu.bean.Employee">
select * from t_employee where id=#{id} and empname=#{empName}
</select>
<!-- public Employee getEmployeeByIdAndEmpName(Map<String, Object> map); -->
<select id="getEmployeeByIdAndEmpName" resultType="com.atguigu.bean.Employee">
select * from ${tableName} where id=#{id} and empname=#{empName}
</select>
<!--现象:
1)、单个参数:
基本类型:
取值:#{随便写}
传入pojo:
2)、多个参数:
public Employee getEmpByIdAndEmpName(Integer id,String empName)
取值:#{参数名}是无效了;
可用:0,1(参数的索引)或者param1,param2(第几个参数paramN)
原因:只要传入了多个参数;mybatis会自动的将这些参数封装在一个map中;
封装时使用的key就是参数的索引和参数的第几个表示
Map<String,Object> map = new HashMap<>();
map.put("1",传入的值);map.put("2","传入的值");
#{key}就是从这个map中取值; 3)、@Param:为参数指定key;命名参数;我们以后也推荐这么做;
我们可以告诉mybatis,封装参数map的时候别乱来,使用我们指定的key 4)传入了pojo(javaBean)
取值:#{pojo的属性名} 5)、传入了map:将多个要使用的参数封装起来
取值:#{key} 扩展:多个参数:自动封装map;
method01(@Param("id")Integer id,String empName,Employee employee);
Integer id -> #{id}
String empName -> #{param2}
Employee employee(取出这个里面的email) ->#{param3.email} 无论传入什么参数都要能正确的取出值;
#{key/属性名}
1)#{key}取值的时候可以设置一些规则:
id=#{id,jdbcType=INT};
javaType、jdbcType、mode、numericScale、resultMap、typeHandler、jdbcTypeName、expression
只有jdbcType才可能是需要被指定的;
默认不指定jdbcType;mysql没问题;oracle没问题;
万一传入的数据是null;
mysql插入null没问题;【oracle不知道null到底是什么类型;】 实际上在mybatis中:两种取值方式:
#{属性名}:是参数预编译的方式,参数的位置都是用?替代,参数后来都是预编译设置进去的;安全,不会有sql注入问题
${属性名}:不是参数预编译,而是直接和sql语句进行拼串;不安全;
//id=1 or 1=1 or and empname=
传入一个'1 or 1=1 or';
有:sql语句只有参数位置是支持预编译的;
log_2017_12、log_2018_1;
select * from log_2018_1 where id=? and empname=? id=${id} and empname=#{empName}:
select * from t_employee where id=1 and empname=? id=#{id} and empname=#{empName}:
select * from t_employee where id=? and empname=? 一般都是使用#{};安全;在不支持参数预编译的位置要进行取值就使用${};
--> <!-- public List<Employee> getAllEmps(); -->
<!-- resultType="":如果返回的是集合,写的是集合里面元素的类型 -->
<select id="getAllEmps" resultType="com.atguigu.bean.Employee">
select * from t_employee
</select> <!-- 查询返回一个map -->
<!--public Map<String, Object> getEmpByIdReturnMap(Integer id); -->
<select id="getEmpByIdReturnMap" resultType="map">
select * from t_employee where id=#{id}
</select> <!-- 查询多个返回一个map;查询多个情况下:集合里面写元素类型;
public Map<Integer, Employee> getAllEmpsReturnMap(); -->
<select id="getAllEmpsReturnMap" resultType="com.atguigu.bean.Employee">
select * from t_employee
</select> 接口、 //
/**
* id empname gender email login_account
------ ------------ ------ -------------- ---------------
1 admin 0 admin@qq.com a
* @param id
* @return
*
* 列名作为key,值作为value
*/
public Map<String, Object> getEmpByIdReturnMap(Integer id); public List<Employee> getAllEmps();
/**
* key就是这个记录的主键;value就是这条记录封装好的对象;
* @return
*
* 把查询的记录的id的值作为key封装这个map
* @MapKey("id")
*/
@MapKey("id")
public Map<Integer, Employee> getAllEmpsReturnMap(); public Employee getEmpById(Integer id);
public Employee getEmpByIdAndEmpName(@Param("id")Integer id,@Param("empName")String empName);
public Employee getEmployeeByIdAndEmpName(Map<String, Object> map); public int updateEmployee(Employee employee);
public boolean deleteEmployee(Integer id);
public int insertEmployee(Employee employee);
public int insertEmployee2(Employee employee); ResultMap的例子、 <mapper namespace="com.atguigu.dao.CatDao">
<!-- getCatById(Integer)
resultType="com.atguigu.bean.Cat":使用默认规则;属性列名一一对应
resultMap="mycat":查出数据封装结果的时候,使用mycat自定义的规则
-->
<select id="getCatById" resultMap="mycat">
select * from t_cat where id=#{id}
</select> <!-- 自定义结果集(resultMap):自己定义每一列数据和javaBean的映射规则
type="":指定为哪个javaBean自定义封装规则;全类名
id="":唯一标识;让别名在后面引用 id cName cAge cgender
1 加菲猫 12 0
-->
<resultMap type="com.atguigu.bean.Cat" id="mycat">
<!-- 指定主键列的对应规则;
column="id":指定哪一列是主键列
property="":指定cat的哪个属性封装id这一列数据
-->
<id property="id" column="id"/>
<!-- 普通列 -->
<result property="name" column="cName"/>
<result property="age" column="cAge"/>
<result property="gender" column="cgender"/>
</resultMap>
</mapper>

Mybatis的*Dao.XML中的配置与其对应的接口、resultMap的运用的更多相关文章

  1. SpringMVC(十六):如何使用编程方式替代/WEB-INF/web.xml中的配置信息

    在构建springmvc+mybatis项目时,更常用的方式是采用web.xml来配置,而且一般情况下会在web.xml中使用ContextLoaderListener加载applicationCon ...

  2. 浅谈Hibernate框架(一)——.hbm.xml中的配置

    Hibernate一枚“全自动”的ORM框架: 用IDE工具集成Hibernate会自动生成: 以.hbm.xml为后缀结尾的配置文件+ POJO类 + Dao类 主键查询: Session.load ...

  3. web.xml中JSP配置及 EL表达式

    web.xml 中JSP配置.servlet配置 及 EL表达式 [摘要] servlet 基本配置 <servlet> <servlet-name>LoginServlet& ...

  4. J2EE进阶(五)Spring在web.xml中的配置

     J2EE进阶(五)Spring在web.xml中的配置 前言 在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制自动加载到容器中.在web ...

  5. Spring中,applicationContext.xml 配置文件在web.xml中的配置详解

    一.首先写一下代码结构. 二.再看web.xml中的配置情况. <?xml version="1.0" encoding="UTF-8"?> < ...

  6. 使用Spring时web.xml中的配置

    使用Spring时web.xml中的配置: <?xml version="1.0" encoding="UTF-8"?> <web-app x ...

  7. Struts在Web.xml中的配置及Struts1和Struts2的区别

    (1)配置Struts的ActionServlet     <servlet>元素来声明ActionServlet    <servlet-name>元素:用来定义Servle ...

  8. 调用init方法 两种方式 一个是浏览器方法 一个是 xml中手工配置(load-on-startup)

    调用init方法 两种方式 一个是浏览器方法 一个是 xml中手工配置(load-on-startup)

  9. spring 和springmvc 在 web.xml中的配置

    (1)问题:如何在Web项目中配置Spring的IoC容器? 答:如果需要在Web项目中使用Spring的IoC容器,可以在Web项目配置文件web.xml中做出如下配置: <!-- Sprin ...

  10. Mybatis配置文件SqlMapConfig.xml中的标签

    SqlMapConfig.xml配置文件中的属性 1 配置内容 properties(属性) settings(全局配置参数) typeAliases(类型别名) typeHandlers(类型处理器 ...

随机推荐

  1. HElib

    什么是HElib? HElib是一个基于C++语言的同态加密开源软件库,底层依赖于NTL数论运算库和GMP多精度运算库实现,主要开发者为IBM的Halevi,目前最新版本为1.0.2,实现了支持&qu ...

  2. thewall靶机

    includes.php 内有文件读取漏洞 一开始是想着直接用为协议写入一句话木马但是后来发现不行 因为他的文件读取方式长这样 点击查看代码 <?php include ('/var/www/h ...

  3. 解决StringBuilder readline阻塞问题

    readline之所以会阻塞socket流没有结束符 阻塞场景:read() 没有读取到任何数据 readLine() 没有读取到结束符或者换行符 可以用ready判断通道中数据是否读完,读完返回fa ...

  4. Idea报错 【cannot access com.xxx】的【解决办法】

    正常操作代码,一个类突然标红,提示 cannot access com.xxx . 执行下面操作,执行完后项目变为正常 但是如果是你代码存在异常,可不是清缓存就能解决了,就要靠自己了哈哈 第一步Fil ...

  5. redis 过期监听配置

    package org.jeecg.config; import java.lang.reflect.Method; import java.time.Duration; import java.ut ...

  6. WinForm 进度条显示进度百分比

    参考: https://blog.csdn.net/zhuimengshizhe87/article/details/20640157 WinForm中显示进度条百分比有多种方式: 1. 添加 Lab ...

  7. vim编译和安装clang_complete插件

    今天找到一个插件clang_complete可以进行语法补全,可能不是很好用.但是vim上用应该也差不多了 全文按照这个链接去操作, configure的时候我配置的目录是/usr/bin/vimv8 ...

  8. 【Unity】图形渲染瓶颈与批处理优化

    [Unity]图形渲染瓶颈与批处理优化 图形渲染 工作方式 显卡的工作方式并非连续的,一般分三步. 上传更新渲染数据至显存 设置显卡的渲染管线状态 启动绘制并等待绘制结果 性能瓶颈 由于显卡是大规模并 ...

  9. Typecho自定义右键菜单美化和禁用F12

    右键美化 使用右键美化,请禁用 HoerMouse 鼠标美化插件,否则貌似没效果 Joe主题在后台-外观设置-设置外观-全局设置-自定义<body></body>标签内填入如下 ...

  10. Kettle - 使用案例

    原文链接:https://blog.csdn.net/gdkyxy2013/article/details/117106691 案例一:把seaking的数据按id同步到seaking2,seakin ...