【SSM】每个层的主要功能
众所周知,SSM即spring,springmvc和mybatis三个模块组合而成的一个框架。
结构如图所示
SSM框架各层之间的联系
view层调用controller层(view和controller层紧耦合,所以要结合开发),controller层调用service层(service层既要由调用Dao层的接口,也要有提供给controller层调用的接口),service层调用Dao层。
开发顺序
- 完成数据库的创建(Department)
- 根据数据库的字段(id,name,number),编写实体(entity)类,定义好每个对象的属性
entity.Department
public class Department {
private Integer id;
private String name;
private Integer number;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- 编写对数据库进行的具体操作,即增删查改。因为需要用到SQL语句,而我们需要将SQL语句和JAVA代码解耦合,所以我们将他放在映射文件(Mapper)中,即DepartmentDao.xml
代码:DepartmentDao.xml
<?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标签对dao接口进行一对一的映射(namespace保持唯一)-->
<mapper namespace="com.dao.DepartmentDao">
<!--配置数据库查找映射 ,连接到实体类-->
<!--id对应的接口内操作的方法名,resultType是定义的查询结果的返回类型。如下为实体类中的Department类型-->
<select id="search" resultType="com.entity.Department">
select * from department
</select>
<!--通过id查找,反正最后的操作都需要连接到实体类
并且接收业务层传输的id数据-->
<select id="searchById"
resultType="com.entity.Department">
select * from department where
id=#{id}
</select>
<!--添加操作,将组添加到部门表中,并且传入编号和姓名值-->
<insert id="add">
insert into department
(number,name)values(#{number},#{name})
</insert>
<!-- 修改操作,在id=x处修改业务层传输过来的编号和姓名-->
<update id="update">
update department set number=#{number},
name=#{name} where id=#{id}
</update>
<!-- 删除操作 从部门表中删除id为x的组-->
<delete id="delete">
delete from department where id=#{id}
</delete>
</mapper>
- 编写DepartmentDao.java,将Mapper中的xml文件通过id和applicationcontext.xml文件的配置映射到Dao接口中
DepartmentDao.xml的命名空间来指定Dao接口:
<mapper namespace="com.alan.hrsys.dao.DepartmentDao">
applicationcontext.xml配置文件配置Dao映射
<!--配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.alan.hrsys.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
- 编写service(业务层)代码。先编写业务层接口,在接口中定义方法,方便controller层进行调用
public interface DepartmentService {
public Department searchById(Integer id);
public boolean add(Department dep);
public boolean update(Department dep);
public boolean delete(Integer id);
}
再在impl包中调用Dao接口完成每个方法的具体实现。
@Service
public class DepartmentServiceImpl implements DepartmentService {
@Autowired
DepartmentDao depDao;
@Autowired
EmployeeDao empDao;
@Override
public List<Department> search() {
List<Department> list = depDao.search();
return list;
}
@Override
public Department searchById(Integer id) {
Department dep = depDao.searchById(id);
return dep;
}
@Override
public boolean add(Department dep) {
int rs = depDao.add(dep);
return rs > 0;
}
@Override
public boolean update(Department dep) {
int rs = depDao.update(dep);
return rs > 0;
}
@Override
public boolean delete(Integer id) {
int rs = depDao.delete(id);
rs = empDao.updateByDep(id);
return rs > 0;
}
}
- 编写controller层,通过controller层接收JSP传来的参数,调用service层接口,进行处理后再将处理后的参数通过JSP层展示给用户
//dep请求由DepartmentController类来处理
@Controller
@RequestMapping("dep")
public class DepartmentController {
@Autowired
DepartmentService depService;
// dep下的search请求会由search方法处理
@RequestMapping("search")
public ModelAndView search() {
ModelAndView mv = new ModelAndView("dep/show");
List<Department> list = depService.search();
mv.addObject("list", list);
return mv;
}
@RequestMapping("showAdd")
public String showAdd() {
return "dep/add";
}
@RequestMapping("add")
public String add(Department dep) {
boolean flag = depService.add(dep);
return "redirect:search";
}
@RequestMapping("showUpdate")
public ModelAndView showUpdat(Integer id) {
ModelAndView mv = new ModelAndView("dep/update");
Department dep = depService.searchById(id);
mv.addObject("dep", dep);
return mv;
}
@RequestMapping("update")
public String update(Department dep) {
boolean flag = depService.update(dep);
return "redirect:search";
}
@RequestMapping("delete")
public String delete(Integer id) {
boolean flag = depService.delete(id);
return "redirect:search";
}
}
- JSP页面代码