代码
package com.zr0701.bean;
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
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.servlet.ModelAndView;
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")
public ModelAndView findAll(){
ModelAndView modelAndView = new ModelAndView();
List<User> userList = userService.findAll();
System.out.println("userList中的内容为:"+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("/insert.do")
public ModelAndView insert(User user){
boolean flag = userService.add(user.getName(),user.getPassword());
ModelAndView modelAndView= new ModelAndView();
if (flag){
modelAndView.setViewName("../ok");
}else{
modelAndView.setViewName("../failure");
}
return modelAndView;
}
@RequestMapping("/findById.do")
public String findById(int id, Model model){
User user = userService.findById(id);
model.addAttribute("user",user);
return "../modfiy";
}
@RequestMapping("/update.do")
public String update(User user){
boolean upd=userService.update(user);
if (upd){
return "redirect:findAll.do";
}
return "../failure";
}
}
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);
}```
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");
String uri = request.getRequestURI();
System.out.println(uri.indexOf("findAll.do"));
System.out.println(uri.indexOf("login.do"));
if(user==null ||uri.indexOf("findAll.do")==-1){
response.sendRedirect(request.getContextPath()+"/");
}else{
filterChain.doFilter(request,response);
}
}
@Override
public void destroy() {
}
}
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("二");
thread2.start();
}
}
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);
}
}
}
<?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.zhongruan.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>
<?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>