sqlMapConfig.xml---全局配置文件(数据源、映射文件等)
mapper.xml
sqlSessionFactory(会话工厂)
sqlSession(会话:增删改查)
Executor(执行器 sqlsession内部通过执行器操作数据库)
输入参数(java、hashmap、pojo)---> mapped statement(底层封装对象 包括sql语句、输入输出) --->输出参数(java、hashmap、pojo)
一、导入所有jar + mysql-connector.jar
二、创建SqlMapConfig.xml
#{}表示占位符号,接收简单类型名称任意,接收pojo通过ognl获得值
${}表示拼接符号,会引起sql注入,接收简单类型时只能为value
resultType
使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。
如果查询出来的列名和pojo中的属性名全部不一致,没有创建pojo对象。
只要查询出来的列名和pojo中的属性有一个一致,就会创建pojo对象。
resultMap
如果查询出来的列名和pojo的属性名不一致(sql语句中使用了别名),
通过定义一个resultMap对列名和 pojo属性名之间作一个映射关系
id表示唯一标识
column表示查询出来的列名
property表示pojo类中的属性名
主键返回:
select last_insert_id()
select uuid()
动态sql拼接查询条件
<select id="findUserList" parameterType="userQueryVo" resultType="userCustom">
select * from user
<where>
<if test="userCustom!=null">
<if test="userCustom.sex!=null and userCustom.sex!=''"> and sex=#{userCustom.sex} </if>
<if test="userCustom.username!=null and userCustom.username!=''"> and username like '%${userCustom.username}%' </if>
</if>
</where>
</select>
动态sqlforeach
<if test="ids!=null">
<foreach collection="ids" item="user_id" open="and (" close=")" separator="or">
id=#{user_id}
</foreach>
</if>
一对一查询resultType(order 和 user)
一、ordercustom 继承 order,添加username、sex等属性
二、编写接口 public List<OrderCustom> findOrderUser();
三、编写mapper
<select id="findOrdersUser" resultType="OrdersCustom">
select orders.*, user.username, user.sex, user.address
from orders,user where orders.user_id=user.id
<!-- 内连接 -->
</select>
一对一查询使用resultMap(order 和 user)
使用resultMap将查询结果中的订单信息映射到Orders对象中,在orders类中添加User属性,
将关联查询出来的用户信息映射到orders对象中的user属性中(也就是不需要OrderCustom了)
一、添加属性
二、编写接口public List<Orders> findOrdersUserResultMap();
三、编写mapper
<resultMap type="cn.itcast.po.Orders" id="ordersUserResultMap">
<!-- 配置映射的订单信息 -->
<!-- id:指定查询列中的唯 一标识,订单信息的中的唯 一标识,如果有多个列组成唯一标识,配置多个id
column:订单信息的唯 一标识 列 property:订单信息的唯 一标识 列所映射到Orders中哪个属性 -->
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="number" property="number"/>
<result column="createtime" property="createtime"/>
<result column="note" property="note"/>
<!-- 配置映射的关联的用户信息 -->
<!-- association:用于映射关联查询单个对象的信息 property:要将关联查询的用户信息映射到Orders中哪个属性 -->
<association property="user" javaType="cn.itcast.po.User">
<!-- id:关联查询用户的唯 一标识 column:指定唯 一标识用户信息的列 javaType:映射到user的哪个属性 -->
<id column="user_id" property="id"/> <result column="username" property="username"/>
<result column="sex" property="sex"/> <result column="address" property="address"/>
</association>
</resultMap>
<select id="findOrdersUserResultMap" resultMap="ordersUserResultMap">
select orders.*, user.username, user.sex, user.address from orders,user where orders.user_id=user.id
</select>
实现一对一查询总结:
resultType:使用resultType实现较为简单,如果pojo中没有包括查询出来的列名,需要增加列名对应的 属性,即可完成映射。
如果没有查询结果的特殊要求建议使用resultType。
resultMap:需要单独定义resultMap,实现有点麻烦,如果对查询结果有特殊的要求,使用resultMap可以 完成将关联查询映射pojo的属性中。
resultMap可以实现延迟加载,resultType无法实现延迟加载
一对多resultMap
public List<Orders> findOrdersAndOrderDetailResultMap()throws Exception;
<resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersAndOrderDetailResultMap" extends="OrdersUserResultMap">
<!-- 订单信息 -->
<!-- 用户信息 -->
<!-- 使用extends继承,不用在中配置订单信息和用户信息的映射 -->
<!-- 订单明细信息 一个订单关联查询出了多条明细要使用collection进行映射
collection:对关联查询到多条记录映射到集合对象中
property:将关联查询到多条记录映射到cn.itcast.mybatis.po.Orders哪个属性
ofType:指定映射到list集合属性中pojo的类型
-->
<collection property="orderdetails" ofType="cn.itcast.mybatis.po.Orderdetail">
<!-- id:订单明细唯 一标识 property:要将订单明细的唯 一标识 映射到cn.itcast.mybatis.po.Orderdetail的哪个属性 -->
<id column="orderdetail_id" property="id"/>
<result column="items_id" property="itemsId"/>
<result column="items_num" property="itemsNum"/>
<result column="orders_id" property="ordersId"/>
</collection>
</resultMap>
<!-- 查询订单关联查询用户及订单明细,使用resultmap -->
<select id="findOrdersAndOrderDetailResultMap" resultMap="OrdersAndOrderDetailResultMap">
SELECT orders.*, USER.username, USER.sex, USER.address, orderdetail.id orderdetail_id, orderdetail.items_id, orderdetail.items_num, orderdetail.orders_id
FROM orders, USER, orderdetail WHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id
</select>
一对多使用resultType,会有重复记录
多对多resultMap(用户商品)
同理,使用collection来映射
多对多resultType
同理,有重复
延迟加载
resultMap可以实现高级映射(使用association、collection实现一对一及一对多映 射),association、collection具备延迟加载功能。
使用association中的select指定延迟加载去执行的statement的id
缓存
mybatis默认支持一级缓存(sqlSession级别)
二级缓存需要配置(sqlsessionFactory级别)