-- 使用`workordermanager`数据库
USE `workordermanager`;
-- 查询`project`项目组表
SELECT * FROM `project`;
-- 查询`workorder`项目组表
SELECT * FROM `workorder`;
-- 使用`workordermanager`数据库
USE `workordermanager`;
-- 查询`project`项目组表
SELECT * FROM `project`;
-- 查询`workorder`工单表
SELECT * FROM `workorder`;
-- 主外键查询工单表,关联项目名
SELECT w.`id`,p.`projectName`,w.`executor`,w.`description`,w.`orderLevel`,w.`createDate`
FROM `workorder` AS w,`project` AS p
WHERE w.`progectId` =p.`id` ORDER BY createDate DESC;
-- 添加一条工单
INSERT INTO workorder (projectId,executor,description,orderLevel,createDate)
VALUES (1,'李小明','完成在线答疑任务',2,'2020-03-04');
————————————————————————————————————————
main下创建java,resources,test文件夹,并Make Driectory as
webapp下导入4个文件夹
java下创建cn.com包下,创建pojo实体类包,dao接口包,service层接口包
controller控制类包,inteceptor拦截器控制类包
开始编码!
1.底层Model层
(Spring容器,pojo持久化类,Dao层的接口Mapper映射,Service层)
编写springmvc-config.xml Spring容器文件
pojo下创建持久化类(输出,构造方法,get/set,toString)
Project 项目组表类
//Project 项目组表类
public class Project {
private int id; //项目编号
private String projectName; //项目名称
//toString输出
@Override
public String toString() {
return "Project{" +
"id=" + id +
", projectName='" + projectName + '\'' +
'}';
}
//无参构造方法
public Project(){}
//带参构造方法
public Project(int id, String projectName) {
this.id = id;
this.projectName = projectName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
}
Workorder 工单表类
//Workorder 工单表类
public class Workorder {
private int id; //工单编号
private int projectId; //项目组编号
private String executor; //执行人
private String description; //任务描述
private int orderLevel; //工单级别
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date createDate; //创建时间
private Project project; //Project项目组表引用类型(项目编号)
@Override
public String toString() {
return "Workorder{" +
"id=" + id +
", project=" + project.getProjectName() +
", executor='" + executor + '\'' +
", description='" + description + '\'' +
", orderLevel='" + orderLevel + '\'' +
", createDate=" + createDate +
'}';
}
//无参构造方法
public Workorder(){}
//带参构造方法
public Workorder(int projectId, String executor, String description, int orderLevel, Date createDate) {
this.projectId = projectId;
this.executor = executor;
this.description = description;
this.orderLevel = orderLevel;
this.createDate = createDate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public String getExecutor() {
return executor;
}
public void setExecutor(String executor) {
this.executor = executor;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getOrderLevel() {
return orderLevel;
}
public void setOrderLevel(int orderLevel) {
this.orderLevel = orderLevel;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
}
Dao下创建Mapper.java接口,Mapper.xml映射文件
//ProjectDao 项目组表类Dao层接口
public interface ProjectMapper {
//查询所有项目组
public List<Project> getAllProject();
}
<?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">
<mapper namespace="cn.com.dao.ProjectMapper"> <!--本映射器文件路径-->
<!--SQL映射文件-->
<!--查-->
<!--查所有项目组-->
<!--SELECT * FROM `project`;-->
<select id="getAllProject" resultType="Project">
SELECT * FROM `project`;
</select>
</mapper>
//WorkorderDao 工单表类Dao层接口
public interface WorkorderMapper {
//查询所有工单
public List<Workorder> getAllWorkorder();
//添加工单(单条)
//@param("注解名")
public void addWorkorder(Workorder workorder);
}
<?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">
<mapper namespace="cn.com.dao.WorkorderMapper"> <!--本映射器文件路径-->
<!--接口映射文件-->
<!--查-->
<!--查所有工单-->
<!--SELECT *FROM `smbms_provider`;-->
<select id="getWorkorder" resultType="Workorder">
SELECT * FROM `workorder`;
</select>
<!--主外键查询工单表,关联项目名-->
<!--resultMap重新映射,用于结果集和DB字段不一致,和连接查询的解决办法,重新映射关联-->
<!--使用Project对象部门类对象,于连接查询,映射关联,找到里面的projectName-->
<resultMap id="WorkorderResult" type="Workorder">
<!--<result映射 result的DB结果集名 column是pojo里的类属性名></result>-->
<result property="id" column="id"></result>
<result property="projectId" column="projectId"></result>
<result property="executor" column="executor"></result>
<result property="description" column="description"></result>
<result property="orderLevel" column="orderLevel"></result>
<result property="createDate" column="createDate"></result>
<!--复杂类型,级联-->
<!--<association引用类型 property对象名 javaType是pojo类名-->
<association property="project" javaType="Project">
<!--<result映射表2 property的DB结果集名 column是pojo里的类属性名></result>-->
<result property="id" column="id"></result>
<result property="projectName" column="projectName"></result>
</association>
</resultMap>
<select id="getAllWorkorder" resultMap="WorkorderResult">
SELECT w.`id`,p.`projectName`,w.`executor`,w.`description`,w.`orderLevel`,w.`createDate`
FROM `workorder` AS w,`project` AS p
WHERE w.`projectId` =p.`id` ORDER BY createDate DESC;
</select>
<!--添加工单(一条)-->
<insert id="addWorkorder" parameterType="Workorder">
INSERT INTO workorder (projectId,executor,description,orderLevel,createDate)
VALUES (#{projectId},#{executor},#{description},#{orderLevel},#{createDate});
</insert>
</mapper>
test下创建TestDao.java
TestDao Dao层测试
//TestDao Dao层测试
public class TestDao {
private Logger logger = Logger.getLogger(TestDao.class);
//Dao查询所有项目组
@Test
public void getAllProject(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("springmvc-config.xml");
ProjectMapper projectMapper = (ProjectMapper) ctx.getBean("projectMapper");
List<Project> projects = projectMapper.getAllProject();
for(Project p:projects){
logger.info(p.toString());
}
}
//Dao查询所有工单
@Test
public void getAllWorkorder(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("springmvc-config.xml");
WorkorderMapper workorderMapper = (WorkorderMapper) ctx.getBean("workorderMapper");
List<Workorder> workorders = workorderMapper.getAllWorkorder();
for(Workorder w:workorders){
logger.info(w.toString());
}
}
//Dao添加工单(一条),并查询
@Test
public void addWorkorder(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("springmvc-config.xml");
WorkorderMapper workorderMapper = (WorkorderMapper) ctx.getBean("workorderMapper");
Date data = new Date();
Workorder workorder = new Workorder
(1,"李小明","完成在线答疑任务",2,new Date());
workorderMapper.addWorkorder(workorder);
getAllWorkorder();
}
}
ProjectService 项目组表Service层接口
//ProjectService 项目组表Service层接口
public interface ProjectService {
//查询所有项目组
public List<Project> getAllProjectService();
}
ProjectServiceImpl 项目组表Service层接口实现类
//ProjectServiceImpl 项目组表Service层接口实现类
@Service("projectService")
public class ProjectServiceImpl implements ProjectService {
//注入Dao层接口
@Autowired
private ProjectMapper projectMapper;
//注入service层接口(用于Controller)
@Autowired
private ProjectService projectService;
@Override
public List<Project> getAllProjectService() {
return projectMapper.getAllProject();
}
}
WorkorderService 工单表Service层接口
//WorkorderService 工单表Service层接口
public interface WorkorderService {
//查询所有工单
public List<Workorder> getAllWorkorderService();
//添加工单(单条)
//@param("注解名")
public void addWorkorderService(Workorder workorder);
}
WorkorderServiceImpl 工单表Service层接口实现类
//WorkorderServiceImpl 工单表Service层接口实现类
@Service("workorderService")
public class WorkorderServiceImpl implements WorkorderService {
//注入Dao层接口
@Autowired
private WorkorderMapper workorderMapper;
//注入service层接口(用于Controller)
@Autowired
private WorkorderService workorderService;
@Override
public List<Workorder> getAllWorkorderService() {
return workorderMapper.getAllWorkorder();
}
@Override
public void addWorkorderService(Workorder workorder) {
workorderMapper.addWorkorder(workorder);
}
}
test下创建TestService.java
TestService Service层测试类
//TestService Service层测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:springmvc-config.xml")
public class TestService {
private Logger logger = Logger.getLogger(TestService.class);
@Resource(name = "projectService")
private ProjectService projectService;
@Resource(name = "workorderService")
private WorkorderService workorderService;
//Service查询所有项目组
@Test
public void getAllProject() {
List<Project> projects = projectService.getAllProjectService();
for (Project p : projects) {
logger.info(p);
}
}
//Service查询所有工单
@Test
public void getAllWorkorder(){
List<Workorder> workorders = workorderService.getAllWorkorderService();
for(Workorder w:workorders){
logger.info(w.toString());
}
}
//Service添加工单(一条),并查询
@Test
public void addWorkorder(){
Date data = new Date();
Workorder workorder = new Workorder
(1,"张鲁一","完成核心开发",1,new Date());
workorderService.addWorkorderService(workorder);
getAllWorkorder();
}
}