一、各种实体类的简介及作用
pojo:不按mvc分层,只是javabean有一些属性,还有getset方法
domain:不按mvc分层,只是javabean有一些属性,还有getset方法
po:用在持久层,还可以再增加或者修改的时候,从页面直接传入action中,它里面的javabean 类名等于表名,
属性名等于表的字段名,还有对应的getset方法
vo:view object表现层对象,主要用于在高级查询中从页面接收传过来的各种参数.好处是扩展性强
bo:用在servie层,现在企业基本不用.
这些po,vo,bo,pojo可以用在各种层面吗
可以,也就是po用在表现层,vo用在持久层不报错,因为都是普通的javabean没有语法错误.但是在企业最好不要混着用,因为这些都是设计的原则,混着用比较乱.不利于代码维护.
二、输入映射paramterType
#{id}-占位符
${value}拼接符字符串原样拼接。
<!--
id:sql语句的唯一标识符
parameterType:指定传入的参数的类型
resultType:指定返回的参数的类型
#{}:如果是基本数据类型(Integer、String、double...),那么#{}花括号中的变量可随意填写,如果是pojo那么必须保证和pojo的变量名一致
-->
<select id="findById" parameterType="int" resultType="org.lier.zz.User">
select * from user where id = #{id}
</select>
三、输出映射
resultType:返回单条是指定是接收数据的类型pojo,返回多条结果的时候,得到的结果是是集合的泛型的类型。
四、动态sql
1、where和if搭配使用
select id="findUsersByNameAndSex" parameterType="org.lier.zz.User" resultType="org.lier.zz.User">
select *from user where 1=1
<-- 这种方式拼接完成的sql有问题 -->
<if test="username != null and username != ''">
and username like '%${username}%'
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if>
</select>
拼接完成 的sql是这样的:
select *from user where 1=1 and username like '%李%' and sex = ?
这样的话,即便不传入值也能获取相应的结果。
更改:
<select id="findUsersByNameAndSex" parameterType="org.lier.zz.User"
resultType="org.lier.zz.User">
select *from user
<where>
<!-- where标签作用:
1、自动添加where关键字
2、去掉第一个条件的and关键字 -->
<if test="username != null and username != ''">
and username like '%${username}%'
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if>
</where>
</select>
这种方式生成的sql语句:
select *from user WHERE username like '%李%' and sex = ?
非常的人性化,特别的棒棒!!
2、sql的重用
sql标签和include标签搭配使用
<sql id="user_where">
<where>
<!-- where标签作用: 1、自动添加where关键字 2、去掉第一个条件的and关键字 -->
<if test="username != null and username != ''">
and username like '%${username}%'
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if>
</where>
</sql>
<select id="findUsersByNameAndSex" parameterType="org.lier.zz.User"
resultType="org.lier.zz.User">
select *from user
//调用sql条件
<include refid="user_where"></include>
</select>
3、foreach语句
应用场景:select* fromuser where id in (?,?,?) 不要使用or语句使用or语句查询速度会非常的慢
<select id="findUsersByIds" parameterType="org.lier.zz.QueryVo" resultType="org.lier.zz.User">
select * from user
<where>
<if test="ids != null">
<!--
foreach:循环传入集合参数
collection:传入集合的变量名称
item:每次循环将循环中的数据放入这个变量中
open:循环开始拼接的字符串
close:循环结束拼接的字符串
separater:循环中的分隔符
-->
<foreach collection="ids" item="id" open="id in (" close=")" separator=",">
#{id}
</foreach>
</if>
</where>
</select>
4、单个对象关系映射
场景:多个表进行映射,单个的实体无法容纳全部的结果。
方式1:将两个表对应的实体类进行平铺,如下
public class CustomOrders extends Orders{
private int uid;
private String username;
private String sex;
private Date birthday;
private String address;
…geter and seter method
}
< – 一对一:自动映射 – >
<select id="findOrderToUser" resultType="org.lier.zz.CustomOrders">
SELECT
o.*, u.id uid, username, birthday, sex, address
FROM
orders o,
user u
WHERE
o.user_id = u.id;
</select>
这是一种偷懒的做法,将相关表单的实体类进行平铺,Mybatis中是不提倡的。~_~
< – 一对一手动映射 – >
<!-- 一对一:手动映射
id:resultMap的唯一标识符
type:将查询的数据放入的对象
-->
<resultMap type="org.lier.zz.Orders" id="Order_user">
<!-- id:id标签指定主键对应关系
column:数据库字段名
property:属性,pojo中的属性名
result:指定非主键列的对应关系
-->
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="createtime" property="createtime"/>
<result column="numeber" property="number"/>
<result column="note" property="note"/>
<!--
这个标签指定单个对象的映射关系
-->
<association property="user" javaType="org.lier.zz.User">
<id column="uid" property="id"/>
<result column="username" property="username"/>
<result column="birthday" property="birthday"/>
<result column="sex" property="sex"/>
<result column="address" property="address"/>
</association>
</resultMap>
<select id="findOrderToUser2" resultMap="Order_user">
SELECT
o.*, u.id uid, username, birthday, sex, address
FROM
orders o,
user u
WHERE
o.user_id = u.id;
</select>
class文件中:
public class Orders {
private Integer id;
private Integer userId;
private String number;
private Date createtime;
private String note;
private User user;
…….geter and seter methods
}
这是Mybatis提倡的方式。
<!-- 一对多映射 -->
<resultMap type="org.lier.zz.User" id="user_orders">
<id column="id" property="id" />
<result column="username" property="username" />
<result column="birthday" property="birthday" />
<result column="sex" property="sex" />
<result column="address" property="address" />
<!--
指定集合对象关系映射
property:将数据放入User中的orders集合中
ofType:指定orders集合的泛型类型
-->
<collection property="orders" ofType="org.lier.zz.Orders">
<id column="oid" property="id" />
<result column="user_id" property="userId" />
<result column="createtime" property="createtime" />
<result column="numeber" property="number" />
<result column="note" property="note" />
</collection>
</resultMap>
<select id="findUserAndOrders" resultMap="user_orders">
select u.*,o.id
oid,user_id,number,createtime,note from user u, orders o where u.id =
o.user_id;
</select>
public class User {
private int id;
private String username;
private String sex;
private Date birthday;
private String address;
private List<Orders> orders;
…….
}