一、src/main/resources/spring
1、spring-context-bean.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:context ="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" >
<bean id="custDao" class="com.cust.mapper.impl.CustDaoImpl">
<property name="sqlSession" ref="sqlSession" />
</bean>
<bean id="custService" class="com.cust.service.impl.CustServiceImpl">
<property name="custDao" ref="custDao" />
</bean>
</beans>
2\ spring-context.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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
default-lazy-init="true">
<description>Spring Configuration</description>
<!-- jdbc -->
<context:property-placeholder location="classpath:/jdbc.properties" />
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<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>
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:/mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sessionFactory" />
</bean>
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes" value="application/json" />
</bean>
</beans>
3\springMVC-servlet
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<description>Spring Configuration</description>
<context:property-placeholder location="classpath:/jdbc.properties" />
<mvc:resources mapping="/javascript/**" location="/javascript/**" />
<context:component-scan base-package="com.cust"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
二、src/main/resources/
1\jdbc.properties
jdbc.type=mysql
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
2\log4j.properties
log4j.rootLogger=DEBUG,Console,File
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c]%m%n
log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.File=mybatis.log
log4j.appender.File.MaxFileSize=10MB
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
3\mybatis-config
<?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>
<typeAliases>
<typeAlias type="com.cust.dto.Customer" alias="Customer" />
</typeAliases>
<mappers>
<mapper resource="com/cust/mapper/custMapper.xml"/>
</mappers>
</configuration>
三、JSP
1\addCust.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>addcust</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<script type="text/javascript" src="/testCustWeb/javascript/jquery/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
function guid() {
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
return (S4()+S4()+S4()+S4());
}
var pk=guid();
$(document).ready(function(){
$("#custId").val(pk);
});
function save(){
$("#addForm").submit();
}
</script>
</head>
<body>
<form id="addForm" action="cust/saveCust" method="post">
<table id="contentTable" border="1px;" align="center">
<thead>
<tr><th id="tableTitle" colspan="2">添加客户</th></tr>
</thead>
<tbody>
<tr>
<th>用户ID</th>
<td><input type="text" name="custId" id="custId" readonly="true"></td>
</tr>
<tr>
<th>用户名称</th>
<td><input type="text" name="custName" id="custName"/></td>
</tr>
<tr>
<th>用户密码</th>
<td><input type="password" name="address" id="address"></td>
</tr>
<tr>
<th>操作</th>
<td align="center"><input type="reset" value="重置"><input type="button" value="保存" οnclick="save();"></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
2\custList.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>Customer</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<script type="text/javascript" src="<%=basePath%>javascript/jquery/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
function openwin(url,title)
{
window.open(url,title,"height=100, width=400, toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no");
}
function add(object){
var href="view/cust/addCust.jsp";
object.href=href;
}
function update(id,object){
var href="cust/initUpdate?custId="+id;
object.href=href;
}
function deleteC(id,object){
var href="cust/deleteCust?custId="+id;
object.href=href;
}
</script>
</head>
<body>
<table id="custTable" border="1px;" align="left">
<thead>
<tr id="tableColumn">
<th>客户ID</th>
<th>客户名称</th>
<th>地址</th>
<th>操作</th></tr>
</thead>
<tbody>
<c:forEach items="${custL}" var="cust">
<tr>
<td>${cust.custId}</td>
<td>${cust.custName}</td>
<td>${cust.address}</td>
<td><a href="javascript:void(0);" οnclick="add(this);">添加</a>
<a href="javascript:void(0);" οnclick="deleteC('${cust.custId}',this)">删除</a>
<a href="javascript:void(0);" οnclick="update('${cust.custId}',this)">修改</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
四、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>testCustWeb</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/spring/spring-context*.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载log4j 默认路径classpath-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:/log4j.properties</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/springMVC-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
五、java
1\CustController
package com.cust.index.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.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.cust.dto.Customer;
import com.cust.service.CustService;
@Controller
@RequestMapping("/cust/")
public class CustController {
@Autowired
private CustService custService;
@RequestMapping("/getList")
@ResponseBody
public List<Customer> getList(){
return custService.selectCustList();
}
@RequestMapping("/custList")
public String custList(Model model){
List<Customer> custL=custService.selectCustList();
model.addAttribute("custL", custL);
return "/cust/custList";
}
@RequestMapping("/initUpdate")
public String updateCust(String custId,Model model){
Customer cust=custService.selectById(custId);
model.addAttribute("cust",cust);
return "/cust/update";
}
@RequestMapping("/deleteCust")
public ModelAndView deleteCust(String custId,ModelAndView model){
custService.delete(custId);
model=getCustList();
return model;
}
@RequestMapping(value="/saveCust",method = RequestMethod.POST)
public ModelAndView add(@ModelAttribute Customer cust,ModelAndView model){
custService.save(cust);
model=getCustList();
return model;
}
@RequestMapping("/getCustList")
public ModelAndView getCustList(){
List<Customer> custL=custService.selectCustList();
ModelAndView model=new ModelAndView("/cust/custList");
model.addObject("custL", custL);
return model;
}
public CustService getCustService() {
return custService;
}
public void setCustService(CustService custService) {
this.custService = custService;
}
}
2\custMapper
<?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="cust">
<resultMap type="Customer" id="resultCust">
<result property="custId" column="cust_id"/>
<result property="custName" column="cust_name"/>
<result property="address" column="address"/>
</resultMap>
<select id="selectCustList" resultMap="resultCust" parameterType="String">
select * from cust
</select>
<select id="selectById" resultType="Customer" resultMap="resultCust" parameterType="String">
select * from cust where 1=1
<if test=" _parameter != null">
and cust_id= #{_parameter}
</if>
</select>
<delete id="deleteById" parameterType="String">
<if test=" _parameter != null">
delete from cust where cust_id= #{_parameter}
</if>
</delete>
<insert id="insert" parameterType="Customer">
insert into cust(cust_id,cust_name,address)
values(
#{custId},#{custName},#{address}
)
</insert>
<update id="update" parameterType="Customer">
update cust set cust_name=#{custName},address=#{address}
where cust_id=#{custId}
</update>
</mapper>
3\CustDaoImpl
package com.cust.mapper.impl;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.cust.dto.Customer;
import com.cust.mapper.CustDao;
public class CustDaoImpl implements CustDao{
private SqlSession sqlSession;
public SqlSession getSqlSession() {
return sqlSession;
}
public void setSqlSession(SqlSession sqlSession) {
this.sqlSession = sqlSession;
}
public List<Customer> selectCustList() {
// TODO Auto-generated method stub
List<Customer> userList=sqlSession.selectList("cust.selectCustList");
return userList;
}
public Customer selectById(String id) {
// TODO Auto-generated method stub
Customer cust=this.sqlSession.selectOne("cust.selectById",id);
return cust;
}
public int delete(String id) {
// TODO Auto-generated method stub
int resutl =this.sqlSession.delete("cust.deleteById",id);
return resutl;
}
public int save(Customer cust) {
int result=sqlSession.insert("cust.insert", cust);
return result;
}
public int update(Customer cust) {
// TODO Auto-generated method stub
int result=sqlSession.update("cust.update", cust);
return result;
}
}
4\ CustServiceImpl
package com.cust.service.impl;
import java.util.List;
import com.cust.dto.Customer;
import com.cust.mapper.CustDao;
import com.cust.service.CustService;
public class CustServiceImpl implements CustService {
private CustDao custDao;
public List<Customer> selectCustList() {
// TODO Auto-generated method stub
return custDao.selectCustList();
}
public Customer selectById(String id) {
// TODO Auto-generated method stub
return custDao.selectById(id);
}
public int delete(String id) {
// TODO Auto-generated method stub
return custDao.delete(id);
}
public int save(Customer cust) {
// TODO Auto-generated method stub
return custDao.save(cust);
}
public int update(Customer cust) {
// TODO Auto-generated method stub
return custDao.update(cust);
}
public CustDao getCustDao() {
return custDao;
}
public void setCustDao(CustDao custDao) {
this.custDao = custDao;
}
}