本篇博客主要内容是:
(1)开发【首页导航栏的用户名部门信息】:
(2)然后,借此引出一种更加常见的开发方式:【基于Xml配置下,使用Mapper接口】的方式;
(3)然后,通过本篇博客也有一个额外的感觉:数据库的表设计很重要,设计技巧和设计能力需要慢慢累计;
目录
零.问题说明:为什么需要【基于Xml配置下,使用Mapper接口】的方式
1.首先,创建Employee员工类,用来承载存储查询adm_employee表的结果
2.然后,创建EmployeeDao接口:定义对应的方法(方法没有实现)
3.然后,编写【Mapper XML】employee.xml:定义(EmployeeDao接口中)方法的具体实现
4.然后,编写EmployeeService类:调用接口中的方法(也就是调用SQL语句啦)
如何验证写的这个Service能不能用? (一个习惯,在编写好Service后,最好测试一下)
5.然后,编写IndexServlet:在登录成功后,获取登录用户的Employee信息,将其存放到session属性中,这样以后再跳转到index.ftl首页
6.然后,在默认首页的前台文件index.ftl中,获取Employee信息
1.首先,创建Department部门类,用来承载存储adm_department表的结果
3.然后,编写【Mapper XML】department.xml:定义(DepartmentDao接口中)方法的具体实现
4.然后,编写DepartmentService:调用接口中的方法
5.然后,编写IndexServlet:在登录成功后,获取登录用户的Department信息,将其存放到session属性中,这样以后再跳转到index.ftl首页
6.然后,在默认首页的前台文件index.ftl中,获取Departmet信息
零.问题说明:为什么需要【基于Xml配置下,使用Mapper接口】的方式
上篇博客中OA系统十一:登录成功后跳转到默认首页,初始化默认首页的左侧功能菜单;(以前接触的开发方式也都是如此)的开发方式是,编写Dao类,然后在类中去调用Mapper XML中SQL语句的方式。
……………………………………………………
但是,记得在MyBatis进阶九:Mybatis注解开发;(查询案例,插入案例,***DTO类的结果映射)中介绍了Mybatis的注解的开发方式。mybatis注解开发方式本身并不是广泛采用的方法,但是从中可以获取一点启发。
mybatis注解开发中,创建的是Dao接口(不是类):
然后,调用的时候,直接基于代理模式去调用接口中的方法:
这给了我们启发,即可以采用【基于Xml配置下,使用Mapper接口】的方式去开发。
一.使用接口的方式开发:开发【姓名】和【职务】
1.首先,创建Employee员工类,用来承载存储查询adm_employee表的结果
Employee类:
package com.imooc.oa.entity;
public class Employee {
private Long employeeId; //员工编号
private String name; //员工姓名
private Long departmentId; //部门编号
private String title; //员工职务
private Integer level; //员工级别
public Long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(Long employeeId) {
this.employeeId = employeeId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getDepartmentId() {
return departmentId;
}
public void setDepartmentId(Long departmentId) {
this.departmentId = departmentId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
}
说明:
(1)以前说过多次,这儿重复啰嗦一下,Employee类的属性名是对比着adm_employee表的字段名来写的:
(2)以前说过多次,这儿重复啰嗦一下;数据库中类型为bigint的,在employee类中属性类型是Long:
……………………………………………………
2.然后,创建EmployeeDao接口:定义对应的方法(方法没有实现)
EmployeeDao接口:
package com.imooc.oa.dao;
import com.imooc.oa.entity.Employee;
public interface EmployeeDao {
public Employee selectById(Long employeeId);
}
说明:
(1)这个接口中的方式,只是定义了方法,并没有方法的实现。
……………………………………………………