ssm框架是由Spring springmvc和Mybatis共同组成的框架。Spring和Springmvc都是spring公司开发的,因此他们之间不需要整合。也可以说是无缝整合。mybatis公司开发的mybatis,因为需要和Spring整合,但是他们之间是有缝的。因此mybatis公司就做出了jar包 mybatis-spring的核心整合包。
Q:什么叫无缝整合,有缝整合?
A:最方便的理解就是有Jar包就是有缝,没有jar包就是无缝。
三个文件:applicationContext.xml springmvc.xml mybatis.xml (名字一般都这样起,因为我们还需要对他们进行配置,需要用文件名,同时也能让其他人明白)
下图是项目的架构图
applicationContext.xml配置文件
里面包含数据源的读取properties,配置dataSource,事务管理对象transactionManager、
sqlsession工厂SqlSessionFactory 以及Mapper扫描(xml名和接口名相同)。主要负责与mybatis整合
Springmvc.xml
主要负责注解,springmvc的三大组件(处理器映射器、处理器适配器、视图解析器,这三个有默认的类,但是已经过时了。)
mybatis.xml
mybatis可以只配一个别名,大小写不敏感,可以不配置,但是不配置的话必须使用包名+类名。
web.xml
ssm配置完了,但是tomcat启动后并不会对Spring的框架进行读取,因此我们还需要配置一个监听器。
ssmDemo
index.jsp
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encoding
/*
ssm-crm
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
ssm-crm
/
在这里ssm的三大框架基本上就整合完毕了。
下面这个案例是一个客户的增删改查的分页小Demo
CustomerMapper.xml(写sql文件的,必须和CustomerMapper.java的接口文件在一个包下,并且名字相同)
update customer set cust_name = #{cust_name} ,
cust_source = #{cust_source} ,
cust_industry = #{cust_industry} ,
cust_level = #{cust_level} ,
cust_linkman = #{cust_linkman} ,
cust_phone = #{cust_phone} ,
cust_mobile = #{cust_mobile} ,
cust_zipcode = #{cust_zipcode} ,
cust_address = #{cust_address} ,
cust_createtime = #{cust_createtime}
cust_id = #{cust_id}
delete from customer
cust_id = #{cust_id}
AND a.cust_name LIKE '%${custName}%'
AND a.cust_source = #{custSource}
AND a.cust_industry = #{custIndustry}
AND a.cust_level = #{custLevel}
CustomerMapper.java(接口文件)(写sql文件的,必须和CustomerMapper.xml文件在一个包下,并且名字相同,两个文件遵循4大原则{名字相同,xml的mapper namespace属性指向接口,入参类型相同,出参类型相同})
package cn.tsu.ssm.dao;
import java.util.List;
import cn.tsu.common.utils.Page;
import cn.tsu.ssm.pojo.Customer;
import cn.tsu.ssm.pojo.Queryvo;
public interface CustomerMapper {
public List findBaseDictByCode(String code);
public List findCustomerByPage(Queryvo vo);
public Integer findCustomerCount(Queryvo vo);
public void editCustomer(Customer customer);
public Customer edit(Integer id );
public void deleteByid (Integer id);
}
service层接口略
…
…
…
service实现类
service层主要写了分页的业务
package cn.tsu.ssm.service;
import java.util.List;
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 cn.tsu.common.utils.Page;
import cn.tsu.ssm.dao.CustomerMapper;
import cn.tsu.ssm.pojo.Customer;
import cn.tsu.ssm.pojo.Queryvo;
@Service
@Transactional(readOnly=false,isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRED)
public class CustomerServiceImpl implements CustomerService{
@Autowired
private CustomerMapper cm;
public List findCustomer(String code) {
return cm.findBaseDictByCode(code);
}
public Page findCustomerByPage(Queryvo vo) {
// TODO Auto-generated method stub
vo.setStart((vo.getPage()-1)*vo.getRows());
List findCustomerByPage = cm.findCustomerByPage(vo);
Integer total = cm.findCustomerCount(vo);
Page page = new Page(total,findCustomerByPage,vo.getPage(),vo.getRows());
return page;
}
public void editCustomer(Customer customer) {
cm.editCustomer(customer);
}
public Customer edit(Integer id) {
// TODO Auto-generated method stub
return cm.edit(id);
}
public void deleteByid(Integer id) {
cm.deleteByid(id);
}
}
DemoController层
主要写了web层,接收前端传递过来的数据,post,get,json等
package cn.tsu.ssm.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.tsu.common.utils.Page;
import cn.tsu.ssm.pojo.Customer;
import cn.tsu.ssm.pojo.Queryvo;
import cn.tsu.ssm.service.CustomerService;
@Controller
public class DemoController {
@Autowired
private CustomerService cs;
@RequestMapping("listUi")
public String listUi(Model model,Queryvo vo) {
List fromType = cs.findCustomer("002");
List industryType = cs.findCustomer("001");
List levelType = cs.findCustomer("006");
model.addAttribute("fromType", fromType);
model.addAttribute("industryType", industryType);
model.addAttribute("levelType", levelType);
//分页查询
Page page = cs.findCustomerByPage(vo);
model.addAttribute("page", page);
model.addAttribute("custName", vo.getCustName());
model.addAttribute("custSource", vo.getCustSource());
model.addAttribute("custIndustry", vo.getCustIndustry());
model.addAttribute("custLevel", vo.getCustLevel());
return "customer";
}
@RequestMapping("edit")
public @ResponseBody
Customer edit(Model model, Integer id) {
Customer customer = cs.edit(id);
return customer;
}
@RequestMapping("update")
public @ResponseBody
String update(Customer customer) {
cs.editCustomer(customer);
return "ok";//这里回复null前端也有响应
}
@RequestMapping(value="delete")
public @ResponseBody
String delete (Integer id) {
System.out.println(id);
cs.deleteByid(id);
return null;
}
}
下面把工具类的代码也写出来
page.java
package cn.tsu.common.utils;
import java.util.List;
import cn.tsu.ssm.pojo.Customer;
public class Page {
//一共有多少数据
private int total;
//当前页
private int page;
//当页显示多少数据
private int size;
//数据
private List rows;
public Page(Integer total2, List findCustomerByPage, Integer page2, Integer rows2) {
this.total=total2;
this.rows=findCustomerByPage;
this.page = page2;
this.size=rows2;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public List getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}
}
NavigationTag.java
package cn.tsu.common.utils;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 显示格式 上一页 1 2 3 4 5 下一页
*/
public class NavigationTag extends TagSupport {
static final long serialVersionUID = 2372405317744358833L;
/**
* request 中用于保存Page 对象的变量名,默认为“page”
*/
private String bean = "page";
/**
* 分页跳转的url地址,此属性必须
*/
private String url = null;
/**
* 显示页码数量
*/
private int number = 5;
@Override
public int doStartTag() throws JspException {
JspWriter writer = pageContext.getOut();
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
Page page = (Page)request.getAttribute(bean);
if (page == null)
return SKIP_BODY;
url = resolveUrl(url, pageContext);
try {
//计算总页数
int pageCount = page.getTotal() / page.getSize();
if (page.getTotal() % page.getSize() > 0) {
pageCount++;
}
writer.print("");
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}
private String append(String url, String key, int value) {
return append(url, key, String.valueOf(value));
}
/**
* 为url 参加参数对儿
*
* @param url
* @param key
* @param value
* @return
*/
private String append(String url, String key, String value) {
if (url == null || url.trim().length() == 0) {
return "";
}
if (url.indexOf("?") == -1) {
url = url + "?" + key + "=" + value;
} else {
if(url.endsWith("?")) {
url = url + key + "=" + value;
} else {
url = url + "&" + key + "=" + value;
}
}
return url;
}
/**
* 为url 添加翻页请求参数
*
* @param url
* @param pageContext
* @return
* @throws javax.servlet.jsp.JspException
*/
private String resolveUrl(String url, javax.servlet.jsp.PageContext pageContext) throws JspException{
//UrlSupport.resolveUrl(url, context, pageContext)
Map params = pageContext.getRequest().getParameterMap();
for (Object key:params.keySet()) {
if ("page".equals(key) || "rows".equals(key)) continue;
Object value = params.get(key);
if (value == null) continue;
if (value.getClass().isArray()) {
url = append(url, key.toString(), ((String[])value)[0]);
} else if (value instanceof String) {
url = append(url, key.toString(), value.toString());
}
}
return url;
}
/**
* @return the bean
*/
public String getBean() {
return bean;
}
/**
* @param bean the bean to set
*/
public void setBean(String bean) {
this.bean = bean;
}
/**
* @return the url
*/
public String getUrl() {
return url;
}
/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
public void setNumber(int number) {
this.number = number;
}
}
commons.tlb
自己写的标签库,只需要在jsp页面引入即可使用,主要用在jsp页面的分页。
该文件放到WEB-INF下即可
2.0
1.2
common
http://cn.tsu.xiaofeng/common/
Common Tag
Common Tag library
page
cn.tsu.common.utils.NavigationTag
JSP
create navigation for paging
bean
true
number
true
url
true
true
完 我把资源上传到文件库中,如果大家有需要代码的,可以自行下载。因为需要设置分,因此就设置1分啦。!!!下载