Mybatis的*Dao.XML中的配置与其对应的接口、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="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的运用的更多相关文章
- SpringMVC(十六):如何使用编程方式替代/WEB-INF/web.xml中的配置信息
在构建springmvc+mybatis项目时,更常用的方式是采用web.xml来配置,而且一般情况下会在web.xml中使用ContextLoaderListener加载applicationCon ...
- 浅谈Hibernate框架(一)——.hbm.xml中的配置
Hibernate一枚“全自动”的ORM框架: 用IDE工具集成Hibernate会自动生成: 以.hbm.xml为后缀结尾的配置文件+ POJO类 + Dao类 主键查询: Session.load ...
- web.xml中JSP配置及 EL表达式
web.xml 中JSP配置.servlet配置 及 EL表达式 [摘要] servlet 基本配置 <servlet> <servlet-name>LoginServlet& ...
- J2EE进阶(五)Spring在web.xml中的配置
J2EE进阶(五)Spring在web.xml中的配置 前言 在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制自动加载到容器中.在web ...
- Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
一.首先写一下代码结构. 二.再看web.xml中的配置情况. <?xml version="1.0" encoding="UTF-8"?> < ...
- 使用Spring时web.xml中的配置
使用Spring时web.xml中的配置: <?xml version="1.0" encoding="UTF-8"?> <web-app x ...
- Struts在Web.xml中的配置及Struts1和Struts2的区别
(1)配置Struts的ActionServlet <servlet>元素来声明ActionServlet <servlet-name>元素:用来定义Servle ...
- 调用init方法 两种方式 一个是浏览器方法 一个是 xml中手工配置(load-on-startup)
调用init方法 两种方式 一个是浏览器方法 一个是 xml中手工配置(load-on-startup)
- spring 和springmvc 在 web.xml中的配置
(1)问题:如何在Web项目中配置Spring的IoC容器? 答:如果需要在Web项目中使用Spring的IoC容器,可以在Web项目配置文件web.xml中做出如下配置: <!-- Sprin ...
- Mybatis配置文件SqlMapConfig.xml中的标签
SqlMapConfig.xml配置文件中的属性 1 配置内容 properties(属性) settings(全局配置参数) typeAliases(类型别名) typeHandlers(类型处理器 ...
随机推荐
- 【忍者算法】从照片旋转到矩阵变换:探索图像旋转问题|LeetCode 48 旋转图像
从照片旋转到矩阵变换:探索图像旋转问题 生活中的旋转 在这个自拍时代,我们经常需要调整照片的方向.有时拍出来的照片歪了,需要旋转90度:有时想要换个角度看看效果,来回旋转照片.这种旋转操作不仅存在于我 ...
- manim边学边做--通用变换
在 Manim 动画制作中,Transform.TransformFromCopy.ReplacementTransform和Restore是四个通用的对象变换动画类. 这几个类能够实现从一个对象到另 ...
- Codeforces 1110D Jongmah 题解 [ 蓝 ] [ 线性 dp ] [ 观察 ]
Jongmah:小清新麻将 dp 题. 观察 首先观察这两个操作的性质,不难发现我们出掉的所有的顺子只要累计出了三次,这三次顺子就一定可以化作出三次相同的单牌. 而我们只需要最大化操作次数,显然这三次 ...
- [JSOI2008]火星人 题解
原题链接:\(luogu\)$\ \ $ \(BZOJ\)$\ \ $ \(LOJ\) 题目大意:有一个可以支持插入和修改的字符串,定义函数 \(\operatorname{LCQ(x,y)}\) 表 ...
- [HDU5396] Expression 题解
每次合并两个数,做过石子合并的人都能看出来是区间 dp. 设状态 \(dp_{i,j}\) 表示区间 \([i,j]\) 中合并为一个数的所有情况之和. 那么我们就可以枚举断点 \(k\): \(b_ ...
- 最新版go-cqhttp的sign 签名服务器搭建教程
安装go-cqhttp 传送门 自建sign签名服务器容器: 拉取镜像(只支持amd64) docker pull hansaes/unidbg-fetch-qsign:latest 启动容器 doc ...
- 傻妞教程——对接PagerMaid-Pyro
PagerMaid-Pyro 是一个开源的 TG 人形自走 Bot 方案,功能强大而丰富,可以帮助你打造专属的便利功能. 为什么叫人形机器人? TG 官方是有 Bot Api 的,但是这个 Api 需 ...
- 使用QT开发远程linux服务器过程
1.添加设备为通用linux 2.设置ip用户名 3.创建私钥文件,原来有的qtc那俩个文件删掉. 4.部署公钥,前提是测试链接要出现成功 5.在kits里添加编译环境设置编译器为32位或者64 6. ...
- 【问题解决】Jenkins使用File的exists()方法判断文件存在,一直提示不存在的问题
小剧场 最近为了给项目组提供一个能给Java程序替换前端.后端的增量的流水线,继续写上了声明式流水线. 替换增量是根据JSON配置文件去增量目录里去取再替换到对应位置的,替换前需要判断增量文件是否存在 ...
- surpac 中如何删除点
找到显示的编号 输入线窜线段编号