SQL 映射文件
SQL 映射文件只有很少的几个顶级元素(按照应被定义的顺序列出):
-
cache
– 对给定命名空间的缓存配置。 -
cache-ref
– 对其他命名空间缓存配置的引用。 -
resultMap – 是最复杂也是最强大的元素,用来描述如何从数据库结果集中来加载对象。 (映射)
-
parameterMap – 已被废弃!老式风格的参数映射。更好的办法是使用内联参数,此元素可能在将来被移除。文档中不会介绍此元素。
-
sql – 可被其他语句引用的可重用语句块。
-
insert – 映射插入语句
-
update – 映射更新语句
-
delete – 映射删除语句
-
select – 映射查询语句
<select>
查询操作
- id:对应接口的方法名
- parameterType:对应接口的参数类型
- resultType:对应接口的返回值类型
- resultMap:对应接口的自定义映射返回值类型
insert update delete 三个标签都没有返回值类型的指定 他们默认返回int值(表示影响行数)。
resultType:当使用resultType做SQL语句返回结果类型处理时,对于SQL语句查询出的字段在相应的pojo中必须有和它相同的字段对应,而resultType中的内容就是pojo在本项目中的位置。
因此对于单表查询的话用resultType是最合适的。但是,如果在写pojo时,不想用数据库表中定义的字段名称,也是可以使用resultMap进行处理对应的
${}和#{}的区别:
1. 掌握使用MyBatis实现查询功能(select)
1.1 Select语句的基本使用方法
<!-- User getById(Long id)throws Exception; -->
<!--
id:关联方法名
parameterType:入参类型
resultType:返回值类型
-->
<select id="getById" parameterType="_long" resultType="User">
select
*
from
smbms_user
where
id = #{id}
</select>
1.2 Select语句的复杂使用
<!--
resultType:如果返回值是集合,只需要定义它的泛型类型即可
-->
<!--通过用户名模糊查询用户
List<User> getListByUsername(String username)throws Exception; -->
<select id="getListByUsername" parameterType="string" resultType="User">
select
*
from
smbms_user
where
userName like CONCAT('%',#{username},'%')
</select>
**多参数:**采用Map集合或者对象形式来传入多个值。
//根据角色编号,用户名模糊查询
List<User> getListByUsernameAndUserRole(String username,Long userRole)throws Exception;
但是在.xml中参数不能写两个 于是我们可以把他组成对象
<sql id="all_user">
select
*
from
smbms_user
</sql>
<!-- List<User> getListByMap(Map<String,Object> paramsMap)throws Exception; -->
<!--多参数传值,参数为map集合。#里面的名为 map.put里面的键名 -->
<select id="getListByMap" parameterType="map" resultType="User">
<include refid="all_user"></include>
where
userName like CONCAT('%',#{userName},'%')
and
userRole = #{userRole}
</select>
<!--
#{对象的属性名}
-->
<!-- List<User> getListByUser(User user)throws Exception; -->
<!-- 如果传输的参数是对象,#里面为对象的属性名,必须保证有getset方法 -->
<select id="getListByUser" parameterType="User" resultType="User">
<include refid="all_user"></include>
where
userName like CONCAT('%',#{userName},'%')
and
userRole = #{userRole}
</select>
**多参数:**还可以使用注解来传递。
<sql id="all_user">
select * from smbms_user
</sql>
<!-- List<User> getListByUsernameAndUserRole(@Param("userName")String username,@Param("roleId")Long userRole)throws Exception; -->
<!--如果采用注解@param 不再需要提供parameterType -->
<!--多参数 使用注解 #里面的和@param里面的一致 @Param("userName")是实体类的的属性 String username是自己起的 -->
<select id="getListByUsernameAndUserRole" resultType="User">
<include refid="all_user"></include>
where
userName like CONCAT('%',#{userName},'%')
and
userRole = #{roleId}
</select>
上述的多参数值传递,注意使用规范:
- 一般来讲我们如果参数超过3个值,那么推荐你采用对象形式来传递!(Map)
- 但是如果我们做的功能是一个固定业务,基本不会变更的,那么可以用注解多为多参数值传递,因为业务描述比较清晰。
2. 掌握使用MyBatis实现增、删、改
【useGeneratedKeys:】(仅适用于 insert 和 update)表示要获取自动生成的主键
这会令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键
(比如:像 MySQL 和 SQL Server 这样的关系型数据库管理系统的自动递增字段),默认值:false。
【keyProperty】:(仅适用于 insert 和 update)表示获取到自动生成主键之后应该映射到对象的哪个属性中
MyBatis 会使用 getGeneratedKeys 的返回值或 insert 语句的 selectKey 子元素设置它的值,默认值:未设置(unset)。
如果生成列不止一个,可以用逗号分隔多个属性名称。
2.1 增加(insert)
<!-- int addUser(User user)throws Exception; -->
<!--
useGeneratedKeys: 表示我们传入的对象信息在数据库存储时使用的是主键自增
keyProperty:将主键自增的结果 在执行完添加之后 自动映射到我们的用户对象中去 id就是自增列的属性
增删改系列都没有返回值类型 默认返回影响行数
-->
<insert id="addUser" parameterType="User" useGeneratedKeys="true" keyProperty="id">
insert into smbms_user
(userCode,userName)
values(#{userCode},#{userName})
</insert>
useGeneratedKeys="true" keyProperty="id
不加这句的时候 数据库也可以正常添加数据, 即使你的数据库主键id有值,但是还是获取不到数据的主键 加上以后 可以获取到他的主键值 这里的主键就是id
但是在项目中实际是可以取到他的id的 不写的时候 暂时也不清楚
?????
2.2 修改(update)
<!-- int updatePassword(@Param("id")Long id,@Param("newPwd")String newPwd)throws Exception; -->
<update id="updatePassword">
update
smbms_user
set
userPassword = #{newPwd}
where
id = #{id}
</update>
<!-- int updateUser(User user)throws Exception; Bug -->
<update id="updateUser" parameterType="User">
update
smbms_user
set
userCode = #{userCode},
userName = #{userName},
userPassword = #{userPassword}
where
id = #{id}
</update>
2.3 删除(delete)
<!-- int delUser(@Param("id")Long id)throws Exception; -->
<delete id="delUser">
delete from smbms_user where id = #{id}
</delete>
3. 掌握MyBatis的自定义映射
resultMap
MyBatis是如何进行工作的?
例如查询:
<!--
#{对象的属性名}
-->
<!-- List<User> getListByUser(User user)throws Exception; -->
<select id="getListByUser" parameterType="User" resultType="User">
select
*
from
smbms_user
where
userName like CONCAT('%',#{userName},'%')
and
userRole = #{userRole}
</select>
我们要给MyBatis指定好要返回的结果类型,传入的参数类型。
我们要给MyBatis编写好要执行的SQL语句。
MyBatis它会将入参中的类型(对象)的属性值按照属性名去注入到对应的SQL语句的占位符中。
MyBatis它会将SQL语句执行的结果按照查询结果字段名和返回结果类型(对象)的属性名进行映射。
**需求1 一般情况下的自定义映射:**根据用户名和角色模糊查询用户,要求查询出来的结果包含角色名。
<!-- 自定义映射
type 自定义映射的结果类型
-->
<resultMap type="User" id="user-role">
<!--
用于自定义映射主键信息【设置后会提高一定的效率】
column:查询的结果列名
property:映射到结果类型中的属性名
-->
<id column="id" property="id"/>
<!--
用于自定义映射普通字段信息
因为默认的映射级别为PARTIAL,所以它会自动映射
哪怕你是采用了自定义映射,这个自动映射也会生效(列名和属性名是一致的)
所以你只需要映射你不一样的这种 列 -> 属性
user这个类中添加userRoleName这个属性
-->
<!-- <result column="userCode" property="userCode"/>
<result column="userName" property="userName"/>
<result column="userPassword" property="userPassword"/> -->
<result column="roleName" property="userRoleName"/>
<!-- ...... -->
</resultMap>
<!-- List<User> getListWithRnameByUnameAndRid(@Param("userName")String userName,
@Param("roleId")Long roleId)throws Exception; -->
<!--
resultMap:自定义映射的id值
-->
<select id="getListWithRnameByUnameAndRid" resultMap="user-role">
select
u.*,r.roleName
from
smbms_user u,smbms_role r
where
u.userRole = r.id
and
u.userName like CONCAT('%',#{userName},'%')
and
u.userRole = #{roleId}
</select>
**需求2 复杂类型 一对一:**根据用户名和角色模糊查询用户,要求查询出来的结果包含角色所有信息(现在嵌套的是一个对象)在user这个类中写一个对象的属性 private Role role;PARTIAL 如果是她 查出来的就没有user这个信息 只有role这个信息
因为自定义了id对应id,usercode对应usercode user里面只有这两信息 如果写出来的话 获取的user就是空的
<resultMap type="User" id="user-role">
<id column="id" property="id"/>
<result column="userCode" property="userCode"/>
<!-- 关联对象映射 -->
<!-- (用户表里面放了一个角色对象,这个对象在user表中的属性名)
property:对象在结果类型中的属性名
javaType:对象的类型
column=rid是下面的sql语句查出来的名
-->
<association property="role" javaType="Role">
<id column="rid" property="id"/>
<result column="roleName" property="roleName"/>
<result column="roleCode" property="roleCode"/>
<result column="rcreatedBy" property="createdBy"/>
<result column="rcreationDate" property="creationDate"/>
</association>
</resultMap>
<!--
List<User> getListWithRoleByUnameAndRid(@Param("userName")String userName,
@Param("roleId")Long roleId)throws Exception;
-->
<!--
resultMap:自定义映射的id值
-->
<!--
u和r里面有重复字段 不能写r.* 自定义映射中用不了
-->
<select id="getListWithRoleByUnameAndRid" resultMap="user-role">
select
u.*,r.roleName,
r.id rid,r.roleCode,
r.createdBy rcreatedBy,
r.creationDate rcreationDate
from
smbms_user u,smbms_role r
where
u.userRole = r.id
and
u.userName like CONCAT('%',#{userName},'%')
and
u.userRole = #{roleId}
</select>
<settings>
<!-- 指定 MyBatis 应如何自动映射列到字段或属性。
NONE 表示取消自动映射;
PARTIAL 只会自动映射没有定义嵌套结果集映射的结果集。 (默认的)
FULL 会自动映射任意复杂的结果集(无论是否嵌套)。 -->
<setting name="autoMappingBehavior" value="FULL"/>
</settings>
<result column="userCode" property="userCode"/>
setting为full 这个可以不写
**需求3 复杂类型 一对多:**根据用户名和角色模糊查询用户,要求查询出来的结果包含所有地址信息
要修改核心配置文件autoMappingBehavior的值为full
user表中private List<Address> address
<resultMap type="User" id="user-role">
<id column="id" property="id"/>
<result column="userCode" property="userCode"/>
<!-- 关联集合映射
ofType:泛型类型
-->
<collection property="addresses" ofType="Address">
<result column="contact" property="contact"/>
<result column="addressDesc" property="addressDesc"/>
</collection>
</resultMap>
<!--
List<User> getListWithAddrListByUnameAndRid(@Param("userName")String userName,
@Param("roleId")Long roleId)throws Exception;
-->
<!--
resultMap:自定义映射的id值
-->
<select id="getListWithAddrListByUnameAndRid" resultMap="user-role">
select
u.*,a.contact,a.addressDesc
from
smbms_user u,smbms_address a
where
u.id = a.userId
and
u.userName like CONCAT('%',#{userName},'%')
and
u.userRole = #{roleId}
</select>