ssm整合拦截器的配置


       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.2.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
                           
    
    
    
    
    
         
         
    

     
    
         
 
           
 
           
 
               
 
                   
 
                       
 
                      
 
                        application/json;charset=UTF-8
 
                   

 
               

 
           

 
       

    

    
         
             
             
         

    





personMapper的接口

public interface PersonMapper {
    public List selectPersonByUserId(Integer wid);
    public Person selectById(Integer id);
    public void insert(Person person);
    public void delete(Integer id);
    public void update(Person person);
}





public interface ShoujiMapper {
public Shouji selectone(String name);
public void insert(Shouji shouji);
public void delete(Integer id);
public void update(Shouji shouji);
}






PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    
    
        insert into person(name,tel,email,address,wid)
        value(#{name},#{tel},#{email},#{address},#{wid})
    

    
        delete from person where id=#{id}
    

    
        update person set name=#{name},tel=#{tel},
        email=#{email},address=#{address} where id=#{id}
    

    
    







PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    
    
        insert into shouji(name,password,email)
        value(#{name},#{password},#{email})
    








package com.bawei.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.bawei.pojo.Shouji;


public class MustLoginInterceptor implements HandlerInterceptor {

    @Override
    public void afterCompletion(HttpServletRequest arg0,
            HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2, ModelAndView arg3) throws Exception {
        // TODO Auto-generated method stub
        
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
            Object arg2) throws Exception {
        // TODO Auto-generated method stub
        HttpSession session = request.getSession();
        Shouji user=(Shouji)session.getAttribute("user");
        if(user==null){
            response.sendRedirect(request.getContextPath()+"/login.jsp");
            return false;
        }else{
            return true;
        }
        
    }


}



public interface PersonService {
    public void addPerson(Person person);
    public void removePerson(Integer id);
    public void changePerson(Person person);
    public Person queryById(Integer id);
    public List queryPersonByUserId(Integer id);
}




public interface ShoujiService {
    public Shouji login(Shouji shouji);
    public void register(Shouji shouji);
    public boolean isExist(String name);
}







import java.util.List;

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

import com.bawei.dao.PersonMapper;
import com.bawei.pojo.Person;
import com.bawei.service.PersonService;

@Service
@Transactional
public class PersonServiceImpl implements PersonService {
    @Autowired
    private PersonMapper pm;
    @Override
    public void addPerson(Person person) {
        pm.insert(person);

    }

    @Override
    public void removePerson(Integer id) {
        pm.delete(id);

    }

    @Override
    public void changePerson(Person person) {
        pm.update(person);

    }

    @Override
    public Person queryById(Integer id) {
        return pm.selectById(id);
    }

    @Override
    public List queryPersonByUserId(Integer id) {
        return pm.selectPersonByUserId(id);
    }

}









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

import com.bawei.dao.ShoujiMapper;
import com.bawei.pojo.Shouji;
import com.bawei.service.ShoujiService;
@Service
@Transactional
public class ShoujiServiceImpl implements ShoujiService{
    @Autowired
    private ShoujiMapper sm;

    @Override
    public Shouji login(Shouji shouji) {
        Shouji user2 = sm.selectone(shouji.getName());
        if(user2!=null && user2.getPassword().equals(shouji.getPassword())){
            return user2;
        }else{
            throw new RuntimeException("用户名或密码错误!");
        }

    }
    @Override
    public void register(Shouji shouji) {
        // TODO Auto-generated method stub
        sm.insert(shouji);
    }
    @Override
    public boolean isExist(String name) {
        if(sm.selectone(name)!=null){
            return true;
        }
        return false;
    }
    

}









package com.bawei.test;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.bawei.dao.PersonMapper;
import com.bawei.dao.ShoujiMapper;
import com.bawei.pojo.Person;
import com.bawei.pojo.Shouji;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext.xml")
public class TestMapper {
    @Autowired
    private PersonMapper pm;
    @Autowired
    private ShoujiMapper sm;
   
    @Test
    public void test5(){
        List list = pm.selectPersonByUserId(1);
        for (Person person : list) {
            System.out.println(person);
        }
    }
    @Test
    public void selectAll(){
        List list = (List) sm.selectone("张三");
        for (Shouji shouji : list) {
            System.out.println(shouji);
        }
    }
 
}














你可能感兴趣的:(ssm整合拦截器的配置)