MyBatis总结
框架原理
创建sqlMapConfig.xml配置文件
这是MyBatis的全局配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties">
</properties>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<!-- <typeAlias type="com.entity.User" alias="user"/> -->
<!-- 批量定义别名 别名就是类名 -->
<package name="com.yunhe.entity"/>
</typeAliases>
<environments default="development">
<environment id="development">
<!-- 使用jdbc的事务,mybatis进行管理 -->
<transactionManager type="JDBC"/>
<!-- 指定连接获取方式-->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 读取 Mapper 配置文件中的 sql 配置
mappers 标签就是用来配置需要加载的 sql 映射配置文件路径
批量为package
加载com.yunhe.mapper路径下的sql 映射xml配置文件
-->
<package name="com.yunhe.mapper"/>
</mappers>
</configuration>
mappers 标签
映射器,用于保存所有sql相关的mapper.xml配置文件
1、用来在mybatis初始化的时候,告诉mybatis需要引入哪些Mapper映射文件
2、mapper逐个注册SQL映射文件
mybatis核心配置文件其实在配置时往往更多的使用的默认配置,大多数只需要修改数据库相关以及映射文件位置即可
注意:使用mapper接口由mybatis生成实现类的配置中要求:mapper接口与对应xml配置在同一包下且文件名相同,在相应xml配置文件中namespace值为对应的接口路径,在mapper接口中书写的抽象方法个数必须在相应xml中可以找到对应id的标签
配置sql语句核心映射文件mapper.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">
<!--
namespace:用于对当前mapper进行标识,多个mapper不能拥有相同的值(一般使用表名)
-->
<mapper namespace="com.yunhe.mapper.UserMapper">
<!--
id:和mapper接口文件中的方法名相同
parameterType:和mapper接口方法中的参数类型相同
-->
<!-- 插入 -->
<insert id="insertUser" parameterType="com.yunhe.entity.User">
insert into user values(#{id},#{username},#{birthday},#{sex},#{address});
</insert>
<!-- 查询 注意要写resultType返回结果集类型-->
<select id="selectUserById" parameterType="int" resultType="com.yunhe.entity.User">
select * from user where id = #{id};
</select>
<!-- 修改 -->
<update id="upDateByid" parameterType="user">
update user set username = #{username} where id = #{id};
</update>
<!-- 删除 -->
<delete id="deleteUserById" parameterType="int">
delete from user where id = #{id};
</delete>
</mapper>
resultMap 实现结果集的一对一映射
<!--resultMap 实现结果集的一对一映射-->
<resultMap id="orderResultMap" type="orders">
<!--订单信息的映射
column:为数据库中的数据属性名,可在sql语句中进行修改
property:为实体类中的属性名
在Orders中添加User属性
-->
<id column="id" property="id"></id>
<result column="user_id" property="user_id"></result>
<result column="createtime" property="createtime"></result>
<result column="note" property="note"></result>
<result column="number" property="number"></result>
<!--用户信息的映射-->
<!--
将结果集映射为一个对象
property:user对象在Orders类中的属性名
javaType:指定对象的类型
association为Orders中的user属性
-->
<association property="user" javaType="user">
<result column="username" property="username"></result>
<result column="address" property="address"></result>
<result column="sex" property="sex"></result>
</association>
</resultMap>
<select id="findOrderAndUser2" resultMap="orderResultMap">
select a.username,a.sex,a.address,b.* from user a,orders b where a.id = b.user_id;
</select>
将结果集映射为一个对象(或集合)
column | 为数据库中的数据属性名,可在sql语句中进行修改 |
---|---|
property | 为实体类中的属性名 |
javaType | 指定对象的类型 |
association | 结果集中对象属性的映射 |
---|---|
collection | 结果集中集合属性的映射 |
定义resultMap完成结果集的一对多的映射
<!--定义resultMap完成结果集的一对多的映射-->
<resultMap id="userOrderResultMap" type="user" extends="userResultMap">
<!--
ofType:集合中元素的java类型
collection:结果集中集合属性的映射
一对多映射:在一方(user)添加多方为属性(List<Orders>)(集合属性)
-->
<collection property="orders" ofType="orders">
<!--完成订单信息的映射-->
<id column="oid" property="id"></id>
<result column="createtime" property="createtime"></result>
<result column="number" property="number"></result>
<result column="user_id" property="user_id"></result>
</collection>
</resultMap>
<!--查询用户和用户对应的订单信息-->
<select id="findUserAndOrders" resultMap="userOrderResultMap">
select a.*,b.id oid,b.user_id,b.createtime,b.note,b.number from user a,orders b where a.id = b.user_id;
</select>
resultMap 实现结果集的多对多映射
<!--
resultMap 实现结果集的多对多映射
订单中(orders)包含多个订单信息( List<orderdetail> ) ,
订单信息List<orderdetail>中包含商品信息(items)
-->
<resultMap id="orderAndItemsResultMap" type="orders">
<!--订单信息(orders)的映射-->
<id column="oid" property="id"></id>
<result column="number" property="number"></result>
<result column="createtime" property="createtime"></result>
<!--订单详情信息( List<orderdetail> )的映射,多个,所以为集合-->
<collection property="ods" ofType="orderdetail">
<id column="bid" property="id"></id>
<result column="items_id" property="items_id"></result>
<result column="items_num" property="items_num"></result>
<!--商品信息的映射-->
<association property="items" javaType="items">
<id column="cid" property="id"></id>
<result column="name" property="name"></result>
<result column="price" property="price"></result>
<result column="detail" property="detail"></result>
<result column="ctime" property="createtime"></result>
</association>
</collection>
</resultMap>
<!--查询订单和订单对应的商品信息-->
<select id="selectOrderAndItems" resultMap="orderAndItemsResultMap">
select a.id oid,a.number,a.createtime,b.id bid,b.items_id,b.items_num,c.id cid,c.name,c.price,c.detail,c.createtime ctime
from orders a,orderdetail b,items c where a.id = b.orders_id and b.items_id = c.id;
</select>
property属性:用于标识返回对象指定属性名
column属性:用于标识查询数据列(会获取对应列数据传入相应的查询语句)
select属性:懒加载执行时调用获取数据的方法(可以调用其他mapper中的方法,通过namespace.id使用)
fetchType属性:懒加载属性可以修改局部懒加载配置
ofType属性:用于标识集合存储数据类型(要求select语句返回结果与之对应)
动态Sql
懒加载
<resultMap id="userOrderResultMap1" type="user" extends="userResultMap">
<!--用户信息映射-->
<!---colleaction实现延迟加载-->
<collection property="orders" ofType="orders" select="findOrdersByUid" column="id">
</collection>
</resultMap>
<!--使用延迟加载 查询用户和订单信息-->
<select id="findUserAndOrder" resultMap="userOrderResultMap1">
select * from user
</select>
<!--根据用户id查询订单-->
<select id="findOrdersByUid" parameterType="int" resultType="orders">
select * from orders where user_id = #{id}
</select>
SqlSession接口
加载配置文件,获取SqlSessionFactory
public class DaoUtils {
private static SqlSessionFactory sf = null;
public DaoUtils() {
}
static {
SqlSessionFactoryBuilder b = new SqlSessionFactoryBuilder();
//读取配置文件
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
sf = b.build(inputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static SqlSessionFactory getFactory(){
return sf;
}
}
测试类
public void Test2() {
SqlSessionFactory sf = DaoUtils.getFactory();
//获取session
SqlSession session = sf.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.selectUserById(1);
System.out.println(user);
session.commit();
session.close();
}