创建位置:必须和持久层接口在相同的包中。
名称:必须以持久层接口名称命名文件名,扩展名是.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.itheima.dao.IUserDao"> <!--配置 查询结果的列名和实体类的属性名的对应关系--> <resultMap id="userMap"type="uSeR">
id=是唯一标志着这个配置映射对象,下面谁想要这个映射,来保证数据库的值能够赋予到实体类中,
就要用
resultMap="userMap"
<!--主键字段的对应-->
<id property="userId"column="id"></id>
<!--非主键字段的对应,只要是因为实体类JavaBean的属性名和数据库列名不一样
,就需要配置导致一一对应,才能保证数据库映射成功--> <result property="userName"column="username"></result> <result property="userAddress"column="address"></result> <result property="userSex"column="sex"></result> <result property="userBirthday"column="birthday"></result> </resultMap> <!--查询所有--> <select id="findAll"resultMap="userMap"> <!--select id as userId,username as userName,address as userAddress,sex as userSex,birthday as userBirthday from user;--> select*from user; </select> <!--保存用户-->
parameterType="user"---》参数类型或者javabean对象的类型,INt,interge,String....
resultType="int",返回的结果集类型是int
<insert id="saveUser"parameterType="user"> <!--配置插入操作后,获取插入数据的id
下面的就是当实现insert功能后,就会产生新的id,然后通过下面那个可以把id查询出来并且赋值进去到JavaBean,
order=after意思就是实现insert插入再执行这个查询赋值
--> <selectKey keyProperty="userId"keyColumn="id"resultType="int"order="AFTER"> select last_insert_id(); </selectKey> insert into user(username,address,sex,birthday)values(#{userName},#{userAddress},#{userSex},#{userBirthday}); </insert> <!--更新用户-->
占位符的书写形式:#{userName}${}这个是字符串拼接或者其他properties文件的key名称<update id="updateUser"parameterType="USER"> update user set username=#{userName},address=#{userAddress},sex=#{userAex},birthday=#{userBirthday}where id=#{userId} </update> <!--删除用户--> <delete id="deleteUser"parameterType="java.lang.Integer"> delete from user where id= #{uid} </delete> <!--根据id查询用户--> <select id="findById"parameterType="INT"resultMap="userMap"> select*from user where id= #{uid} </select> <!--根据名称模糊查询--> <select id="findByName"parameterType="string"resultMap="userMap"> select*from user where username like #{name} <!--select*from user where username like'%${value}%'--> </select> <!--获取用户的总记录条数--> <select id="findTotal"resultType="int"> select count(id)from user; </select> <!--根据queryVo的条件查询用户--> <select id="findUserByVo"parameterType="com.itheima.domain.QueryVo"resultMap="userMap"> select*from user where username like #{user.username} </select> </mapper>
sql语句的拼接
<!-- 了解的内容:抽取重复的sql语句-->
<sql id="defaultUser">
select * from user
</sql>
<!-- 查询所有 -->
<select id="findAll" resultMap="userMap">
<include refid="defaultUser"></include>
</select>
<!-- 根据id查询用户 -->
<select id="findById" parameterType="INT" resultMap="userMap">
select * from user where id = #{uid}
</select>
<!-- 根据名称模糊查询 -->
<select id="findByName" parameterType="string" resultMap="userMap">
select * from user where username like #{name}
</select>
<!-- 根据queryVo的条件查询用户 -->
<select id="findUserByVo" parameterType="com.itheima.domain.QueryVo" resultMap="userMap">
select * from user where username like #{user.userName}
</select>
<!-- 根据条件查询
<select id="findUserByCondition" resultMap="userMap" parameterType="user">
select * from user where 1=1
<if test="userName != null">
and username = #{userName}
</if>
<if test="userSex != null">
and sex = #{userSex}
</if>
</select>-->
<select id="findUserByCondition" resultMap="userMap" parameterType="user">
select * from user
<where>
<if test="userName != null">
and username = #{userName}
</if>
<if test="userSex != null">
and sex = #{userSex}
</if>
</where>
</select>
<!-- 根据queryvo中的Id集合实现查询用户列表 -->
<select id="findUserInIds" resultMap="userMap" parameterType="queryvo">
<include refid="defaultUser"></include>
<where>
<if test="ids != null and ids.size()>0">
<foreach collection="ids" open="and id in (" close=")" item="uid" separator=",">
#{uid}
</foreach>
</if>
</where>
</select>
<mapper namespace="com.itheima.dao.IUserDao">
namespace:指的是该数据库接口类所在的全限定类名,用来唯一标志
<!-- 定义User的resultMap-->
<resultMap id="userAccountMap" type="user">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="address" column="address"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="birthday"></result>
<!-- 配置user对象中accounts集合的映射 -->
collection:接口--》由于AcountBean中有 List<AcountBean>accounts这个属性
ofType="AcountBean",是为了告诉该属性是属于那个JavaBean类型,
通过查询Userbean就可以通过那个字段查询另一个表并封装对应这个关系到实体类中
<collection property="accounts" ofType="AcountBean">
<id column="aid" property="id"></id>
<result column="uid" property="uid"></result>
<result column="money" property="money"></result>
</collection>
</resultMap>
<!-- 查询所有 -->
<select id="findAll" resultMap="userAccountMap">
select * from user u left outer join account a on u.id = a.uid
</select>
<!-- 根据id查询用户 -->
<select id="findById" parameterType="INT" resultType="user">
select * from user where id = #{uid}
</select>
<mapper namespace="com.itheima.dao.IAccountDao">
<!-- 定义封装account和user的resultMap -->
<resultMap id="accountUserMap" type="account">
<id property="id" column="aid"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
<!-- 一对一的关系映射:配置封装user的内容-->
association :一个JavaBean属性--》由于AcountBean中有 UserBean user这个属性
ofType="AcountBean",是为了告诉该属性是属于那个JavaBean类型,
通过查询Userbean就可以通过那个字段查询另一个表并封装对应这个关系到实体类中
javaType为了告诉封装这个user对象的类型是UserBean,
column="uid" 值得是通过上面查询到的uid来查询下面的数据,并且封装到这些实体类中,多表操作
<association property="user" column="uid" javaType="UserBean">
<id property="id" column="id"></id>
<result column="username" property="username"></result>
<result column="address" property="address"></result>
<result column="sex" property="sex"></result>
<result column="birthday" property="birthday"></result>
</association>
</resultMap>
<!-- 查询所有 -->
<select id="findAll" resultMap="accountUserMap">
select u.*,a.id as aid,a.uid,a.money from account a , user u where u.id = a.uid;
</select>
<!--查询所有账户同时包含用户名和地址信息-->
<select id="findAllAccount" resultType="accountuser">
select a.*,u.username,u.address from account a , user u where u.id = a.uid;
</select>
</mapper>
<!--定义role表的ResultMap-->
<resultMap id="roleMap" type="role">
<id property="roleId" column="rid"></id>
<result property="roleName" column="role_name"></result>
<result property="roleDesc" column="role_desc"></result>
<collection property="users" ofType="user">
<id column="id" property="id"></id>
<result column="username" property="username"></result>
<result column="address" property="address"></result>
<result column="sex" property="sex"></result>
<result column="birthday" property="birthday"></result>
</collection>
</resultMap>
<!--查询所有
通过下面的sql语句将得到的数据安装上面的配置封装到相应的实体类相应的属性中去-->
<select id="findAll" resultMap="roleMap">
select u.*,r.id as rid,r.role_name,r.role_desc from role r
left outer join user_role ur on r.id = ur.rid
left outer join user u on u.id = ur.uid
</select>
实现数据的2 延迟加载和立即加载的概念
<!-- 定义User的resultMap-->
<resultMap id="userAccountMap" type="user">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="address" column="address"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="birthday"></result>
<!-- 配置user对象中accounts集合的映射 -->
先查询select * from user这个语句得到的数据,再去执行com.itheima.dao.IAccountDao.findAccountByUid这个办法,
所需要的参数就是select * from user这个语句得到的数据中的id的值就是这个办法所需要的参数值,从而查询出数据封装到该属性中
<collection property="accounts" ofType="account" select="com.itheima.dao.IAccountDao.findAccountByUid" column="id"></collection>
</resultMap>
<!-- 查询所有 -->
<select id="findAll" resultMap="userAccountMap">
select * from user
</select>
<!-- 根据id查询用户 -->
<select id="findById" parameterType="INT" resultType="user">
select * from user where id = #{uid}
</select>
<mapper namespace="com.itheima.dao.IAccountDao">
<!-- 定义封装account和user的resultMap -->
<resultMap id="accountUserMap" type="account">
<id property="id" column="id"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
<!-- 一对一的关系映射:配置封装user的内容
select属性指定的内容:查询用户的唯一标识:
column属性指定的内容:用户根据id查询时,所需要的参数的值
-->
<association property="user" column="uid" javaType="user" select="com.itheima.dao.IUserDao.findById"></association>
</resultMap>
<!-- 查询所有 -->
<select id="findAll" resultMap="accountUserMap">
select * from account
</select>
<!-- 根据用户id查询账户列表 -->
<select id="findAccountByUid" resultType="account">
select * from account where uid = #{uid}
</select>
</mapper>
版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!
常见资源合集和破解beqptwpmc...