<configuration>
<!-- 配置数据源 -->
<environments default="mysqldb">
<environment id="mysqldb">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///mydb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
</configuration>
映射文件 书写sql语句
<mapper namespace="cn.tedu.UserMapper">
<select id="select01" resultType="cn.tedu.domain.User">
select * from user;
</select>
</mapper>
名称空间 + id 确定唯一sql语句 结果类型
映射到配置文件中
<mappers>
<mapper resource="userMapper.xml"/>
</mappers>
调用过程
public void test01() throws Exception {
//1.创建SqlSessionFactory
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//2.创建SqlSession
SqlSession session = factory.openSession();
//3.调用sql得到结果
List<User> list = session.selectList("cn.tedu.UserMapper.select01");
//4.处理结果
System.out.println(list);}
除了查询之外,增删改都需要 session.commit();
手动映射结果集:
若数据库中的列名与要封装的bean的属性名不一致,则需要手动映射结果集
<resultMap type=cs.tedu.domain.User2 id=rm01>
主键配置,必须配置
<id column=id property=uid>
其他属性配置,要用result
<result column=name property=uname>
</resultMap>
<select id=select01 resultMao= rm01>
多表设计与查询
mysql语句中多表查询
笛卡尔积查询:select * from emp,dept;
内连接查询:
select * from dept,emp where dept.id=emp.ud;
select * from dept inner jion emp on dept.id=emp.id
外连接查询:
左外:select * from dept left jion emp on dept.id=emp.id;
右外:select * from depr right jion emp on dept.id=emp.id;
全外连接查询:
Select * from dept right jion emp on dept.id= emp.id
union
select * from reigt join emp on dept.id=emp.id;
MyBatis
一对一查询:
需要手动设置结果集映射
<resultMap type=”cn.tedu.domain.Grade” id=”rm01”>
<id column=”gid” property=”id”/>
<result column=”gname” propertu=”name”/>
<association property=”room” javatype=”cn.tedu.....”>
<id column=”rid” property=”id”/>
<result column=”rname” propertu=”name”/>
</association>
</requltMap>
//如果bean中包含了对象,则用association
Select grade.id as gid,…
一对多查询:
<resultMap type="cn.tedu.domain.Teacher" id="rm01">
<id column="tid" property="id"/>
<result column="tname" property="name"/>
<collection property="stuList" ofType="cn.tedu.domain.Stu">
<id column="sid" property="id"/>
<result column="sname" property="name"/>
</collection>
</resultMap>
//如果bean中包含了集合属性,则用collection
<select id="m2m1" resultMap="rm01">
select
stu.id as sid,
stu.name as sname,
teacher.id as tid,
teacher.name as tname
from
stu
inner join stu_teacher on stu.id = stu_teacher.sid
inner join teacher on teacher.id = stu_teacher.tid
多对多查询:
<resultMap type="cn.tedu.domain.Stu" id="rm02">
<id column="sid" property="id"/>
<result column="sname" property="name"/>
<collection property="teacherList" ofType="cn.tedu.domain.Teacher">
<id column="tid" property="id"/>
<result column="tname" property="name"/>
</collection>
</resultMap>
<select id="m2m2" resultMap="rm02">
select
stu.id as sid,
stu.name as sname,
teacher.id as tid,
teacher.name as tname
from
stu
inner join stu_teacher on stu.id = stu_teacher.sid
inner join teacher on teacher.id = stu_teacher.tid
</select>
Mybatis细节
别名标签:如果在映射文件中,大量使用类名比较长,可以在sqlMapConfig.xml声明别名,在映射文件中可以使用别名缩短配置。
在sqlMapConfig.xml中配置,注意此配置要放在最前面
<typeAliases>
<typeAlais type=”...............” alias=”...”>
</typeAliases>
Sql的复用
如果某段sql语句的片段在映射文件中重复出现,可以将其单独配置为一个引用,从而在需要时直接引用,减少配置量
MyBatis的缓存机制
一级缓存:
缓存只在一个事务中有效,即同一个事务中先后执行多次同一个查询,只在第一次真正去查库。
二级缓存:
缓存在全局有效,一个事务查询一个sql得到结果,会被缓存起来,之后只要缓存未被清除,则其他事务如果查询同一个sql,得到的将会是之前缓存的结果。
在sqlMapConfig.xml配置:
在映射文件中配置:
想要被二级缓存缓存的bean必须实现序列化接口:
接口实现
为了简化MyBatis的使用,MyBatis提供了接口方式 自动化 生成调用过程的机制,可以大大简化MyBatis的开发
接口有4点要实现
1.接口名称要和名称空间一致
2.映射文件中有几个sql id,接口中就要有几个方法,且名称要一致
3.方法接受的参数应该和sql中接受的参数一致
4.方法返回值要和sql语句的返回值一致
测试类:
真正的开发中,都是使用这种接口+配置文件方式,实现MyBatis的使用。
实现原理: