<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>mvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mvc.xmlparam-value>
init-param>
<load-on-startup>0load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>mvcservlet-name>
<url-pattern>*.dourl-pattern>
servlet-mapping>
<filter>
<filter-name>encodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring.xmlparam-value>
context-param>
<display-name>display-name>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
web-app>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.controller">context:component-scan>
<mvc:annotation-driven/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/"/>
<property name="suffix" value=".jsp"/>
bean>
beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com">context:component-scan>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://localhost:3306/ssm01">property>
<property name="username" value="root">property>
<property name="password" value="root">property>
bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="mapperLocations" value="classpath:com/mapper/*.xml">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapper">property>
bean>
beans>
package com.controller;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.ResponseBody;
import com.dto.Relation;
import com.dto.User;
import com.service.UserService;
import com.util.PageUtils;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("list")
public String list(HttpServletRequest request,Integer cpage){
if(cpage==null){
cpage=1;
}
Integer pageSize = 2;
Integer count = userService.getCount();
PageUtils pu = new PageUtils(cpage, pageSize, count);
List userList = userService.findUserList(pu);
System.out.println(userList);
request.setAttribute("userList", userList);
request.setAttribute("pu", pu);
return "list";
}
@RequestMapping("getObj")
@ResponseBody
public Map getObj(User user,HttpServletResponse response) throws IOException{
List relationList = userService.findRelationList();
User u = userService.getObj(user);
System.out.println(relationList);
System.out.println(u);
Map map = new HashMap();
map.put("u", u);
map.put("relationList", relationList);
response.setCharacterEncoding("utf-8");
return map;
}
@RequestMapping("update")
public void update(User user,HttpServletResponse response) throws IOException{
int i = userService.update(user);
System.out.println(i);
response.getWriter().print(i);
}
}
package com.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dto.Relation;
import com.dto.User;
import com.mapper.UserMapper;
import com.util.PageUtils;
@Service
@Transactional
public class UserService {
@Autowired
private UserMapper userMapper;
public List findUserList(PageUtils pu) {
return userMapper.findUserList(pu);
}
public Integer getCount() {
return userMapper.getCount();
}
public User getObj(User user) {
return userMapper.getObj(user);
}
public List findRelationList() {
return userMapper.findRelationList();
}
public int update(User user) {
return userMapper.update(user);
}
}
package com.mapper;
import java.util.List;
import com.dto.Relation;
import com.dto.User;
import com.util.PageUtils;
public interface UserMapper {
List findUserList(PageUtils pu);
Integer getCount();
User getObj(User user);
List findRelationList();
int update(User user);
}
<mapper namespace="com.mapper.UserMapper">
<resultMap type="com.dto.User" id="userMap">
<id property="userid" column="userid"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="usersex" column="usersex"/>
<result property="birthday" column="birthday"/>
<result property="usertel" column="usertel"/>
<result property="userlive" column="userlive"/>
<association property="r" column="rid" resultMap="relationMap"/>
resultMap>
<resultMap type="com.dto.Relation" id="relationMap">
<id property="rid" column="rid"/>
<result property="rname" column="rname"/>
resultMap>
<select id="findUserList" resultMap="userMap">
select a.*,b.rname from t_user a left join t_relation b on a.rid=b.rid limit ${startIndex},${pageSize}
select>
<select id="getCount" resultType="int">
select count(*) from t_user
select>
<select id="getObj" resultMap="userMap">
select a.*,b.rname from t_user a left join t_relation b on a.rid=b.rid where a.userid=${userid}
select>
<select id="findRelationList" resultType="com.dto.Relation">
select * from t_relation
select>
<update id="update">
update t_user set username=#{username},password=#{password},usersex=#{usersex},birthday=#{birthday},usertel=#{usertel},userlive=#{userlive},rid=${r.rid} where userid=${userid}
update>
mapper>
package com.dto;
public class User {
private Integer userid;
private String username;
private String password;
private String usersex;
private String birthday;
private String usertel;
private String userlive;
private Relation r;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(Integer userid, String username, String password,
String usersex, String birthday, String usertel, String userlive,
Relation r) {
super();
this.userid = userid;
this.username = username;
this.password = password;
this.usersex = usersex;
this.birthday = birthday;
this.usertel = usertel;
this.userlive = userlive;
this.r = r;
}
@Override
public String toString() {
return "User [userid=" + userid + ", username=" + username
+ ", password=" + password + ", usersex=" + usersex
+ ", birthday=" + birthday + ", usertel=" + usertel
+ ", userlive=" + userlive + ", r=" + r + "]";
}
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsersex() {
return usersex;
}
public void setUsersex(String usersex) {
this.usersex = usersex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getUsertel() {
return usertel;
}
public void setUsertel(String usertel) {
this.usertel = usertel;
}
public String getUserlive() {
return userlive;
}
public void setUserlive(String userlive) {
this.userlive = userlive;
}
public Relation getR() {
return r;
}
public void setR(Relation r) {
this.r = r;
}
}
package com.dto;
public class Relation {
private Integer rid;
private String rname;
public Relation() {
super();
// TODO Auto-generated constructor stub
}
public Relation(Integer rid, String rname) {
super();
this.rid = rid;
this.rname = rname;
}
@Override
public String toString() {
return "Relation [rid=" + rid + ", rname=" + rname + "]";
}
public Integer getRid() {
return rid;
}
public void setRid(Integer rid) {
this.rid = rid;
}
public String getRname() {
return rname;
}
public void setRname(String rname) {
this.rname = rname;
}
}
package com.util;
public class PageUtils {
private Integer cpage;//当前页
private Integer pageSize;//每页展示最大条数
private Integer count;//数据总条数
private Integer totalPage;//总页数
private Integer startIndex;//起始下标
private Integer prevPage;//上一页
private Integer nextPage;//下一页
//使用此工具类,调用这个构造器
public PageUtils(Integer cpage, Integer pageSize, Integer count) {
this.cpage = cpage;
this.pageSize = pageSize;
this.count = count;
calPrevPage();
calStartIndex();
calTotalPage();
calNextPage();
}
private void calTotalPage(){//计算总页数
this.totalPage = count/pageSize + (count%pageSize==0?0:1);
}
private void calStartIndex(){//计算起始下标
this.startIndex = (cpage-1)*pageSize;
}
private void calPrevPage(){//计算上一页
this.prevPage = cpage==1?1:(cpage-1);
}
private void calNextPage(){//计算下一页
this.nextPage = cpage.equals(totalPage)?totalPage:(cpage+1);
}
public Integer getCpage() {
return cpage;
}
public void setCpage(Integer cpage) {
this.cpage = cpage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public Integer getStartIndex() {
return startIndex;
}
public void setStartIndex(Integer startIndex) {
this.startIndex = startIndex;
}
public Integer getPrevPage() {
return prevPage;
}
public void setPrevPage(Integer prevPage) {
this.prevPage = prevPage;
}
public Integer getNextPage() {
return nextPage;
}
public void setNextPage(Integer nextPage) {
this.nextPage = nextPage;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'list.jsp' starting pagetitle>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" href="<%=basePath%>/css/index_work.css" type="text/css">link>
<script type="text/javascript" src="../js/jquery-1.8.2.js">script>
<script type="text/javascript">
function gopage(cpage){
location ="list.do?cpage="+cpage;
}
script>
head>
<body>
<table>
<tr>
<th>useridth>
<th>usernameth>
<th>passwordth>
<th>usersexth>
<th>birthdayth>
<th>usertelth>
<th>userliveth>
<th>ridth>
<th>rnameth>
<th><input type="button" value="添加">th>
tr>
<c:forEach items="${userList }" var="u">
<tr>
<th>${u.userid }th>
<th>${u.username }th>
<th>${u.password }th>
<th>${u.usersex }th>
<th>${u.birthday }th>
<th>${u.usertel }th>
<th>${u.userlive }th>
<th>${u.r.rid }th>
<th>${u.r.rname }th>
<th><input type="button" value="删除">
<input type="button" value="修改" onclick="location='<%=basePath%>/view/update.jsp?userid=${u.userid}'">th>
tr>
c:forEach>
<tr>
<th colspan="21">
<button onclick="gopage(1)">首页button>
<button onclick="gopage(${pu.prevPage})">上页button>
<button onclick="gopage(${pu.nextPage})">下页button>
<button onclick="gopage(${pu.totalPage})">尾页button>
th>
tr>
table>
body>
html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'update.jsp' starting pagetitle>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="<%=basePath%>/js/jquery-1.8.2.js">script>
<script type="text/javascript">
var userid = "${param.userid}";
if(userid==null){
userid=-1;
}
$.post(
"getObj.do",
{userid:userid},
function (data){
var relationList = data.relationList;
for ( var i in relationList) {
$("select").append("+relationList[i].rname+"");
}
var u = data.u;
$("[name='userid']").val(u.userid);
$("[name='username']").val(u.username);
$("[name='password']").val(u.password);
$("[name='usersex'][value="+u.usersex+"]").attr("checked",true);
$("[name='birthday']").val(u.birthday);
$("[name='usertel']").val(u.usertel);
$("[name='userlive']").val(u.userlive);
$("[name='r.rid']").val(u.r.rid);
},"json"
);
function sub(){
$.post(
"update.do",
$("form").serialize(),
function (message){
if(message==1){
alert("修改成功");
location="list.do";
}else{
alert("修改失败");
}
},"json"
);
}
script>
head>
<body>
<form>
<input type="hidden" name="userid"><br>
username<input type="text" name="username"><br>
password<input type="text" name="password"><br>
usersex<input type="radio" value="男" name="usersex">男
<input type="radio" value="女" name="usersex">女<br>
birthday<input type="text" name="birthday"><br>
usertel<input type="text" name="usertel"><br>
userlive<input type="text" name="userlive"><br>
relation
<select name="r.rid">
select>
<br>
form>
<input type="button" value="修改" onclick="sub()">
body>
html>
有时候,我们做错事,是因为该用脑子的时候却动用了感情。