UserController.java
package com.itheima.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 com.itheima.po.User;
import com.itheima.service.UserService;
@Controller
@RequestMapping("/user")
public class UserConller {
@Autowired
private UserService userService;
//通过学号查询
@RequestMapping("/queryUserByUid")
public String queryUserByUid(int sno,Model model) {
User user=userService.queryUserByUid(sno);
System.out.println(user);
model.addAttribute("user",user);
return "userinfo";
}
//查询所有信息
@RequestMapping("/getAllUser")
public String queryall(Model model,@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "2")int pageSize) {
PageHelper.startPage(pageNum, pageSize);//开始分页
List<User> u=userService.queryall();//查询所有用户信息
PageInfo<User> p=new PageInfo<User>(u);//封装了分页后的信息
model.addAttribute("ulist",u);
model.addAttribute("pageinfo",p);
model.addAttribute("flag", 1);
return "userinfo";
}
//通过姓名查询
@RequestMapping("/queryByUname")
public String queryByUname(String sname,Model model,@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "2")int pageSize) {
PageHelper.startPage(pageNum, pageSize);//开始分页
//PageHelper.startPage(1, 2);
List<User> u1=userService.queryByUname(sname);//查询所有用户信息
PageInfo<User> p1=new PageInfo<User>(u1);//封装了分页后的信息
model.addAttribute("ulist",u1);
model.addAttribute("pageinfo",p1);
model.addAttribute("sname", sname);
model.addAttribute("flag", 0);
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("/delUser")
public String delUser(int sno) {
int t=userService.delUser(sno);
if(t>0) {
return "forward:getAllUser";
}else {
return "erro";
}
}
//修改用户信息
@RequestMapping("/update")
public String update(int sno,Model model) {
User user=userService.queryUserByUid(sno);
model.addAttribute("user",user);
return "updateUser";
}
@RequestMapping("/updateUser")
public String updateUser(User user) {
int t=userService.updateUser(user);
if(t>0) {
return "forward:getAllUser";
}else {
return "erro";
}
}
}
UserMapper.java:
package com.itheima.mapper;
import java.util.List;
import com.itheima.po.User;
public interface UserMapper {
//通过用户id查找用户
public User queryUserByUid(int sno);
public List<User> queryall();
public List<User> queryByUname(String sname);
public int addUser(User user);
public int delUser(int sno);
public int updateUser(User user);
}
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="com.itheima.mapper.UserMapper">
<select id="queryUserByUid" parameterType="Integer" resultType="com.itheima.po.User">
select * from student where sno=#{_parameter}
</select>
<select id="queryall" resultType="com.itheima.po.User">
select * from student
</select>
<select id="queryByUname" parameterType="String" resultType="com.itheima.po.User">
select * from student where sname like concat('%',#{_parameter},'%')
</select>
<insert id="addUser" parameterType="com.itheima.po.User">
insert into student(sname,age) values(#{sname},#{age})
</insert>
<delete id="delUser">
delete from student where sno=#{_parameter}
</delete>
<update id="updateUser" parameterType="com.itheima.po.User">
update student set sname=#{sname},age=#{age} where sno=#{sno}
</update>
</mapper>
User.java
package com.itheima.po;
public class User {
private Integer sno;
private String sname;
private Integer age;
public User(Integer sno, String sname, Integer age) {
super();
this.sno = sno;
this.sname = sname;
this.age = age;
}
public User() {
super();
}
public Integer getSno() {
return sno;
}
public void setSno(Integer sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User [sno=" + sno + ", sname=" + sname + ", age=" + age + "]";
}
}
UserService.java
package com.itheima.service;
import java.util.List;
import com.itheima.po.User;
public interface UserService {
public User queryUserByUid(int sno);
public List<User> queryall();
public List<User> queryByUname(String sname);
public int addUser(User user);
public int delUser(int sno);
public int updateUser(User user);
}
UserServiceImpl.java
package com.itheima.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itheima.mapper.UserMapper;
import com.itheima.po.User;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public User queryUserByUid(int sno) {
return userMapper.queryUserByUid(sno);
}
@Override
public List<User> queryall() {
return userMapper.queryall();
}
@Override
public List<User> queryByUname(String sname) {
return userMapper.queryByUname(sname);
}
@Override
public int addUser(User user) {
return userMapper.addUser(user);
}
@Override
public int delUser(int sno) {
return userMapper.delUser(sno);
}
@Override
public int updateUser(User user) {
return userMapper.updateUser(user);
}
}
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- 读取db.properties -->
<context:property-placeholder
location="classpath:db.properties" />
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource">
<!-- 数据驱动 -->
<property name="driverClassName" value="${jdbc.driver}"></property>
<!-- 连接数据库的url -->
<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" />
<!-- 配置mybatis工厂sqlSessionfactory -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<!-- 配置分页插件 -->
<property name="dataSource" ref="dataSource"></property>
<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>
<!-- 配置mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.mapper"></property>
</bean>
<!-- 扫描service -->
<context:component-scan
base-package="com.itheima.service"></context:component-scan>
</beans>
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/stu
jdbc.username=root
jdbc.password=12345
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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<context:component-scan base-package="com.itheima.controller"></context:component-scan>
<!-- 定义视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 设置前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 设置后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 开启mvc注解,显示的装配自定义转换器 -->
<mvc:annotation-driven ></mvc:annotation-driven>
<!-- 不拦截静态页面 -->
<mvc:default-servlet-handler/>
<!-- 配置拦截器 -->
<!-- <mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/***" />拦截所有网址请求
<mvc:exclude-mapping path="/login/**" />配置不拦截的网址
<bean class="com.itheima.interceptor.Myinterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
-->
<!-- 配置静态资源的访问映射,此配置中的文件,将不被前端控制器拦截 -->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<!-- 自定义类型转换器配置 -->
<!-- <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.itheima.convert.DateConverter"></bean>
</set>
</property>
</bean> -->
</beans>
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>
<!-- 配置加载spring文件监听器 -->
<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>
<!-- 配置spring mvc前端核心控制器 -->
<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.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
addUser.jsp
<%@ 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 action="${pageContext.request.contextPath}/user/addUser" method="post">
姓名:<input type="text" name="sname"/>
年龄:<input type="text" name="age"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
erro.jsp
<%@ 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>
操作错误!
</body>
</html>
updateUser.jsp
<%@ 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 action="${pageContext.request.contextPath}/user/updateUser" method="post">
<input type="text" value="${user.sno}" name="sno" readonly="readonly"/></br>
<input type="text" value="${user.sname}" name="sname"/></br>
<input type="text" value="${user.age}" name="age" />
<input type="submit" value="提交" />
</form>
</body>
</html>
userinfo.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/queryByUname"
method="post">
<input type="text" name="sname" placeholder="请输入姓名关键字进行搜索" /> <input
type="submit" value="提交" />
<a href="${pageContext.request.contextPath}/user/toAddUser">添加</a>
</form>
<table border="1">
<tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>操作</td>
</tr>
<c:forEach items="${ulist}" var="x">
<tr>
<td>${x.sno}</td>
<td>${x.sname}</td>
<td>${x.age}</td>
<td><a href="${pageContext.request.contextPath}/user/update?sno=${x.sno}">修改/</a><a href="javascript:del(${x.sno})">删除</a></td>
</tr>
</c:forEach>
</table>
第${pageinfo.pageNum}页/共${pageinfo.pages}页
<c:choose>
<c:when test="${flag==1}">
<a
href="${pageContext.request.contextPath}/user/getAllUser?pageNum=${pageinfo.prePage}">上一页</a>
<c:forEach begin="1" end="${pageinfo.pages}" step="1" var="y">
<a
href="${pageContext.request.contextPath }/user/getAllUser?pageNum=${y}">${y}</a>
</c:forEach>
<a
href="${pageContext.request.contextPath }/user/getAllUser?pageNum=${pageinfo.nextPage}">下一页</a>
</c:when>
<c:otherwise>
<a
href="${pageContext.request.contextPath}/user/queryByUname?pageNum=${pageinfo.prePage}&sname=${sname}">上一页</a>
<c:forEach begin="1" end="${pageinfo.pages}" step="1" var="y">
<a
href="${pageContext.request.contextPath }/user/queryByUname?pageNum=${y}&sname=${sname}">${y}</a>
</c:forEach>
<a
href="${pageContext.request.contextPath }/user/queryByUname?pageNum=${pageinfo.nextPage}&sname=${sname}">下一页</a>
</c:otherwise>
</c:choose>
</body>
<script >
function del(sno){
if(window.confirm("确定要删除吗?")){
//console.log("跳转");
//window.open();
window.location.href="${pageContext.request.contextPath}/user/delUser?sno="+sno;
}else{
console.log();
}
}
</script>
</html>