1.概要设计(建表,建实体)

2.设计 保存1个部门和n个员工的页面
jQuery
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><script src="js/jquery-3.3.1.min.js"></script><style>dt,dd{margin-top: 8px;margin-bottom: 8px;}</style><script>$(function () {$('#btn').click(function () {$('#addBox').append('<dd>员工名称:<input name="ename" required="required" /> 员工工资:<input name="sal" required="required" /><input type="button" class="deleteBtn" value="x"></dd>');//为刚刚新增加那行的按钮(最后一个按钮)设置实践$('.deleteBtn:last').click(function (){$(this).parent().remove();})});});</script></head><body><form action="saveDept" method="post"><dl id="addBox"><dt> <button>保存</button> <input id="btn" type="button" value="增加员工" /></dt><dt>部门名称:<input name="dname" required="required" /> 部门位置:<input name="loc" required="required" /></dt><dd>员工名称:<input name="ename" required="required" /> 员工工资:<input name="sal" required="required" /></dd></dl></form></body></html>
3.级联保存
先保存部门,获得自增主键值
结合获得的部门主键值,保存员工,获得员工的自增主键值
基于dao接口,通过spring注入获得接口的实现类/操作类/代理类对象
注解配置事务处理
使用spring提供的一个编码过滤器,请求中文编码处理
4.关联查询(查询所有部门及其对应员工信息)
DeptController
package com.duyi.controller;import com.duyi.domain.Dept;import com.duyi.service.DeptService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import java.util.Arrays;import java.util.List;@Controllerpublic class DeptController {@Autowiredprivate DeptService deptService;@RequestMapping(value="saveDept",produces = "text/html;charset=utf-8")@ResponseBodypublic String saveDept(String dname,String loc,String[] ename,Float[] sal){System.out.println(dname+"---"+loc);System.out.println(Arrays.toString(ename));System.out.println(Arrays.toString(sal));deptService.saveDept(dname,loc,ename,sal);return "保存成功";}@RequestMapping("deptList")@ResponseBodypublic List<Dept> deptList(){// List<Dept> deptList=deptService.findAll();// for(Dept d:deptList){// System.out.println(d.getDname());// }return deptService.findAll();}}
DeptService
package com.duyi.service;import com.duyi.dao.DeptDao;import com.duyi.dao.EmpDao;import com.duyi.domain.Dept;import com.duyi.domain.Emp;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Isolation;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import java.util.List;import java.util.Random;@Servicepublic class DeptService {@Autowiredprivate DeptDao deptDao;@Autowiredprivate EmpDao empDao;//开启事务处理@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)public void saveDept(String dname,String loc,String[] ename,Float[] sal){Random random=new Random();Dept dept=new Dept(random.nextInt(100)+40,dname,loc,null);//假设主键是自增时,保存dept,同时获得自增的组件deptDao.save(dept);//假设主键是自增时,保存前dept对象中deptno为null,保存后有了具体数值for (int i=0;i< ename.length;i++){Emp emp=new Emp(i+1,ename[i],sal[i],dept.getDeptno(),null);empDao.save(emp);}}public List<Dept> findAll(){return deptDao.findAll();}}
EmpDao
package com.duyi.dao;import com.duyi.domain.Emp;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Select;import java.util.List;public interface EmpDao {@Insert("insert into myemp values(#{empno},#{ename},#{sal},#{deptno})")public void save(Emp emp);@Select("select * from myemp where deptno = #{deptno}")public List<Emp> selectEmps(Integer deptno);}
DeptDao
package com.duyi.dao;import com.duyi.domain.Dept;import com.duyi.domain.Emp;import org.apache.ibatis.annotations.*;import java.util.List;public interface DeptDao {@Insert("insert into dept values(#{deptno},#{dname},#{loc})")//保存dept,同时获得自增的组件//@Options(useGeneratedKeys = true,keyProperty = "deptno")public void save(Dept dept);@Select("select * from dept")@Results({@Result(property = "deptno",column = "deptno",id = true),@Result(property = "dname",column = "dname"),@Result(property = "loc",column = "loc"),@Result(property = "emps",column = "deptno",many=@Many(select = "com.duyi.dao.EmpDao.selectEmps")),})public List<Dept> findAll();}
mybat.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><mappers><mapper class="com.duyi.dao.DeptDao"></mapper><mapper class="com.duyi.dao.EmpDao"></mapper></mappers></configuration>
deptList.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><script src="js/jquery-3.3.1.min.js"></script><script>$(function () {$.post('deptList',{},function (depts){for(var i=0;i<depts.length;i++){var dept=depts[i];$('#showBox').append('<dt>'+dept.dname+'</dt>');for (var j=0;j<dept.emps.length;j++){var emp=dept.emps[j];$('#showBox').append('<dd>'+emp.ename+','+emp.sal+'</dd>');}}},'json');});</script></head><body><h2>部门信息列表</h2><dl id="showBox"></dl></body></html>
