1、domain实体类
Employee:
public class Employee {
private Long id;
private String username;
private Integer sex;
private Integer age;
//维护关系:多个员工对应一个部门
private Dept dept;
}
Dept:
public class Dept {
private Long id;
private String name;
private String sn;
}
2、创建Dao接口及实现类:编写CRUD并实现
3、创建Mapper映射器:编写CRUD方法
4、Mapper的映射文件
新增:
因为员工实体类包含对象所以需要使用对象 . 属性的方式
<insert id="insert" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO
employee(username,age,sex,dept_id)
VALUES
(#{username},#{age},#{sex},#{dept.id})
</insert>
测试:
新增部门后将部门Id设置给员工的dept属性
@Test
public void insert() {
Dept dept = new Dept();
dept.setId(4L);
Employee employee = new Employee("哈喽",1,18,dept);
employeeDao.insert(employee);
}
修改:
<update id="updte">
UPDATE employee
<set>
<if test="username != null">
username=#{username},
</if>
<if test="sex != null">
sex=#{sex},
</if>
<if test="age != null">
age=#{age},
</if>
<if test="dept != null and dept.id != null">
dept_id = #{dept.id},
</if>
</set>
where id=#{id}
</update>
测试:
@Test
public void testUpdate() throws Exception{
Dept dept = new Dept();
dept.setId(5L);
Employee employee = new Employee("哈喽",1,12,dept);
employee.setId(12L);
employeeDao.updte(employee);
}
5、嵌套结果查询
查询的SQL使用JOIN连表的方式把要查询的内容employee和dept全部查询出来 ,然后使用ResultMap处理结果。在处理结果时,由于两个表中都具有id属性,所以需要使用别名来进行映射,必须使用as来取别名
e.id AS e_id
因为employee中的dept是一个对象,所以需要使用< association >元素来映射
property=“dept” :表示需要处理哪一个字段,表示对员工的dept属性进行映射
javaType=“cn.yinsh.domain.Dept”:表示这个字段的类型,员工的dept属性的类型
< association>里面的字段映射内容跟通常的映射方式一样。
如果使用了别名,在映射的时候:column=“别名”
<resultMap id="baseResultMap" type="cn.yinsh.domain.Employee">
<id column="e_id" property="id"/>
<result column="username" property="username"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
<association property="dept" javaType="cn.yinsh.domain.Dept">
<id column="d_id" property="id"/>
<result column="name" property="name"/>
<result column="sn" property="sn"/>
</association>
</resultMap>
ResultType只能根据查询的列把对应的值封装到实体类的属性中,Employee中的Dept是一个自定义的字段,如果查询的列和对象中的属性名不一致,就需要用到resultMap
<select id="selectAll" resultMap="baseResultMap">
SELECT
e.id AS e_id,e.username,e.age,e.sex,d.id AS d_id,d.name,d.sn
FROM
employee e join dept d on d.id = e.dept_id
</select>
6、嵌套查询
查询SQL的时候,只需要查询employee表即可,不需要去连表查询Dept,而是在ResultMap中另外发送一条子SQL去查询emloyee关联的dept的数据,然后映射给Employee。
在resultMap处理结果集的时候,使用了
< association> 来映射dept
- select=“cn.yinsh.mapper.DeptMapper.selectById” : 是指额外发送SQL来查询当前employee表关联的dept表
- column=“dept_id” :是指在另外发送的SQL中的参数使用 employee表中的dept_id为查询条件
<resultMap id="baseResultMap" type="cn.yinsh.domain.Employee">
<id column="e_id" property="id"/>
<result column="username" property="username"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
<association
property="dept"
javaType="cn.yinsh.domain.Dept"
column="dept_id"
select="cn.yinsh.mapper.DeptMapper.selectById"/>
</resultMap>
查询时只查询了一张表
<select id="selectAll" resultMap="baseResultMap">
SELECT
e.id,e.username,e.sex,e.age,e.dept_id
FROM
employee e
</select>
另外发送的sql需要根据ID来查询
deptMapper.xml
<select id="selectById" resultType="cn.yinsh.domain.Dept">
SELECT id,name,sn FROM dept WHERE id=#{id}
</select>
7、一对多
① domain实体类:
product
public class Product {
private Long id;
private String productName;
private Long productTypeId;
}
productType
public class ProductType {
private Long id;
private String name;
//维护关系:一方
private List<Product> products = new ArrayList<>();
}
② 创建Dao层接口以实现类:CRUD
③ Mapper映射器&Mapper映射文件:CRUD
④ 将mapper映射文件注册到核心配置文件中
<mappers>
<mapper resource="cn/yinsh/mapper/DeptMapper.xml"/>
<mapper resource="cn/yinsh/mapper/EmployeeMapper.xml"/>
<mapper resource="cn/yinsh/mapper/ProductMapper.xml"/>
<mapper resource="cn/yinsh/mapper/ProductTypeMapper.xml"/>
</mappers>
④ 测试
@Test
public void insert() {
//创建商品类型为:键盘
ProductType productType = new ProductType();
productType.setName("键盘");
productTypeDao.insert(productType);
//一对多维护关系,多个商品为一种商品类型,在商品类型(一方)中使用list集合存放商品(多方)
productType.setProducts(Arrays.asList(
new Product("罗技K380",productType.getId()),
new Product("海盗船K68RGB",productType.getId()),
new Product("海盗船K70RGMK.2iG限量版 ",productType.getId())
));
//保存数据,首先获取设置的Products(list集合),然后遍历的方式将数据插入Product表中
productType.getProducts().forEach(product -> productDao.insert(product));
}