package cn.test.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.RequestParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.test.po.User;
import cn.test.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/queryUserByUid")
public String queryUser(int uid, Model model) {
User u = userService.queryUserByUid(uid);
model.addAttribute("user", u);
return "updataUser";
}
@RequestMapping("updataUser")
public String updataUser(User user) {
int t = userService.updataUser(user);
if (t > 0) {
return "forward:getAllUser2";
} else {
return "erro";
}
}
@RequestMapping("/getAllUser")
public String getAllUser(Model model, @RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "2") int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> u = userService.getAllUser();
PageInfo<User> p = new PageInfo<User>(u);
model.addAttribute("ulist", u);
model.addAttribute("pageinfo", p);
return "userinfo";
}
@RequestMapping("/queryUserByUname")
public String queryByUname(String uname, Model model) {
List<User> u = userService.queryByUname(uname);
model.addAttribute("ulist", u);
return "userinfo";
}
@RequestMapping("/toAddUser")
public String toAddUser() {
return "addUser";
}
@RequestMapping("/addUser")
public String addUser(User user) {
int t = userService.addUser(user);
if (t > 0) {
return "forward:getAllUser";
} else {
return "erro";
}
}
@RequestMapping("/deleteUser")
public String deleteUser(int uid) {
int t = userService.deleteUser(uid);
if (t > 0) {
return "forward:getAllUser";
} else {
return "erro";
}
}
}
---------------------------------------------------------------------------------------------
usermapper
package cn.test.mapper;
import java.util.List;
import cn.test.po.User;
public interface UserMapper {
public User queryUserByUid(int uid) ;
public int updataUser(User user);
public List<User> getAllUser() ;
public List<User> queryByUname(String uname);
public int addUser(User user);
public int deleteUser(int uid);
------------------------------------------------------------------------
usermapper.xml
<?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.test.mapper.UserMapper">
<select id="queryUserByUid" parameterType="Integer"
resultType="cn.test.po.User">
select * from userTab where uid=#{_parameter}
</select>
<update id="updataUser" parameterType="cn.test.po.User">
update mes set uname=#{uname},uage=#{uage},umark=#{umark} where uid=#{uid}
</update>
<select id="getAllUser" resultType="cn.test.po.User">
select * from usertab
</select>
<select id="queryByUnam" parameterType="String" resultType="cn.test.po.User">
select * from usertab where uname like concat('',#{-parameter},'%')
</select>
<insert id="addUser" parameterType="cn.test.po.User">
insert into usertab(uname,age) values(#{uname},#{age})
</insert>
<delete id="deleteUser" parameterType="Integer">
delete from usertab where uid=#{_parameter}
</delete>
</mapper>
----------------------------------------------------------------------------
service
package cn.test.service;
import java.util.List;
import cn.test.po.User;
public interface UserService {
public User queryUserByUid(int uid) ;
public int updataUser(User user);
public List<User> getAllUser() ;
public List<User> queryByUname(String uname) ;
public int addUser(User user);
public int deleteUser(int uid);
}
---------------------------------------------------------------------------
impl
package cn.test.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import cn.test.mapper.UserMapper;
import cn.test.po.User;
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public User queryUserByUid(int uid) {
// TODO Auto-generated method stub
return userMapper.queryUserByUid(uid);
}
@Override
public int updataUser(User user) {
return userMapper.updataUser(user);
}
@Override
public List<User> getAllUser() {
// TODO Auto-generated method stub
return userMapper.getAllUser();
}
@Override
public List<User> queryByUname(String uname) {
// TODO Auto-generated method stub
return userMapper.queryByUname(uname);
}
@Override
public int addUser(User user) {
// TODO Auto-generated method stub
return userMapper.addUser(user);
}
@Override
public int deleteUser(int uid) {
// TODO Auto-generated method stub
return userMapper.deleteUser(uid);
}
}
----------------------------------------------------------------------------------------
applicationContext
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:property-placeholder
location="classpath:db.properties" />
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven
transaction-manager="transactionManager" />
<bean id="sqlsessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="plugins">
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<props>
<prop key="dialect">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.test.mapper"></property>
</bean>
<context:component-scan
base-package="cn.test.service"></context:component-scan>
</beans>
-----------------------------------------------------------------------------------------------------------
springmvc
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value="/WEB-INF/jsp/.jsp"></property>
</bean>
<context:component-scan base-package="cn.test.controller">
</context:component-scan>
<mvc:annotation-driven ></mvc:annotation-driven>
</beans>
-----------------------------------------------------------------------------------------
addUser
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>添加用户</title>
</head>
<body>
<form method="post" action="${pageContext.request.contextPath}/user/addUser">
姓名:<input type="text" name="uname" />
年龄:<input type="text" name="age" />
<input type="submit" name="提交" />
</form>
</body>
</html>
-----------------------------------------------------------------------------------------------------
updatauser
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="${pageContext.request.contextPath}/user/updataUser">
序 号:<input type="text" readonly="readonly" value="${user.uid}" name="uid"/><br>
姓 名:<input type="text" value="${user.uname}" name="uname"/><br>
年 龄:<input type="text" value="${user.uage}" name="uage"/><br>
<input class="bt" type="submit" value="提交"/>
</form>
</body>
</html>
-----------------------------------------------------------------------------------------------------------
userinfo
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post"
action="${pageContext.request.contextPath}/user/queryUserByUname">
<input type="text" name="uname" placeholder="请输入姓名"/>
<input type="submit" name="提交" />
<a href="${pageContext.request.contextPath}/user/toAddUser">添加</a>
</form>
<table border="1px" width="500px">
<tr>
<th>用户编号</th>
<th>姓名</th>
<th>年龄</th>
<th>操作</th>
</tr>
<c:forEach items="${ulist} " var="x">
<tr>
<td>${x.uid}</td>
<td>${x.uname}</td>
<td>${x.age}</td>
<td><a href="${pageContext.request.contextPath}/user/queryUserByUid?uid=${x.uid }">修改</a>/<a href="javascript:del(${x.uid})">删除</a></td>
</tr>
</c:forEach>
</table>
第${pageinfo.pageNum }页/共${pageinfo.pages}页
<a
href="${pageContext.request.contextPath}/user/getAllUser?pageNum=${pageinfo.prePage}">上一页</a>
<c:forEach begin="1" end="${pageinfo.pages}" step="1" var="y">
<as
href="${pageContext.request.contextPath}/user/getAllUser?pageNum=${y}">${y}</a>
</c:forEach>
<a
href="${pageContext.request.contextPath}/user/getAllUser?pageNum=${pageinfo.nextPage}">下一页</a>
</body>
<script>
function del(uid){
if(window.confirm("确定要删除吗?")){
console.log("删除");
window.location.href="${pageContext.request.contextPath}/user/deleteUser?uid="+uid;
}else{
console.log("不删除");
}
}
</script>
-------------------------------------------------------------------------
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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>week13-2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>encoding</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>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param><param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value></init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
----------------------------------------------------------------------------
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/tvhs?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
---------------------------------------------------------------------------------------