- Mapper.xml总结
Namespace:代表整个Mapper.xml的唯一标识,即命名空间
DML操作标签 insert、update,delete, DQL标签:select parameterType:sql命令要绑定的参数类型,必须是全限定名,参数是javaBean对象不能省略parameterType 参数是简单类型(非javaBan对象)时,可以省略parameterType resultType:sql命令返回的结果类型 ,DML操作可以省略resultType;查询操作select标签不能省略resultType; 如果返回的是对象,则resultType是类的全限定名;如果返回对象的集合,则resultType是集合中元素的类型(全限定名)
useGeneratedKeys:是否使用自动增长的主键 #{xx}:占位符,用于接收传进来的值,如果参数是对象,则xx就是对象中的属性名,mybatis会调用该xx属性对应的getXxx(). 如果参数是简单类型的且只有1个参数,则xx可以任意写(符合java命名规范即可) 例如: <delete id="deleteEmp" parameterType="int"> delete from t_employee where eid=#{sid} </delete> |
2#{xxx}和${xxx}的区别
#{xxx}底层是用的PreParedStatement对象,对SQL进行预编译,每个占位符都会变成?
${xxx}底层用的Statement对象,是直接用参数值代替占位符,如果参数值是String,则要手动的在两边加上单引号
参数绑定的区别:
如果sql语句只需传入一个简单参数,则#{xxx}里面的xxx可以任意写(符合命名规范),而${xxx}里面的xxx只能是value
3.DAO层代理开发(重点)
DAO层代理开发要满足四个规范:
1.Mapper接口的全限定名和Mapper.xml的namespace一致
2.Mapper接口的方法名和Mapper.xml的statment的id一致(不是完整唯一标识)
3.Mapper接口的方法的参数类型和Mapper.xml的statment的parameterType一致
4.Mapper接口的方法的返回值类型和Mapper.xml的statment的resultType一致
- 定义Mapper接口,相当于原始的DAO接口
public interface EmployeeMapper {
//插入
int insert(Employee emp);
//修改
int update(Employee emp);
//删除
int delete(int eid);
//查询单个记录
Employee queryById(int eid);
//全查询
List<Employee> queryAll();
}
-
2).定义Mapper.xml文件
<?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.sxt.mapper.EmployeeMapper"> <insert id="insert" parameterType="com.sxt.domain.Employee" useGeneratedKeys="true"> insert into t_employee(ename,sex,salary) values(#{ename},#{sex},#{salary})
<!-- insert into t_employee(ename,sex,salary) values('${ename}','${sex}',${salary}) --> </insert> <update id="update" parameterType="com.sxt.domain.Employee"> update t_employee set ename=#{ename},sex=#{sex},salary=#{salary} where eid=#{eid} </update>
<delete id="delete" parameterType="int"> delete from t_employee where eid=#{sid} </delete> <select id="queryById" resultType="com.sxt.domain.Employee"> <!-- select * from t_employee where eid=#{sid} --> select * from t_employee where eid=${value} </select>
<select id="queryAll" resultType="com.sxt.domain.Employee"> select * from t_employee </select> </mapper> |
3.1 多个简单参数的绑定
如果Mapper接口中的方法有多个简单参数,那么Mapper.xml文件中绑定多个简单参数的占位符名称xxx只能是param1,param2,...paramN (param1绑定第1个参数,param2绑定第2个参数)
原理:当方法的入参是多个简单参数时,myBatis会将调用方法的实参作为value,将param1,param2..作为key存储到Map中
例如:
方法定义: List<Employee> queryByCondition(String sex,int salary); 调用 List<Employee> list = mapper.queryByCondition("男", 7500); MyBatis会构造一个Map,把实参作为value,用param1,param2..作为key存到Map中 类似于:map.put(“param1”,"男"); map.put(“param2”,7500);
在Mapper.xml文件中通过#{key}来获取Map中的参数值,例如#{param1}绑定参数1的值,#{param2}绑定参数2的值 |
- 主配置文件的其它配置(重点)
4.1.properties标签
加载外部的properties文件
<!-- 加载外部properties文件 --> <properties resource="db.properties"></properties> 可以通过${key}语法去读取properties文件中的value |
4.2.typeAliases标签
用于配置类的别名,具体有2种配置别名方式:
- 在typeAliases内用一个typeAlias来为一个类型配置别名
<typeAliases> <!-- typeAlias指定一个类的别名 type:类的全限定名 alias:别名 --> <typeAlias type="com.sxt.domain.Employee" alias="emp"/> </typeAliases> |
2.在typeAliases内用package标签来批量配置别名
package标签为某个包下的所有类指定别名,默认别名就是类名(不区分大小写)
<typeAliases>
<!-- typeAlias指定一个类的别名 type:类的全限定名 alias:别名 -->
<!-- <typeAlias type="com.sxt.domain.Employee" alias="emp"/> -->
<!-- package为某个包下所有的类批量配置别名,默认别名就是类名本身(不区分大小写) -->
<package name="com.sxt.domain"/>
</typeAliases>
Mapper.xml中用Employee或者employee来引用类全限定名
<select id="queryAll" resultType="Employee">
select * from t_employee
</select>
4.3 加载SQL映射文件的三种方式
1.通过mapper标签的resource属性来加载,值是Mapper.xml的物理路径
2.通过mapper标签的class属性来加载,值是Mapper接口的全限定名,
前提是Mapper接口和Mapper.xml要在同一级包下且文件名要相同,
mybatis会加载与Mapper接口在同一包下且名字相同的Mapper.xml
3.通过package标签来加载,package是指定Mapper接口所在的包名称,mybatis会自动扫描该包下所有Mapper接口
并加载映射文件,前提条件和方式2相同,即Mapper接口和Mapper.xml要在同一级包下且文件名要相同
以上3种方式1,只有方式3是批量加载,即可以加载多个Mapper.xml文件
<mappers>
<!-- <mapper resource="com/sxt/mapper/EmployeeMapper.xml" /> -->
<!-- <mapper class="com.sxt.mapper.EmployeeMapper"/> -->
<package name="com.sxt.mapper"/>
</mappers>
常见错误:
BindingException:绑定异常,没有找到名字为queryAll的Statement,本质就是Mapper.xml没有加载成功
-
5.resultType和resultMap(重点)
resultType:映射(封装)单表的查询结果集,要求字段名和属性名完全一致,如果哪个属性名和字段不相同,则该属性值为null.因为mybatis默认将字段的值封装到实体类的同名属性上
resultMap:映射(封装)多表查询的复杂结果集。可以使用resultMap解决属性名和字段名不一致的问题
使用resultMap属性时,它的值就是外部resultMap元素的id,是对外部resultMap的一个引用
- 自定义一个resultMap,指定封装的规则(字段和属性之间的映射规则)
<resultMap type="com.sxt.domain.Card" id="cardMap">
<id column="cid" property="cid"/>
<result column="c_name" property="cname"/>
</resultMap>
2.在select标签上用resultMap属性来引用resultMap元素,值是resultMap元素的id
<select id="queryAll" resultMap="cardMap">
select * from t_card
</select>