一、创建项目
- 1.导包:
Mybatis 核心包
Mysql 驱动包
junit 测试包
2.添加核心配置文件
jdbc.properties:
mybatis-config.xmljdbc.username=root jdbc.password=admin jdbc.url=jdbc:mysql:///mybatis jdbc.driverClassName=com.mysql.jdbc.Driver
<?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>
<!-- 加载jdbc.properties-->
<properties resource="jdbc.properties"/>
<!--申明操作数据库的环境-->
<environments default="MYSQL">
<environment id="MYSQL">
<!--使用jdbc的事务-->
<transactionManager type="JDBC"/>
<!--支持连接池-->
<dataSource type="POOLED">
<!--自动补全结构:ctrl+shift+回车-->
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
<property name="driver" value="${jdbc.driverClassName}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 加载mapper.xml文件-->
</mappers>
</configuration>
注意:
测试的时候需要在mybatis-config.xml文件中加载mapper.xml文件
二、Mysql动态标签
foreach标签
<?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的内容就是ProductMapper接口的全限定名-->
<mapper namespace="cn.itsource._01_batch.mapper.ProductMapper">
<insert id="insert">
insert into product(name,price) values(
#{name},#{price}
)
</insert>
<!--如果传过来的参数是集合或者数组,mybatis会自动地把参数封装成一个Map<K,V>
V:就是我们的参数对象List<Product>
K:如果是集合,k的值是“list”;如果是数组,K的值是"array"
-->
<!--foreach标签:mybatis的遍历手段
collection:要遍历的参数名字(集合是list,数组是array)
item:循环变量,名字随便起,
separator:分隔符-->
<insert id="insertBatch">
INSERT INTO product (name, price)
VALUES
<foreach collection="list" item="p" separator=",">
(#{p.name},#{p.price})
</foreach>
</insert>
<!--open:表达式以什么符号开始,
close:表达式以什么符号结束-->
<delete id="deleteBatch">
delete from product where id in
<foreach collection="list" item="i" separator="," open="(" close=")">
#{i}
</foreach>
</delete>
<!-- List<Product> findAll();-->
<select id="findAll" resultType="cn.itsource._01_batch.domain.Product">
select * from product
</select>
</mapper>
标签中的属性:
collection=“list”,表示使用的是list来接收,还可以使用数组array
separator=",",分割符号
item=“p”,遍历的对象的别名,可以任取
index,获取当前遍历索引(一般使用比较少)
open="(",close=")":以什么开始,以什么结束
三、域和对象
1.关联关系:
一对一:如一个员工对应一张身份证
多对一:如多个员工对应多个部门
一对多:如多个部门对应一个员工
多对多:如一个员工拥有不同的职责对应不同的部门
2.结果映射
多对一:
结果嵌套:发送一条Sql查询所有的信息(关联对象+本身)
<resultMap id="employeeResultMap" type="cn.itsource._02_many2one.domain.Employee">
<id column="id" property="id"/>
<result column="name" property="name"/>
<association property="department"
javaType="cn.itsource._02_many2one.domain.Department">
<id column="did" property="id"/>
<result column="dname" property="name"/>
</association>
</resultMap>
<!--结果嵌套-->
<select id="findAll" resultMap="employeeResultMap">
select emp.*,d.id did,d.name dname from employee emp JOIN department d ON emp.dept_id = d.id
</select>
查询嵌套:发送多条Sql查询
<resultMap id="employeeResultMap" type="cn.itsource._02_many2one.domain.Employee">
<id column="id" property="id"/>
<result column="name" property="name"/>
<association property="department" javaType="cn.itsource._02_many2one.domain.Department"
column="dept_id" select="findDeptById">
</association>
</resultMap>
<!--查询嵌套-->
<select id="findAll" resultMap="employeeResultMap">
select * from employee
</select>
<select id="findDeptById" resultType="cn.itsource._02_many2one.domain.Department">
select * from department where id=#{id}
</select>
</mapper>
resultMap:专门配置结果映射的标签,里面配置了实体的属性和表字段的映射关系
association:就是关联的意思,多对一使用
association中的property:关联的属性名
javatype:关联的属性的类型
column:使用哪个列作为参数去执行查询
select:查询部门的SQL,规范写法是namespace+查询id,如果在同一个namespace里面可以简写
一对多:
结果嵌套:
<resultMap id="departmentResultMap"
type="cn.itsource._03_one2many.domain.Department">
<id column="id" property="id"/>
<result column="name" property="name"/>
<collection property="employees" ofType="cn.itsource._03_one2many.domain.Employee">
<id column="eid" property="id"/>
<result column="ename" property="name"/>
</collection>
</resultMap>
<!--结果嵌套-->
<select id="findAll" resultMap="departmentResultMap">
select d.*,e.id eid,e.name ename from department d left join employee e ON d.id = e.dept_id
</select>
查询嵌套:
<!--表示DepartmentMapper里面的所有SQL开启了二级缓存-->
<cache/>
<resultMap id="departmentResultMap"
type="cn.itsource._03_one2many.domain.Department">
<id column="id" property="id"/>
<result column="name" property="name"/>
<collection property="employees" ofType="cn.itsource._03_one2many.domain.Employee"
column="id" select="findEmpByDeptId">
</collection>
</resultMap>
<!--查询嵌套,效率要优于结果嵌套,懒加载-->
<select id="findAll" resultMap="departmentResultMap">
select * from department
</select>
<select id="findEmpByDeptId" resultType="cn.itsource._03_one2many.domain.Employee">
select * from employee where dept_id=#{id}
</select>
注意:一对多使用的关联标签是collection 属性的类型标签是oftype
多对多:
四、Mybatis的缓存
Mybatis支持缓存
一级缓存:SqlSession级别缓存,缓存对象存储周期为第一次获取,到sqlsession被销毁掉,或是sqlSession().clearCache();
二级缓存:SqlSessionFactory级别缓存,缓存对象存储周期为第一次获取,到SqlSessionFactory被销毁掉(应用停止了)
实现缓存 :
一级缓存:默认开启
二级缓存:
1. 在主配置文件中;
2. 在Mapper.xml添加一个标签