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(类型处理器 ...
随机推荐
- shell脚本中的循环(for循环,while循环)及break、continue的用法示例
Shell脚本中也算是一门简易的编程语言了,当然循环是不能缺少的.常用到的循环有for循环和while循环.下面就分别介绍一下两种循环的结构. [for循环]: Shell脚本中的for循环示例: # ...
- Loadrunner11压测场景时最多跑5000个Vuser的问题解决办法
当我在用Loadrunner11压测场景时,设置Start Vusers是10000个.但实际运行是发现大约跑到5000个左右,后面的基本都是异常.如图: 查看Vusers运行详情: Error... ...
- Centos 7 单用户模式修改密码/配置
1.启动系统,启动项界面显示时,按"↑↓"键停留在此界面,按下"e"键进入编辑模式 2.利用"↑↓"键找到"linux16&quo ...
- 火了整个春节的DeepSeek,他对AI产品的意义到底是什么?
相信春节期间各位的朋友圈一定被DeepSeek"轰炸"了,就算是普通人也获得了一些信息:国内AI取得了巨大突破. 但DeepSeek这次突破到底对一般的互联网从业者有什么帮助,绝大 ...
- 解决Git拉取出现“bad config line 1 in file C:\Users\quber/.gitconfig”的错误
1.问题描述 我们在拉取Git项目的时候,突然出现如下图所示的错误提示: 2.解决办法 定位到.gitconfig文件,然后将其删除掉: 然后在项目文件夹中点击鼠标右键,选择Git Bash Here ...
- Django项目实战:创建和迁移数据库
Django项目实战:创建和迁移数据库 一.配置数据库 Django默认使用SQLite数据库,但你也可以选择其他数据库如MySQL.PostgreSQL等.在./settings.py文件中,你可以 ...
- FFT & NTT & FWT
只是学习笔记,真心推荐 cmd ,他讲的真的细到把所有的前置知识都讲了一遍. \[FBI \ WARNING:本篇 NTT 部分非常不完善 \] FFT & NTT & FWT 大杂烩 ...
- Flink11--FliterAndKeyBy算子
一.导入依赖 参考本人下博客 二.代码 FLink11FilterApp.java package net.xdclass.class9; import org.apache.flink.api.co ...
- SpringBoot+Mybatis-Plus使用多数据源
常见的使用Mybatis-Plus配置多数据源方式有两种:一种是通过java config的方式手动配置两个数据源,另一种方式便是使用 dynamic-datasource-spring-boot-s ...
- Thymeleaf判断集合是否为空
Thymeleaf判断集合是否为空最近项目使用的是thymeleaf.项目架构是Springboot+Thymeleaf. 在判断集合是否为空的时候踩了坑与大家分享. 以下代码是判断集合是否为空的,m ...