spring 5.1.5版本(五)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

spring 5.1.5版本(一)

spring 5.1.5版本(二)

spring 5.1.5版本(三)

spring 5.1.5版本(四)

spring 5.1.5版本(五)

demo地址:https://gitee.com/gwlCode/springDemo

导包

hibernate

hibernate必要包 lib/required

spring 5.1.5版本(五)_第1张图片

hibernate jpa包 lib/jpa-metamodel-generator

06022c665e055b62fe837b0d4270d3cbe38.jpg

数据库驱动包

ce20c70d833ddc60735e0b8b66995ced597.jpg

struts2

核心包 Essential Dependencies Only/struts-2.5.20-min-lib.zip/lib

spring 5.1.5版本(五)_第2张图片

struts2整合spring插件包 All Dependencies/struts-2.5.20-lib.zip/lib/struts2-spring-plugin-2.5.20.jar

f3111ad7dc0b676b2b05eb22834192dedfd.jpg

spring

基本

spring 5.1.5版本(五)_第3张图片

整合web

f5e9c39a08fa3fb4f3f5a27939b06023674.jpg

整合aop

spring 5.1.5版本(五)_第4张图片

整合hibernate和事务

spring 5.1.5版本(五)_第5张图片

整合junit测试

997cd9e4f5a32493be26382220c7e58bf41.jpg

日记包

37216529039e154bccce7360cee0f229609.jpg

c3p0

e59d82bef731a7cea2d804f2880badc3a50.jpg

jstl

f72ee41ea40151b0b41ed2b302729a6d07f.jpg

annotations-api.jar

e0bdc0b0d6e85b287da758567e79ff9d518.jpg

整合spring到web

创建配置文件 applicationContext.xml





配置spring随项目启动 web.xml



    
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
    
        contextConfigLocation
        classpath:applicationContext.xml
    
    

启动Tomcat测试

3c180bd9ab204c44a4238ece742965f48cc.jpg

spring 5.1.5版本(五)_第6张图片

整合struts2到web

配置struts2主配置文件 struts.xml





    
        regex:.*

        
            /success.jsp
        
    

配置struts2核心过滤器 web.xml

    
    
        struts2
        org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
    
   
    
        struts2
        /*
    

整合struts2到spring

配置常量 struts.xml






    
    

    
        regex:.*

        
        
            /success.jsp
        
    

applicationContext.xml





    
    
        
    

    

整合hibernate与spring

配置applicationContext.xml

    

    
    

        
        
            
                com.mysql.jdbc.Driver
                jdbc:mysql://localhost:3306/mydatabase?characterEncoding=utf8
                
                root
                root123
                org.hibernate.dialect.MySQL5Dialect

                true
                true
                update
            
        

        
        

    

c3p0连接池

配置db.properties

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/mydatabase?characterEncoding=utf8
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root123

引入连接池到spring

    
    

    
    
        
        
        
        
    

将连接池注入给SessionFactory

    

        
        

        
            
                org.hibernate.dialect.MySQL5Dialect

                true
                true
                update
            
        

        

    

HibernateTemplate模板操作数据库

UserDaoImpl

package com.company.dao.impl;

import com.company.dao.UserDao;
import com.company.domain.User;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import java.util.List;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

    @Override
    public User getByUserName(String name) {

       //HQL
       return getHibernateTemplate().execute(new HibernateCallback() {
            @Override
            public User doInHibernate(Session session) throws HibernateException {

                String hql = "from User where name = ?0 ";
                Query query = session.createQuery(hql);
                query.setParameter(0, name);
                User user = (User) query.uniqueResult();
                return user;
            }
        });


        /*
        //Criteria
        DetachedCriteria dc = DetachedCriteria.forClass(User.class);
        dc.add(Restrictions.eq("id",id));
        List list = (List) getHibernateTemplate().findByCriteria(dc);

        if (list!=null && list.size()>0) {
            return  list.get(0);
        }
        return null;
        */

    }
}

spring中配置dao注入sessionFactory   applicationContext.xml

    
    
        
    

aop事务

配置核心事务管理器 applicationContext.xml

    
    
        
    

xml配置aop事务

配置通知

    
        
            
            
            
            
            
            
            
            
        
    

配置将通知织入目标对象

    
        
        
    

注解配置aop事务

开启注解事务

    
    

 @Transactional

package com.company.service.impl;

import com.company.dao.UserDao;
import com.company.domain.User;
import com.company.service.UserService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, readOnly = true)
public class UserServiceImpl implements UserService {

    private UserDao userDao;

    @Override
    public User getUserByPassword(User user) {
        return null;
    }

    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, readOnly = false)
    public void saveUser(User user) {
        userDao.save(user);
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
}

扩大session作用范围

为了避免使用懒加载时出现no-session问题,需要扩大session的作用范围




    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
        contextConfigLocation
        classpath:applicationContext.xml
    

    
    
        openSessionInView
        org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
    
    
    
    
        struts2
        org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
    

    
        openSessionInView
        /*
    
    
    
        struts2
        /*
    

    

 

转载于:https://my.oschina.net/gwlCode/blog/3027122

你可能感兴趣的:(spring 5.1.5版本(五))