目录
一、一对一映射
若一个实体类中含有另一个实体,则是一对一的关系。例如,一个地址对应一个用户
写ORM映射
<resultMap type="address" id="addressMap">
<id property="id" column="a_id"/>
<result property="shen" column="a_shen"/>
<result property="shi" column="a_shi"/>
<result property="xian" column="a_xian"/>
<result property="street" column="a_street"/>
</resultMap>
测试发现:users对象为null
在ORM映射中添加一对一映射
<resultMap type="address" id="addressMap">
<id property="id" column="a_id"/>
<result property="shen" column="a_shen"/>
<result property="shi" column="a_shi"/>
<result property="xian" column="a_xian"/>
<result property="street" column="a_street"/>
<!-- association为一对一映射
属性
property:映射的是哪一个属性
javaType:映射的属性的类型
autoMapping:如果存在一对一或者一对多映射,那么必须添加自动映射的属性
-->
<association property="users" javaType="users" >
<id property="id" column="u_id"/>
<result property="username" column="u_username"/>
<result property="sex" column="u_sex"/>
<result property="address" column="u_address"/>
</association>
</resultMap>
二、一对多映射
<select id="findUsers" resultMap="UserMap">
select * from t_users us join t_address addr on us.u_id=addr.u_id
</select>
<resultMap type="users2" id="UserMap" autoMapping="true">
<id property="id" column="u_id"/>
<result property="username" column="u_username"/>
<result property="sex" column="u_sex"/>
<!-- 一对多关系映射 -->
<collection property="address" ofType="address2" autoMapping="true">
<!-- 开始地址属性和地址表字段的映射 -->
<id property="id" column="a_id"/>
<result property="shen" column="a_shen"/>
<result property="shi" column="a_shi"/>
<result property="xian" column="a_xian"/>
<result property="street" column="a_street"/>
</collection>
</resultMap>
一对一和一对多经常在连表查询时候要用到
三、多对多
任何多对多的关系都会有有一张中间表,转成一对多,或者一对一的关系
四、动态SQL拼接
1.if标签
<!-- 多条件拼接:if标签 -->
<select id="findTeachers1" parameterType="teacher" resultMap="teacherMap">
select * from teacher where 1=1
<!-- 判断查询条件:姓名或者工资、注册时间 -->
<if test="#{name!=null}"> <!--#{}里面取对象的属性名 -->
and t_name=#{name}
</if>
<if test="#{balance!=0}">
and t_balance=#{balance}
</if>
<if test="#{regTime!=null}">
and t_regTime=#{regTime}
</if>
</select>
2.where标签
<!-- 多条件拼接: where标签 -->
<select id="findTeachers2" parameterType="teacher" resultMap="teacherMap">
select * from teacher
<!-- where标签中的条件,会自动去掉 多余的 and -->
<where>
<if test="name!=null and name!=''">
and t_name=#{name}
</if>
<if test="balance!=0 and balance!=''">
and t_balance=#{balance}
</if>
</where>
</select>
3.choose\when\otherwise标签
<!-- 用价格或者商品类别查找-->
<select id="findProductByCondition" parameterType="productQueryBean" resultMap="productMap">
select pro.*,typ.type_name from t_product pro
join t_type typ on pro.type_id=typ.type_id
<where>
<if test="maxPrice !=0">
<choose>
<when test="maxPrice <= 2000">
and pro_price < 2000
</when>
<when test="maxPrice <= 4000">
and pro_price < 4000
</when>
<when test="maxPrice <= 6000">
and pro_price < 6000
</when>
<when test="maxPrice <= 8000">
and pro_price < 8000
</when>
<otherwise>
and pro_price <1000
</otherwise>
</choose>
</if>
<if test ="typeId !=0 and typeId!=''">
and typ.type_id =#{typeId}
</if> -->
<!-- <if test ="product !=null and product!=''">
</if> -->
</where>
</select>
4.set标签
<!--动态set语句可以用来更新数据 -->
<update id="updateUseSet" parameterType="Dept">
update dept
<set>
<if test="deptName!=null">dept_name=#{deptName},</if>
<if test="deptAddress!=null">dept_address=#{deptAddress},</if>
</set>
where dept_id=#{deptId}
</update>
5.foreach标签
<select id="findShopcarItems" parameterType="java.util.List" resultMap="shopcaritemMap">
select * from t_shopcaritem where citem_id in
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
五、批量操作
1.PLSQL块做批量插入(不能返回受影响行数)
将多条insert语句作为一个整体一起插入
<mapper namespace="b_batch.Mapper2">
<!-- PLSQL块做批量插入 -->
<insert id="insertBatchStus" parameterType="java.util.List">
begin
<foreach collection="list" item="stu">
insert into stu values(seq_emp.nextval,
#{stu.username},
#{stu.password});
</foreach>
end;
</insert>
</mapper>
2.使用虚表插入,能够返回受影响行数
<!-- 批量插入2:能返回受影响的行数 -->
<insert id="insertBatchStus2" parameterType="java.util.List">
insert into stu(u_id,u_username,u_password)
select seq_emp.nextval u_id, u.* from
(
<foreach collection="list" item="stu" separator="union">
select
#{stu.username} u_username,
#{stu.password} u_password
from dual
</foreach>
)u
</insert>
3.批量删除、修改
<!-- 批量删除 -->
<delete id="deleteBatchusers" parameterType="java.util.List">
delete from stu where u_id in
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
<!-- 批量修改 -->
<update id="updateBatchUsers" parameterType="java.util.List">
begin
<foreach collection="list" item="stu">
update stu
<set>
<if test="stu.username != null and stu.username != ''">
u_username = #{stu.username},
</if>
<if test="stu.password != null and stu.password != ''">
u_password= #{stu.password}
</if>
</set>
where u_id = #{stu.id};
</foreach>
end;
</update>
六、Mybatis解决jdbc编程的问题
1.将SQL语句放在mapper.xml配置文件中,解决传统jdbc的代码不易维护的问题,实现了代码和SQL语句分离
2.在mybatis.xml中配置数据链接池,使用连接池管理数据库链接。解决了jdbc链接创建、释放频繁造成系统资源浪费从而影响系统性能(可用连接池)
3.Mybatis自动将java对象映射至sql语句,在mapper.xml中对SQL语句的parameterType定义传入参数类型,更加方便,简单。
4.Mybatis自动将sql执行结果映射至java对象,在mapper.xml中对SQL语句的resultType定义输出参数类型。