客戶管理系統 (SSM)全配置
阿新 • • 發佈:2018-12-11
package cn.itcast.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; import org.apache.taglibs.standard.tag.common.core.UrlSupport; /** * 顯示格式 上一頁 1 2 3 4 5 下一頁 */ public class NavigationTag extends TagSupport { static final long serialVersionUID = 2372405317744358833L; /** * request 中用於儲存Page<E> 物件的變數名,預設為“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("<nav><ul class=\"pagination\">"); //顯示“上一頁”按鈕 if (page.getPage() > 1) { String preUrl = append(url, "page", page.getPage() - 1); preUrl = append(preUrl, "rows", page.getSize()); writer.print("<li><a href=\"" + preUrl + "\">上一頁</a></li>"); } else { writer.print("<li class=\"disabled\"><a href=\"#\">上一頁</a></li>"); } //顯示當前頁碼的前2頁碼和後兩頁碼 //若1 則 1 2 3 4 5, 若2 則 1 2 3 4 5, 若3 則1 2 3 4 5, //若4 則 2 3 4 5 6 ,若10 則 8 9 10 11 12 int indexPage = (page.getPage() - 2 > 0)? page.getPage() - 2 : 1; for(int i=1; i <= number && indexPage <= pageCount; indexPage++, i++) { if(indexPage == page.getPage()) { writer.print( "<li class=\"active\"><a href=\"#\">"+indexPage+"<span class=\"sr-only\">(current)</span></a></li>"); continue; } String pageUrl = append(url, "page", indexPage); pageUrl = append(pageUrl, "rows", page.getSize()); writer.print("<li><a href=\"" + pageUrl + "\">"+ indexPage +"</a></li>"); } //顯示“下一頁”按鈕 if (page.getPage() < pageCount) { String nextUrl = append(url, "page", page.getPage() + 1); nextUrl = append(nextUrl, "rows", page.getSize()); writer.print("<li><a href=\"" + nextUrl + "\">下一頁</a></li>"); } else { writer.print("<li class=\"disabled\"><a href=\"#\">下一頁</a></li>"); } writer.print("</nav>"); } 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; } }
utils
package cn.itcast.common.utils; import java.util.List; import cn.itcast.crm.pojo.Customer; public class Page<T> { private int total; private int page; private int size; private List<T> rows; public Page(int total, int page, int size, List<T> rows) { super(); this.total = total; this.page = page; this.size = size; this.rows = rows; } 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<T> getRows() { return rows; } public void setRows(List<T> rows) { this.rows = rows; } }
Controller
package cn.itcast.crm.controller; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; 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.itcast.common.utils.Page; import cn.itcast.crm.pojo.BaseDict; import cn.itcast.crm.pojo.Customer; import cn.itcast.crm.pojo.QueryVo; import cn.itcast.crm.service.BaseDictService; import cn.itcast.crm.service.CustomerService; @Controller @RequestMapping("customer") public class CustomerController { // 客戶來源 @Value("${CUSTOMER_FROM_TYPE}") private String CUSTOMER_FROM_TYPE; // 客戶行業 @Value("${CUSTOMER_INDUSTRY_TYPE}") private String CUSTOMER_INDUSTRY_TYPE; // 客戶級別 @Value("${CUSTOMER_LEVEL_TYPE}") private String CUSTOMER_LEVEL_TYPE; @Autowired private CustomerService customerService; @Autowired private BaseDictService baseDictService; /** * 顯示使用者頁面 */ @RequestMapping("list") public String queryCustometrList(QueryVo queryVo, Model model) { /*try { // 解決get請求亂碼問題 if (StringUtils.isNotBlank(queryVo.getCustName())) { queryVo.setCustName(new String(queryVo.getCustName().getBytes("UTF-8"), "UTF-8")); } } catch (Exception e) { e.printStackTrace(); }*/ // 客戶來源 List<BaseDict> fromType = this.baseDictService.queryBaseDictByDictTypeCode(this.CUSTOMER_FROM_TYPE); // 所屬行業 List<BaseDict> industryType = this.baseDictService.queryBaseDictByDictTypeCode(this.CUSTOMER_INDUSTRY_TYPE); // 客戶級別 List<BaseDict> levelType = this.baseDictService.queryBaseDictByDictTypeCode(this.CUSTOMER_LEVEL_TYPE); // 把前端頁面需要顯示的資料放到模型中 model.addAttribute("fromType", fromType); model.addAttribute("industryType", industryType); model.addAttribute("levelType", levelType); // 分頁查詢資料 Page<Customer> page = this.customerService.queryCustomerByQueryVo(queryVo); // 把分頁查詢的結果放到模型中 model.addAttribute("page", page); // 資料回顯 model.addAttribute("custName", queryVo.getCustName()); model.addAttribute("custSource", queryVo.getCustSource()); model.addAttribute("custIndustry", queryVo.getCustIndustry()); model.addAttribute("custLevel", queryVo.getCustLevel()); return "customer"; } /** * 根據id查詢使用者,返回json格式資料 * * @param id * @return */ @RequestMapping("edit") @ResponseBody public Customer queryCustomerById(Long id) { Customer customer = this.customerService.queryCustomerById(id); return customer; } /** * 根據id查詢使用者,返回更新後客戶的json格式資料 * * @param id * @return */ @RequestMapping("update") @ResponseBody public String updateCustomerById(Customer customer) { Customer result = this.customerService.updateCustomerById(customer); return "OK"; } /** * 刪除使用者 * * @param id * @return */ @RequestMapping("delete") @ResponseBody public String deleteCustomerById(Long id) { this.customerService.deleteCustomerById(id); return "ok"; } }
Mapper
package cn.itcast.crm.mapper;
import java.util.List;
import cn.itcast.crm.pojo.BaseDict;
public interface BaseDictMapper {
/**
* 根據類別程式碼查詢資料
*
* @param dictTypeCode
* @return
*/
List<BaseDict> queryBaseDictByDictTypeCode(String dictTypeCode);
}
package cn.itcast.crm.mapper;
import java.util.List;
import cn.itcast.crm.pojo.Customer;
import cn.itcast.crm.pojo.QueryVo;
public interface CustomerMapper {
/**
* 根據queryVo分頁查詢資料
*
* @param queryVo
* @return
*/
List<Customer> queryCustomerByQueryVo(QueryVo queryVo);
/**
* 根據queryVo查詢資料條數
*
* @param queryVo
* @return
*/
int queryCountByQueryVo(QueryVo queryVo);
/**
* 根據id查詢客戶
*
* @param id
* @return
*/
Customer queryCustomerById(Long id);
/**
* 根據id編輯客戶
*
* @param customer
* @return
*/
Customer updateCustomerById(Customer customer);
/**
* 根據id刪除使用者
*
* @param id
*/
void deleteCustomerById(Long id);
}
<?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.itcast.crm.mapper.BaseDictMapper">
<!-- 根據類別程式碼查詢資料 -->
<select id="queryBaseDictByDictTypeCode" parameterType="String" resultType="cn.itcast.crm.pojo.BaseDict">
SELECT * FROM base_dict WHERE dict_type_code = #{dict_type_code}
</select>
</mapper>
<?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.itcast.crm.mapper.CustomerMapper">
<sql id="customerQueryVo">
<where>
<if test="custName != null and custName != ''">
AND a.cust_name LIKE '%${custName}%'
</if>
<if test="custSource != null and custSource != ''">
AND a.cust_source = #{custSource}
</if>
<if test="custIndustry != null and custIndustry != ''">
AND a.cust_industry = #{custIndustry}
</if>
<if test="custLevel != null and custLevel != ''">
AND a.cust_level = #{custLevel}
</if>
</where>
</sql>
<!-- 根據queryVo分頁查詢資料 -->
<select id="queryCustomerByQueryVo" parameterType="cn.itcast.crm.pojo.QueryVo" resultType="cn.itcast.crm.pojo.Customer">
SELECT
a.cust_id,
a.cust_name,
a.cust_user_id,
a.cust_create_id,
b.dict_item_name cust_source,
c.dict_item_name cust_industry,
d.dict_item_name cust_level,
a.cust_linkman,
a.cust_phone,
a.cust_mobile,
a.cust_zipcode,
a.cust_address,
a.cust_createtime
FROM
customer a
LEFT JOIN base_dict b ON a.cust_source = b.dict_id
LEFT JOIN base_dict c ON a.cust_industry = c.dict_id
LEFT JOIN base_dict d ON a.cust_level = d.dict_id
<include refid="customerQueryVo" />
<if test="start != null">
LIMIT #{start}, #{rows}
</if>
</select>
<!-- 根據queryVo查詢資料條數 -->
<select id="queryCountByQueryVo" parameterType="cn.itcast.crm.pojo.QueryVo" resultType="int">
SELECT count(1) FROM customer a
<include refid="customerQueryVo" />
</select>
<!-- 根據id查詢使用者 -->
<select id="queryCustomerById" resultType="cn.itcast.crm.pojo.Customer">
SELECT * FROM customer WHERE cust_id = #{id}
</select>
<select id="updateCustomerById" parameterType="cn.itcast.crm.pojo.Customer">
UPDATE `customer`
SET
<if test="cust_name !=null and cust_name != ''">
`cust_name` = #{cust_name},
</if>
<if test="cust_user_id !=null">
`cust_user_id` = #{cust_user_id},
</if>
<if test="cust_create_id !=null">
`cust_create_id` = #{cust_create_id},
</if>
<if test="cust_source !=null and cust_source != ''">
`cust_source` = #{cust_source},
</if>
<if test="cust_industry !=null and cust_industry != ''">
`cust_industry` = #{cust_industry},
</if>
<if test="cust_level !=null and cust_level != ''">
`cust_level` = #{cust_level},
</if>
<if test="cust_linkman !=null and cust_linkman != ''">
`cust_linkman` = #{cust_linkman},
</if>
<if test="cust_phone !=null and cust_phone != ''">
`cust_phone` = #{cust_phone},
</if>
<if test="cust_mobile !=null and cust_mobile != ''">
`cust_mobile` = #{cust_mobile},
</if>
<if test="cust_zipcode !=null and cust_zipcode != ''">
`cust_zipcode` = #{cust_zipcode},
</if>
<if test="cust_address !=null and cust_address != ''">
`cust_address` = #{cust_address},
</if>
`cust_createtime` = NOW()
WHERE
(`cust_id` = #{cust_id});
</select>
<!-- 根據id刪除客戶 -->
<delete id="deleteCustomerById" parameterType="long">
DELETE FROM customer WHERE cust_id = #{id}
</delete>
</mapper>
pojo
package cn.itcast.crm.pojo;
import java.io.Serializable;
public class BaseDict implements Serializable{
private String dict_id;
private String dict_type_code;
private String dict_type_name;
private String dict_item_name;
private String dict_item_code;
private Integer dict_sort;
private String dict_enable;
private String dict_memo;
public String getDict_id() {
return dict_id;
}
public void setDict_id(String dict_id) {
this.dict_id = dict_id;
}
public String getDict_type_code() {
return dict_type_code;
}
public void setDict_type_code(String dict_type_code) {
this.dict_type_code = dict_type_code;
}
public String getDict_type_name() {
return dict_type_name;
}
public void setDict_type_name(String dict_type_name) {
this.dict_type_name = dict_type_name;
}
public String getDict_item_name() {
return dict_item_name;
}
public void setDict_item_name(String dict_item_name) {
this.dict_item_name = dict_item_name;
}
public String getDict_item_code() {
return dict_item_code;
}
public void setDict_item_code(String dict_item_code) {
this.dict_item_code = dict_item_code;
}
public Integer getDict_sort() {
return dict_sort;
}
public void setDict_sort(Integer dict_sort) {
this.dict_sort = dict_sort;
}
public String getDict_enable() {
return dict_enable;
}
public void setDict_enable(String dict_enable) {
this.dict_enable = dict_enable;
}
public String getDict_memo() {
return dict_memo;
}
public void setDict_memo(String dict_memo) {
this.dict_memo = dict_memo;
}
@Override
public String toString() {
return "BaseDict [dict_id=" + dict_id + ", dict_type_code=" + dict_type_code + ", dict_type_name="
+ dict_type_name + ", dict_item_name=" + dict_item_name + ", dict_item_code=" + dict_item_code
+ ", dict_sort=" + dict_sort + ", dict_enable=" + dict_enable + ", dict_memo=" + dict_memo + "]";
}
}
package cn.itcast.crm.pojo;
import java.io.Serializable;
import java.util.Date;
public class Customer implements Serializable{
private Long cust_id;
private String cust_name;
private Long cust_user_id;
private Long cust_create_id;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
private String cust_zipcode;
private String cust_address;
private Date cust_createtime;
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public Long getCust_user_id() {
return cust_user_id;
}
public void setCust_user_id(Long cust_user_id) {
this.cust_user_id = cust_user_id;
}
public Long getCust_create_id() {
return cust_create_id;
}
public void setCust_create_id(Long cust_create_id) {
this.cust_create_id = cust_create_id;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_linkman() {
return cust_linkman;
}
public void setCust_linkman(String cust_linkman) {
this.cust_linkman = cust_linkman;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
public String getCust_zipcode() {
return cust_zipcode;
}
public void setCust_zipcode(String cust_zipcode) {
this.cust_zipcode = cust_zipcode;
}
public String getCust_address() {
return cust_address;
}
public void setCust_address(String cust_address) {
this.cust_address = cust_address;
}
public Date getCust_createtime() {
return cust_createtime;
}
public void setCust_createtime(Date cust_createtime) {
this.cust_createtime = cust_createtime;
}
@Override
public String toString() {
return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_user_id=" + cust_user_id
+ ", cust_create_id=" + cust_create_id + ", 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 + "]";
}
}
package cn.itcast.crm.pojo;
public class QueryVo {
private String custName;
private String custSource;
private String custIndustry;
private String custLevel;
// 當前頁碼數
private Integer page = 1;
// 資料庫從哪一條資料開始查
private Integer start;
// 每頁顯示資料條數
private Integer rows = 10;
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getStart() {
return start;
}
public void setStart(Integer start) {
this.start = start;
}
public Integer getRows() {
return rows;
}
public void setRows(Integer rows) {
this.rows = rows;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustSource() {
return custSource;
}
public void setCustSource(String custSource) {
this.custSource = custSource;
}
public String getCustIndustry() {
return custIndustry;
}
public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
}
public String getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
}
service
package cn.itcast.crm.service;
import java.util.List;
import cn.itcast.crm.pojo.BaseDict;
public interface BaseDictService {
/**
* 根據類別程式碼查詢
*
* @param dictTypeCode
* @return
*/
List<BaseDict> queryBaseDictByDictTypeCode(String dictTypeCode);
}
package cn.itcast.crm.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.itcast.crm.mapper.BaseDictMapper;
import cn.itcast.crm.pojo.BaseDict;
@Service
public class BaseDictServiceImpl implements BaseDictService {
@Autowired
private BaseDictMapper baseDictMapper;
@Override
public List<BaseDict> queryBaseDictByDictTypeCode(String dictTypeCode) {
List<BaseDict> list = this.baseDictMapper.queryBaseDictByDictTypeCode(dictTypeCode);
return list;
}
}
package cn.itcast.crm.service;
import cn.itcast.common.utils.Page;
import cn.itcast.crm.pojo.Customer;
import cn.itcast.crm.pojo.QueryVo;
public interface CustomerService {
/**
* 根據條件分頁查詢客戶
*
* @param queryVo
* @return
*/
Page<Customer> queryCustomerByQueryVo(QueryVo queryVo);
/**
* 根據id查詢資料
*
* @param id
* @return
*/
Customer queryCustomerById(Long id);
/**
* 根據id編輯客戶資料
*
* @param customer
*/
Customer updateCustomerById(Customer customer);
/**
* 根據id刪除客戶
*
* @param id
*/
void deleteCustomerById(Long id);
}
package cn.itcast.crm.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.itcast.common.utils.Page;
import cn.itcast.crm.mapper.CustomerMapper;
import cn.itcast.crm.pojo.Customer;
import cn.itcast.crm.pojo.QueryVo;
@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerMapper customerMapper;
@Override
public Page<Customer> queryCustomerByQueryVo(QueryVo queryVo) {
// 設定查詢條件,從哪一條資料開始查
queryVo.setStart((queryVo.getPage() - 1) * queryVo.getRows());
// 查詢資料結果集
List<Customer> list = this.customerMapper.queryCustomerByQueryVo(queryVo);
// 查詢到的資料總條數
int total = this.customerMapper.queryCountByQueryVo(queryVo);
// 封裝返回的page物件
Page<Customer> page = new Page<>(total, queryVo.getPage(), queryVo.getRows(), list);
return page;
}
//通過ID查詢
@Override
public Customer queryCustomerById(Long id) {
Customer customer = this.customerMapper.queryCustomerById(id);
return customer;
}
//通過id更新
@Override
public Customer updateCustomerById(Customer customer) {
return this.customerMapper.updateCustomerById(customer);
}
//通過id刪除
@Override
public void deleteCustomerById(Long id) {
this.customerMapper.deleteCustomerById(id);
}
}
配置檔案
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置 讀取properties檔案 jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置 資料來源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置SqlSessionFactory -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 設定MyBatis核心配置檔案 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
<!-- 設定資料來源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置Mapper掃描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 設定Mapper掃描包 -->
<property name="basePackage" value="cn.itcast.crm.mapper" />
</bean>
</beans>
applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置Service掃描 -->
<context:component-scan base-package="cn.itcast.crm.service" />
</beans>
applicationContext-trans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 事務管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 資料來源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 傳播行為 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.itcast.crm.service.*.*(..))" />
</aop:config>
</beans>
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置Controller掃描 -->
<context:component-scan base-package="cn.itcast.crm.controller" />
<!-- 配置註解驅動 -->
<mvc:annotation-driven />
<!-- 解決靜態資源無法被springMVC處理的問題 -->
<mvc:default-servlet-handler />
<!-- 載入controller需要的配置資訊 -->
<context:property-placeholder location="classpath:env.properties" />
<!-- 配置檢視解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 字首 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 字尾 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
env.properties
#客戶來源
CUSTOMER_FROM_TYPE=002
#客戶行業
CUSTOMER_INDUSTRY_TYPE=001
#客戶級別
CUSTOMER_LEVEL_TYPE=006
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/crm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
log4j.properties
# 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
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>SSMzonghelainxi</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<!-- 配置監聽器載入spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置過濾器,解決post的亂碼問題 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置SpringMVC -->
<servlet>
<servlet-name>SSMzonghelainxi</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<!-- 配置springmvc什麼時候啟動,引數必須為整數 -->
<!-- 如果為0或者大於0,則springMVC隨著容器啟動而啟動 -->
<!-- 如果小於0,則在第一次請求進來的時候啟動 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SSMzonghelainxi</servlet-name>
<!-- 所有的請求都進入springMVC -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SSMzonghelainxi</servlet-name>
<!-- 所有的請求都進入springMVC -->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>