SSM框架整合思路
SSM框架整合思路
-
Spring在整合中起到的作用(面试时常问到)
Spring管理持久层的mapper。
Spring管理业务层的service,service可以调用mapper接口。Spring进行事物控制。
Spring管理表现层的Handler,handler可以调用service接口。
mapper,service,handler都是javabean。
-
Spring的核心(面试时常问到)
Ioc:依赖注入、控制反转。意思是在一个类中不用new关键字来声明对象,而是在xml文件中配置相应的节点即可,这样使整个程序变得灵活多样。
aop:面对切面编程。Spring中AOP代理由Spring的IOC容器负责生成、管理,其依赖关系也由IOC容器负责管理。
-
工程结构

-
管理mybatis的 xml 文件(单独使用mybatis时需要配置许多,如数据库连接等,现与Spring进行整合,这些东西配置在了管理Spring的xml文件中)
SqlMapConfig.xml
-
Spring配置文件

log4j.properties(日志文件,直接复制即可,开发模式使用DEBUG)
Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n数据库连接文件
db.properties(账号密码是我的数据库的,修改即可)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/device?characterEncoding=utf-8
jdbc.username=root
jdbc.password=rootapplicationContext-dao.xml(Spring管理持久层的mapper)
<context:property-placeholder location="classpath:db.properties"/>
applicationContext-service.xml(Spring管理业务层的service)
applicationContext-transaction.xml(Spring进行事物控制)
<tx:advice id="txAdvice" transaction-manager="transactionManager">
tx:attributes
<tx:method name="save" propagation="REQUIRED" />
<tx:method name="delete" propagation="REQUIRED" />
<tx:method name="insert" propagation="REQUIRED" />
<tx:method name="update" propagation="REQUIRED" />
<tx:method name="find" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
aop:config
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* cn.itcast.ssm.service.impl..(..))"/>
</aop:config>springmvc.xml(Spring管理表现层的Handler)
<context:component-scan
base-package="cn.itcast.ssm.controller" >
</context:component-scan>mvc:annotation-driven</mvc:annotation-driven>
web.xml
Device_web1.1
contextConfigLocation
/WEB-INF/classes/spring/applicationContext-.xml
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springmvc.xml
.action
springmvc
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/*
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
-
代码实现流程

LiuyanMapper.xml-->LiuyanMapper.java-->LiuyanService.java-->LiuyanServiceImpl.java-->LiuyanController.java-->jsp
LiuyanMapper.xml
insert into liuyan(liuyan) value(#{liuyan})
UPDATE liuyan SET liuyan=#{liuyan} WHERE id=#{id}
DELETE FROM liuyan WHERE liuyan=#{liuyan}
LiuyanMapper.java
package cn.itcast.ssm.mapper;
import java.util.List;
import cn.itcast.ssm.po.Liuyan;
import cn.itcast.ssm.po.LiuyanCustom;
import cn.itcast.ssm.po.LiuyanQueryVo;public interface LiuyanMapper {
public List
findLiuyanList(LiuyanQueryVo liuyanQueryVo) throws Exception; public List
findLiuyanByname(String liuyan) throws Exception; public Liuyan findLiuyanBynameOne(String liuyan) throws Exception;
public void insertLiuyan(Liuyan liuyan) throws Exception;
int updateLiuyan(Liuyan liuyan) throws Exception;
public void deleteLiuyanBynameOne(String liuyan) throws Exception;
}LiuyanService.java
package cn.itcast.ssm.service;
import java.util.List;
import cn.itcast.ssm.po.Liuyan;
import cn.itcast.ssm.po.LiuyanCustom;
import cn.itcast.ssm.po.LiuyanQueryVo;public interface LiuyanService {
public List
findLiuyanList(LiuyanQueryVo liuyanQueryVo) throws Exception; public List
findLiuyanByname(String liuyan) throws Exception; public Liuyan findLiuyanBynameOne(String liuyan) throws Exception;
public void insertLiuyan(Liuyan liuyan) throws Exception;
int updateLiuyan(Integer id,Liuyan liuyan) throws Exception;
public void deleteLiuyanBynameOne(String liuyan) throws Exception;
}LiuyanServiceImpl.java
package cn.itcast.ssm.service.impl;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;import cn.itcast.ssm.mapper.LiuyanMapper;
import cn.itcast.ssm.po.Liuyan;
import cn.itcast.ssm.po.LiuyanCustom;
import cn.itcast.ssm.po.LiuyanQueryVo;
import cn.itcast.ssm.service.LiuyanService;public class LiuyanServiceImpl implements LiuyanService{
@Autowired
private LiuyanMapper liuyanMapper;
@Override
public ListfindLiuyanList(LiuyanQueryVo liuyanQueryVo) throws Exception {
// TODO 自动生成的方法存根
return liuyanMapper.findLiuyanList(liuyanQueryVo);
}
@Override
public ListfindLiuyanByname(String liuyan) throws Exception {
// TODO 自动生成的方法存根List
liuyanCustom = liuyanMapper.findLiuyanByname(liuyan); return liuyanCustom;
}
@Override
public void insertLiuyan(Liuyan liuyan) throws Exception {
// TODO 自动生成的方法存根liuyanMapper.insertLiuyan(liuyan);
return ;
}
@Override
public int updateLiuyan(Integer id,Liuyan liuyan) throws Exception {
return liuyanMapper.updateLiuyan(liuyan);
}
@Override
public Liuyan findLiuyanBynameOne(String liuyan) throws Exception {
// TODO 自动生成的方法存根
liuyanMapper.findLiuyanBynameOne(liuyan);return liuyanMapper.findLiuyanBynameOne(liuyan);
}
@Override
public void deleteLiuyanBynameOne(String liuyan) throws Exception {
// TODO 自动生成的方法存根
liuyanMapper.deleteLiuyanBynameOne(liuyan);
}
}LiuyanController.java
package cn.itcast.ssm.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.ibatis.annotations.Param;
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.RequestParam;
import org.springframework.web.servlet.ModelAndView;import cn.itcast.ssm.po.Liuyan;
import cn.itcast.ssm.po.LiuyanCustom;
import cn.itcast.ssm.service.LiuyanService;@Controller
@RequestMapping("/liuyan")
public class LiuyanController {@Autowired
private LiuyanService liuyanService;@RequestMapping("/queryLiuyan")
public ModelAndView queryLiuyan()throws Exception {
//调用service查找 数据库,查询商品列表,这里使用静态数据模拟
ListliuyanList = liuyanService.findLiuyanList(null); //返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
//相当 于request的setAttribut,在jsp页面中通过itemsList取数据
modelAndView.addObject("liuyanList",liuyanList);
//modelAndView.addObject("liuyanList", liuyanList);
//不可以有空格
modelAndView.setViewName("liuyan/liuyanList");
return modelAndView;
}@RequestMapping("/queryLiuyanByname")
public ModelAndView queryLiuyanByname()throws Exception {
//调用service查找 数据库,查询商品列表,这里使用静态数据模拟
ListliuyanCustom = liuyanService.findLiuyanByname("任秀兴"); //返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
//相当 于request的setAttribut,在jsp页面中通过itemsList取数据
modelAndView.addObject("liuyanList",liuyanCustom);
//modelAndView.addObject("liuyanList", liuyanList);
//不可以有空格
modelAndView.setViewName("liuyan/liuyanList");
return modelAndView;
}@RequestMapping("/insertLiuyan")
public String insertLiuyan(HttpServletRequest request)throws Exception {
//调用service查找 数据库
String strliuyan = request.getParameter("liuyan");
//String strliuyan1 = request.getParameter("liuyan1");
System.out.println(request.getParameter("liuyan"));
//System.out.println(request.getParameter("liuyan1"));
Liuyan liuyan = new Liuyan();
liuyan.setLiuyan(strliuyan);
liuyanService.insertLiuyan(liuyan);
//重定向
return "redirect:queryLiuyan.action";}
@RequestMapping("/updateLiuyan")
public ModelAndView updateLiuyan(String liuyan)throws Exception {Liuyan speak = liuyanService.findLiuyanBynameOne(liuyan);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("speak",speak);
modelAndView.setViewName("liuyan/updateliuyan");return modelAndView;
}@RequestMapping("/updateLiuyanSubmit")
public String updateLiuyanSubmit(HttpServletRequest request,Liuyan liuyan)throws Exception {String strid = request.getParameter("id");
System.out.println(strid);
int id = Integer.parseInt(strid);
liuyan.setId(id);
liuyanService.updateLiuyan(id,liuyan);
//重定向
return "redirect:queryLiuyan.action";}
@RequestMapping("/deleteLiuyan")
public String deleteLiuyan(HttpServletRequest request)throws Exception {
String strliuyan = request.getParameter("liuyan");liuyanService.deleteLiuyanBynameOne(strliuyan);
return "redirect:queryLiuyan.action";
}
}jsp

liuyanList.jsp
<%@page import =" java.text.SimpleDateFormat" %>
<%@page import =" java.util.Date" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
留言板


浙公网安备 33010602011771号