这就是我的SSM
程序员文章站
2022-07-16 09:24:31
...
SSM
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>final</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>
<!-- 配置前端控制器 -->
<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>
</web-app>
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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 组件扫描,扫描controller包 -->
<context:component-scan
base-package="zl.controller" />
<!-- 开启mvc注解驱动 -->
<mvc:annotation-driven />
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 不拦截静态资源 -->
<mvc:default-servlet-handler />
</beans>
applicationContext.xml配置
<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"
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-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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 id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource">
<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="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven
transaction-manager="transactionManager" />
<!-- 配置mybatis工厂 -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 配置分页插件PageHelper -->
<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="zl.mapper" />
</bean>
<!-- 组件扫描,扫描service包 -->
<context:component-scan
base-package="zl.service" />
</beans>
db.properties配置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/userdb
jdbc.username=root
jdbc.password=123456
log4j.properties配置
# 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
po包
package zl.po;
public class User {
private int uid;
private String uname;
private int age;
public User() {
super();
}
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 + "]";
}
}
Mapper包
UserMapper.java(接口类)
package zl.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import zl.po.User;
public interface UserMapper {
public User queryUserByUid(int uid);//根据用户编号查询用户信息,返回一个用户对象
public List<User> getAllUser();//获取所有用户信息
public List<User> queryByUname(String uname);//输入用户姓名,模糊查询用户
public Integer addUser(User user);//添加用户
public Integer deleteUser(int uid);//根据用户编号来删除用户
public Integer 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="zl.mapper.UserMapper">
<select id="queryUserByUid" parameterType="Integer" resultType="zl.po.User">
select * from usertab where uid=#{_parameter}
</select>
<select id="getAllUser" resultType="zl.po.User">
select * from usertab
</select>
<select id="queryByUname" parameterType="String" resultType="zl.po.User">
select * from usertab where uname like concat('%',#{_parameter},'%')
</select>
<!-- 添加用户 -->
<select id="addUser" parameterType="zl.po.User">
insert into usertab(uid,uname,age) values(0,#{uname},#{age})
</select>
<!-- 删除指定编号的用户 -->
<delete id="deleteUser" parameterType="Integer">
delete from usertab where uid=#{_parameter}
</delete>
<!-- 修改用户 -->
<update id="updateUser" parameterType="zl.po.User">
update usertab set uname=#{uname},age=#{age} where uid=#{uid}
</update>
</mapper>
UserService包
UserService.java(接口类)
package zl.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zl.mapper.UserMapper;
import zl.po.User;
@Service
public interface UserService {
public User queryUserByUid(int uid);//根据用户编号查询用户信息,返回一个用户对象
public List<User> getAllUser();//获取所有用户信息
public List<User> queryByUname(String uname);//输入用户姓名,模糊查询用户
public Integer addUser(User user);//添加用户
public Integer deleteUser(int uid);//根据用户编号来删除用户
public Integer updateUser(User user);//修改用户
}
UserServiceimpl.java(实现类)
package zl.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zl.mapper.UserMapper;
import zl.po.User;
@Service
public class UserServiceimpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public User queryUserByUid(int uid) {
return userMapper.queryUserByUid(uid);
}
@Override
public List<User> getAllUser() {
return userMapper.getAllUser();
}
@Override
public List<User> queryByUname(String uname) {
return userMapper.queryByUname(uname);
}
@Override
public Integer addUser(User user) {
return userMapper.addUser(user);
}
@Override
public Integer deleteUser(int uid) {
return userMapper.deleteUser(uid);
}
@Override
public Integer updateUser(User user) {
return userMapper.updateUser(user);
}
}
controller包
package zl.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 zl.po.User;
import zl.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
UserService userService;// 自动注入Service的实例对象
@RequestMapping("/queryUserByUid")
public String queryUser(int uid, Model model) {
User u = userService.queryUserByUid(uid);// 调用service中的方法
model.addAttribute("user", u);// 保存数据并传给前端页面
return "userinfo";
}
@RequestMapping("/queryUserByUname")
public String queryByUname(String uname, Model model) {
PageHelper.startPage(1, 3);
List<User> u=userService.queryByUname(uname);
PageInfo<User> p=new PageInfo<User>(u);
model.addAttribute("ulist", u);
model.addAttribute("pageinfo", p);
return "userinfo";
}
//分页查询
@RequestMapping("/getAllUser")
public String getAllUser(Model model,@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "2") int pageSize) {// pageNum:第几页,pageSize:每页几条记录
PageHelper.startPage(pageNum, pageSize);// 开始分页,第一个参数是表示:第几页,第二个参数:表示每页显示几条记录
List<User> u = userService.getAllUser();// 查询所有用户信息
PageInfo<User> p = new PageInfo<User>(u);// 包含了分页信息的类
model.addAttribute("pageinfo", p);
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";
}
@RequestMapping("/toUpdateUser")
public String toUpdateUser(Integer uid,Model model) {//跳转到修改用户界面
User u=userService.queryUserByUid(uid);
model.addAttribute("user", u);
return "updateUser";
}
@RequestMapping("/xiugaiUser")
public String xiugaiUser(User user) {
int t=userService.updateUser(user);
if(t > 0)
return "forward:getAllUser";
else
return "erro";
}
}
jsp
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>首页</title>
</head>
<body>
<form method="post"
action="${pageContext.request.contextPath }/user/queryUserByUname">
<input type="text" name="uname" /><input type="submit" value="提交" />
<a href="${pageContext.request.contextPath}/user/toAddUser">添加</a> <br>
<br>
</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/toUpdateUser?uid=${x.uid}">修改</a> <a
href="${pageContext.request.contextPath}/user/deleteUser?uid=${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">
<a 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) {
//console.log(uid);
if (window.confirm("确定要删除吗?")) {
console.log("跳转");
//window.open();
window.location.href = "${pageContext.request.contextPath}/user/deleteUser?uid=)"
+ uid;
} else {
console.log("不删除");
}
}
</script>
</html>
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 method="post" action="${pageContext.request.contextPath}/user/addUser">
姓名:<input type="text" name="uname" /><br>
年龄:<input type="text" name="age" /><br>
<input type="submit" value="提交">
</form>
</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 method="post" action="${pageContext.request.contextPath }/user/xiugaiUser">
姓名:<input type="text" name="${user.uname }" /><br>
龄号:<input type="text" name="${user.age }" /><br>
<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>
上一篇: 第三章:类与继承(Ⅱ)