ssm期末考试

ssm期末考试

 

 

usercontroller
package controller;

import java.util.ArrayList;
import java.util.List;

import org.omg.CORBA.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.support.PagedListHolder;
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 po.User;
import service.Userservice;

@Controller
@RequestMapping("/user")
public class Usercontroller {

@Autowired
Userservice userservice;

@RequestMapping("/queryUserByUid")
public String querUser(int uid,Model model,@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "4")int pageSize) {
PageHelper.startPage(pageNum, pageSize);
User u=userservice.queryUserByUid(uid);
List<User> ulist=new ArrayList<>();
PageInfo<User> p=new PageInfo<User>(ulist);

ulist.add(u);
model.addAttribute("ulist",ulist);
model.addAttribute("pageinfo",p);
model.addAttribute("flag","0");

return "userinfo";
}

@RequestMapping("/deleteUserByUid")
public String insertUser(int uid) {
int du=userservice.deleteUserByUid(uid) ;
return "forward:getAllUser";
}
@RequestMapping("/toupdateUser")
public String toAddUser(int uid,Model model) {

return "updateUser";
}

@RequestMapping("/updateUser")
public String updateUser(User u,Model model) {
int uu=userservice.updateUser(u);
if (uu>0) {
System.out.println("已更新");
return "forward:getAllUser";
}
else
return "error";
}

@RequestMapping("/toaddUser")
public String toAddUser() {
return "addUser";
}

@RequestMapping("/addUser")
public String addUser(User u,Model model) {
int au=userservice.addUser(u);
if (au>0)
return "forward:getAllUser";
else
return "error";
}
@RequestMapping("/getAllUser")
public String getAllUser(Model model,@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "4")int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> ulist=userservice.getAllUser();
PageInfo<User> p=new PageInfo<User>(ulist);
model.addAttribute("ulist",ulist);
model.addAttribute("pageinfo",p);
model.addAttribute("flag","1");
return "userinfo";
}

@RequestMapping("/queryByUname")
public String getAllUser(String uname,Model model,@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "4")int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> ulist=userservice.getUserByUname(uname);
PageInfo<User> p=new PageInfo<User>(ulist);
model.addAttribute("ulist",ulist);
model.addAttribute("pageinfo",p);
model.addAttribute("flag","0");
model.addAttribute("uname",uname);
return "userinfo";
}

}

Usermapper.java
package mapper;

import java.util.List;

import po.User;

public interface Usermapper {
public User queryUserByUid(int uid);///用ID查询
public int addUser(User u);//插入
public int deleteUserByUid(int uid);///用ID删除
public int updateUser(User u);//更新
public List<User> getAllUser();//获取所有
public List<User> getUserByUname(String uname);//用名字查询

}

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="mapper.Usermapper">
<select id="queryUserByUid" parameterType="Integer" resultType="po.User">
select * from usertab where uid =#{_parameter}
</select>

<select id="getAllUser" resultType="po.User">
select * from usertab
</select>

<select id="getUserByUname" parameterType="String" resultType="po.User">
select * from usertab where uname like concat('%',#{_parameter},'%')
</select>

<delete id="deleteUserByUid" parameterType="Integer" >
delete from usertab where uid=#{_parameter}
</delete>
<insert id="addUser" parameterType="po.User" >
insert into usertab(uname,age)values(#{uname},#{age})
</insert>
<update id="updateUser" parameterType="po.User" >
update usertab set uname=#{uname},age=#{age} where uid=#{uid}
</update>

</mapper>

User
package po;


public class User {
private int uid;
private String uname;
private int age;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(int uid, String uname, int age) {
super();
this.uid = uid;
this.uname = uname;
this.age = age;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [uid=" + uid + ", uname=" + uname + ", age=" + age + "]";
}
}

Userservice
package po;


public class User {
private int uid;
private String uname;
private int age;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(int uid, String uname, int age) {
super();
this.uid = uid;
this.uname = uname;
this.age = age;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [uid=" + uid + ", uname=" + uname + ", age=" + age + "]";
}
}

Userserviceimpl
package service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import mapper.Usermapper;
import po.User;

@Service
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 List<User> getAllUser() {
// TODO Auto-generated method stub
return usermapper.getAllUser();
}
@Override
public List<User> getUserByUname(String uname) {
// TODO Auto-generated method stub
return usermapper.getUserByUname(uname);
}
@Override
public int deleteUserByUid(int uid) {
// TODO Auto-generated method stub
return usermapper.deleteUserByUid(uid);
}

@Override
public int updateUser(User u) {
// TODO Auto-generated method stub
return usermapper.updateUser(u);
}
@Override
public int addUser(User u) {
// TODO Auto-generated method stub
return usermapper.addUser(u);
}


}

applicactionContext
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

<context:property-placeholder location="classpath:db.properties"/>

<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<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 name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>

<bean name="sqlSession" 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>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"></property>
</bean>

<context:component-scan base-package="service"></context:component-scan>
<context:component-scan base-package="po"></context:component-scan>


</beans>

db
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/x6
jdbc.username=root
jdbc.password=12345

log4j
# Global logging configuration
log4j.rootLogger=debug,ERROR,stdout
# MyBatis logging configuration...
log4j.logger.com.itheima=DEBUG
# 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

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">

<!--组件扫描,赛马控制器层controller包 -->
<context:component-scan base-package="controller"></context:component-scan>

<!--开启mvc注解器 -->
<mvc:annotation-driven></mvc:annotation-driven>

<!--配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>


</beans>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>p1</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>

<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
<filter-name>encode</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>encode</filter-name>
<url-pattern>/*</url-pattern>
</filter-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>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/addUser" method="post">
姓名:<input type="text" name="uname">
年龄:<input type="text" name="age">
<input type="submit" value="提交">
</form>

</body>
</html>

error.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>
error!
</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" name="uid" value="<%=request.getParameter("uid")%>" readonly="readonly">
姓名:<input type="text" name="uname">
年龄:<input type="text" name="age">

<input type="submit" value="提交">
</form>

</body>
</html>

userinfo
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body >
<center>
<form action="${pageContext.request.contextPath}/user/queryByUname">
模糊查询:<input type="text" name="uname"><input type="submit" >
<br>
</form>
<form action="${pageContext.request.contextPath}/user/queryUserByUid">
用ID查询:<input type="text" name="uid"><input type="submit" >
<br>
<a href="${pageContext.request.contextPath }/user/toaddUser">添加/</a>
<a href="${pageContext.request.contextPath }/user/getAllUser">展示全部</a>
</form>

<table border="1px" >
<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/toupdateUser?uid=${x.uid}">修改</a>/
<a href="${pageContext.request.contextPath }/user/deleteUserByUid?uid=${x.uid}">刪除</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}&uname=${uname}">上一页</a>
<c:forEach begin="1" end="${pageinfo.pages}" step="1" var="y">

<a
href="${pageContext.request.contextPath }/user/queryByUname?pageNum=${y}&uname=${uname}">${y}</a>

</c:forEach>

<a
href="${pageContext.request.contextPath }/user/queryByUname?pageNum=${pageinfo.nextPage}&uname=${uname}">下一页</a>
</c:otherwise>
</c:choose>

</center>
</body>
<!--<script >

if(window.confirm("你确定要删除吗?"))
{
console.log("跳转");
window.open();
window.location.href="${pageContext.request.contextPath}/user/deleteUser?uid="+uid;
}else
{
console.log("不删除");

}


</script>-->
</html>
————————————————
版权声明:本文为CSDN博主「Sheldon?」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sheldonds/article/details/122052602

上一篇:微服务设计指导-用“死信-延时”队列彻底解决业务超时补偿时发生的数据库“死锁”问题


下一篇:ssm整合小项目(动力节点王老师的课程学的)