bean 包
User
public class User {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
controller包
Usercontroller
package com.zr0701.controller;
import com.zr0701.bean.User;
import com.zr0701.service.UserService;
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 org.springframework.web.servlet.ModelAndView;
import sun.font.EAttribute;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/login.do")
public ModelAndView login(User user, HttpSession session){
boolean flag = userService.login(user.getName(),user.getPassword());
ModelAndView modelAndView = new ModelAndView();
if(flag){
session.setAttribute("user",user);
modelAndView.setViewName("../ok");
}
else{
modelAndView.setViewName("../failure");
}
return modelAndView;
}
@RequestMapping("/findAll.do")//..../findAll.do?name=
public ModelAndView findAll(@RequestParam(value="name",defaultValue = "")String name){
ModelAndView modelAndView= new ModelAndView();
List<User> userList = userService.searchByName(name);
System.out.println(userList);
modelAndView.addObject("userList",userList);
modelAndView.setViewName("../main");
System.out.println("modelAndView中的内容为:"+modelAndView);
return modelAndView;
}
@RequestMapping("/delete.do")
public String delete(int id){
boolean del = userService.delete(id);
if(del){
return "redirect:findAll.do";
}
return"../failure";
}
@RequestMapping("/add.do")
public String add(User user){
boolean ad = userService.add(user);
if(ad){
return"redirect:findAll.do";
}
return"../failure";
}
@RequestMapping("/findById.do")
public String findById(int id, Model model){
User user = userService.findById(id);
model.addAttribute("user",user);
return"../modify";
}
@RequestMapping("/update.do")
public String update(User user){
boolean upd = userService.update(user);
if(upd){
return"redirect:findAll.do";
}
return "../failure";
}
}
dao包
userdao
package com.zr0701.dao;
import com.zr0701.bean.User;
import java.util.List;
public interface UserDao {
User findUserByName(String name);
List<User>findAll();
int deleteById(Integer id);
int add(User user);
int update(User user);
User findUserById(Integer id);
List<User> searchByName(String name);
}
filter包
LoginFilter
package com.zr0701.filter;
import com.zr0701.bean.User;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException{
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpSession session = request.getSession();
User user = (User)session.getAttribute("user");
//request.getRequestURL();返回全路径
//request.getRequestURL();
String uri = request.getRequestURI();
System.out.println((uri.indexOf("findAll.do")));
System.out.println(uri.indexOf("login.do"));
if(user==null && uri.indexOf("login.do")==-1){
response.sendRedirect(request.getContextPath()+"/");
}
else{
filterChain.doFilter(request,response);
}
}
@Override
public void destroy(){
}
}
service包
UserServicelmpl
package com.zr0701.service.lmpl;
import com.zr0701.bean.User;
import com.zr0701.dao.UserDao;
import com.zr0701.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServicelmpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public boolean login(String name, String password) {
User user = userDao.findUserByName(name);
if (user != null && user.getPassword().equals(password)) {
return true;
}
return false;
}
@Override
public boolean delete(Integer id) {
int del = userDao.deleteById(id);
if(del>0){
return true;
}
return false;
}
@Override
public List<User> findAll() {
return userDao.findAll();
}
@Override
public boolean add(User user) {
int add = userDao.add(user);
if(add>0){
return true;
}
return false;
}
@Override
public User findById(Integer id){
return userDao.findUserById(id);
}
@Override
public boolean update(User user){
int upd = userDao.update(user);
if(upd>0){
return true;
}
return false;
}
@Override
public List<User> searchByName(String name){
return userDao.searchByName(name);
}
}
UserService
package com.zr0701.service;
import com.zr0701.bean.User;
import java.util.List;
public interface UserService {
boolean login(String name,String password);
boolean delete(Integer id);
List<User> findAll();
boolean add(User user);
User findById(Integer id);
boolean update(User user);
List<User> searchByName(String name);
}
Test
package com.zr0701;
public class Test {
public static void main(String[] args) {
String name = Thread.currentThread().getName();
System.out.println(name);
Thread1 thread1 =new Thread1();
thread1.setName("第一");
thread1.start();
Thread1 thread2 = new Thread1();
thread2.setName("222");
thread2.start();
}
}
Thread1
package com.zr0701;
public class Thread1 extends Thread{
@Override
public void run(){
for (int i=0;i<100;i++){
System.out.println(Thread.currentThread().getName()+"------"+i);
}
}
}
Mapper包
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.zr0701.dao.UserDao">
<select id="findUserByName" parameterType="String" resultType="User">
select * from user where name = #{name};
</select>
<delete id="deleteById" parameterType="Integer">
delete from user where id=#{id}
</delete>
<insert id="add" parameterType="User">
insert into user(name,password) values (#{name},#{password})
</insert>
<select id="findAll" resultType="User">
select * from user
</select>
<select id="findUserById" parameterType="Integer" resultType="User">
select * from user where id=#{id}
</select>
<update id="update" parameterType="User">
update user set name=#{name},password=#{password} where id=#{id}
</update>
<select id="searchByName" parameterType="String" resultType="User">
select * from user where name like concat('%',#{name},'%')
--
</select>
</mapper>
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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/context
http://www.springframework.org/schema/context/spring-context-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">
<!-- 1.配置数据库相关参数properties的属性:${url} -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 2.配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="2"/>
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 扫描bean包 使用别名 -->
<property name="typeAliasesPackage" value="com.zr0701.bean"></property>
<!--配置加载映射文件 UserMapper.xml-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 自动生成dao,mapper-->
<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.zr0701.dao"/>
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--自动扫描-->
<context:component-scan base-package="com.zr0701"/>
<!-- 配置事务-->
<!-- 5.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 6.开启事务注解-->
<tx:annotation-driven></tx:annotation-driven>
</beans>
db.properies
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/a?useSSL=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/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">
<!-- 1.注解扫描位置-->
<context:component-scan base-package="com.zr0701.controller" />
<!-- 2.配置映射处理和适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- 3.视图的解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/" />
<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://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"
version="3.1">
<!-- 配置加载类路径的配置文件 -->
<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>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- 解决中文乱码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 前端控制器(加载classpath:spring-mvc.xml 服务器启动创建servlet) -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.zr0701.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
</web-app>
add.jsp
新增功能
<%--
Created by IntelliJ IDEA.
User: 0MEN
Date: 2020/7/3
Time: 8:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>新增</title>
</head>
<body>
<div>
<form form action="/user/add.do" method="post">
<div>
<label for="name">用户名:</label>
<input name="name" type="text" id="name">
</div>
<div>
<label for="password">密码:</label>
<input name="password" type="text" id="password">
</div>
<div>
<input type="submit" value="提交">
</div>
<a href="javascript:window.history.go(-1)">返回</a>
</form>
</div>
</body>
</html>
failure.jsp
失败措施
<%--
Created by IntelliJ IDEA.
User: 1
Date: 2020/7/1
Time: 11:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>失败!</title>
</head>
<body>
<h1>失败</h1>
</body>
</html>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>title</title>
</head>
<body>
<h1>登陆</h1>
<form action="/user/login.do" method="post">
name:<input name="name" type="text">
password:<input name="password" type="password">
<input type="submit" value="login">
</form>
<a href="add.jsp">注册</a>
<a href="/user/findAll.do?name=">查看全部</a>
</body>
</html>
main.jsp
主页
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: 0MEN
Date: 2020/7/2
Time: 9:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>列表</title>
</head>
<body>
<div>
<form action="/user/findAll.do">
<input id="name" type=search" name="name" value="${name}">
<button onclick="form.submit()">搜索</button>
</form>
<table>
<thead>
<tr>
<th>ID</th>
<th>用户名</th>
<th>密码</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.password}</td>
<td>
<a href="${pageContext.request.contextPath}/user/delete.do?id=${user.id}">删除</a>
<a href="${pageContext.request.contextPath}/user/findById.do?id=${user.id}">修改</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</body>
</html>
modify.jsp
修改功能
<%--
Created by IntelliJ IDEA.
User: 0MEN
Date: 2020/7/3
Time: 9:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改</title>
</head>
<body>
<div>
<form form action="/user/update.do" >
<input name="id" value="${user.id}" type="hidden">
<%-- 单词拼错了,导致id没有传过去--%>
<div>
<label for="name">用户名:</label>
<input name="name" type="text" id="name" value="${user.name}">
</div>
<div>
<label for="password">密码:</label>
<input name="password" type="text" id="password" value="${user.password}">
</div>
<div>
<input type="submit" value="提交">
</div>
<a href="javascript:window.history.go(-1)">返回</a>
</form>
</div>
</body>
</html>
ok.jsp
测试功能
<%--
Created by IntelliJ IDEA.
User: 0MEN
Date: 2020/7/1
Time: 11:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a>ok!</a>
</body>
</html>