JAVA学习笔记:SSJ框架集成

1. Spring集成JPA

1.1 导包


   
        org.springframework
        spring-web
        4.2.5.RELEASE
    
    
        org.springframework
        spring-webmvc
        4.2.5.RELEASE
    
    
        org.springframework
        spring-jdbc
        4.2.5.RELEASE
    
    
        org.springframework
        spring-orm
        4.2.5.RELEASE
    
    
        org.hibernate
        hibernate-core
        4.3.8.Final
    
    
        org.hibernate
        hibernate-entitymanager
        4.3.8.Final
    
    
        mysql
        mysql-connector-java
        5.1.6
    
    
        commons-dbcp
        commons-dbcp
        1.2.2
    
    
        org.springframework
        spring-test
        4.2.5.RELEASE
    
    
        org.aspectj
        aspectjweaver
        1.8.9
    
    
        com.fasterxml.jackson.core
        jackson-databind
        2.6.5
    
    
        junit
        junit
        4.12
        test
    

1.2 加载jdbc.properties

添加context、mvc和tx命名空间(mvc和tx后面会使用)




1.3 配置连接池dataSource



	
	
	
	

1.4 配置EntityManagerFactory



    
    
    
    
    
        
            
            
            
            
            
            
        
    

1.5 开启注解扫描控制器



1.6 配置事务管理器



    



service层中配置事务注解

@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class ProducteDirServiceImpl implements IProductDirService {
    @Autowired
    private IProductDirDao productDirDao;
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void add(ProductDir productDir) {
        productDirDao.add(productDir);
    }
}

2. Spring集成SpringMVC

2.1 配置web.xml

配置核心控制器,服务器编码,添加处理懒加载问题的过滤器



    characterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
        encoding
        UTF-8
    
    
        forceEncoding
        true
    


    characterEncodingFilter
    /*



    OpenEntityManagerInViewFilter
    org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter


    OpenEntityManagerInViewFilter
    /*



    contextConfigLocation
    classpath:applicationContext.xml


    org.springframework.web.context.ContextLoaderListener



    dispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
        contextConfigLocation
        classpath:applicationContext-mvc.xml
    
    1


    dispatcherServlet
    /

2.2 配置applicationContext-mvc.xml

配置SpringMVC静态资源放行、扫描@RequestMapping、视图解析器、上传解析器、拦截器等







	
	




    
    
    
    



	
	
		
		
		
		
		
		
		  
	

登录拦截器类

public class LoginInterceptor implements HandlerInterceptor {
	@Autowired
	private UserServiceImpl userServiceImpl;
	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object arg2) throws Exception {
		User user = (User) req.getSession().getAttribute("user");
		if(user==null){
			resp.sendRedirect("/login.jsp");
			return false;
		}
		return true;
	}
}

3. 解决延迟加载的异常(LazyInitializationException)

关联映射关系中设置了fetch=FetchType.LAZY 懒加载,数据返回给页面层的时候,此时Session已经关闭,导致延迟加载的数据访问异常,需要在web.xml中配置过滤器(参考2.1)
在domain配置了懒加载的实体类中添加@JsonIgnoreProperties({“hibernateLazyInitializer”, “handler”})注解

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="dir_id")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
private ProductDir productDir;

你可能感兴趣的:(JAVA学习笔记:SSJ框架集成)