Java框架

Spring5

2018-08-10  本文已影响0人  虫儿飞ZLEI

layout: post
title: spring5
subtitle: SSH整合
date: 2018-06-17
author: ZL
header-img: img/20180617.jpg
catalog: true
tags:
- spring
- SSH


整合理论

image

导包(41个)

hibernate

  1. hibernate/lib/required
    image
  2. hibernate/lib/jpa | java persist api java的持久化规范(接口)
    image
  3. 数据库驱动
    image

struts2

  1. struts-blank.war/WEB-INF/lib/*
    image
    注意:javassist-3.18.1-GA.jar包与hibernate中的重复
  2. struts整合spring插件包
    image
    注意:这个包一旦导入,那么struts2在启动时就会寻找spring容器.找不到将会抛出异常,单独使用struts2不需要导

Spring

  1. 基本:4+2
    core|beans|context|expression|logging|log4j
  2. 整合web:web包
    spring-web
  3. 整合aop:4个
    spring-aop|spring-aspect|aop联盟|aopweaving
  4. 整合Hibernate和事务:4个
    spring-jdbc|spring-tx|c3p0|spring-orm
  5. 正junit4测试:test包
    spring-test

标签库

  1. standard.jar
  2. jstl-1.2.jar

全家福

image

整合ssh

单独配置Spring容器到web项目

applicationContext.xml

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans" 
        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.2.xsd 
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
                            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

</beans>

web.xml

<!-- 让spring随web启动而创建的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring配置文件位置参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

单独配置struts2到web项目

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>



</struts>

web.xml

<!-- struts2核心过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

整合struts2和Spring

配置常量(struts.xml)

<!--  

    struts.objectFactory = spring   将action的创建交给spring容器    


    struts.objectFactory.spring.autoWire = name spring负责装配Action依赖属性
    (默认是打开的,不用配置)
    -->
<constant name="struts.objectFactory" value="spring"></constant>

情境需求
struts2里面有很多的action,在action里面需要用到service对象,这个对象由Spring创建。

整合方式一(不推荐)

struts.xml

<!-- 整合方案1:class属性上仍然配置action的完整类名
    struts2仍然创建action,由spring负责组装Action中的依赖属性
 -->
<package name="crm" namespace="/" extends="struts-default">
  <action name="UserAction_*" class="zl.action.UserAction" method="{1}">
    <result name="success">/success.jsp</result>
  </action>
</package>

applicationContext.xml

<bean name="userService" class="zl.service.impl.UserServiceImpl"></bean>

UserAction代码


public class UserAction extends ActionSupport{

    private UserService userService;

    public String login() throws Exception {
        System.out.println(userService);
        return super.execute();
    }
    
    public void setUserService(UserService userService) {
        this.userService = userService;
    } 
}

整合方式二

struts.xml

<!-- 
 整合方案2:class属性上填写spring中action对象的BeanName
   完全由spring管理action生命周期,包括Action的创建
   注意:需要手动组装依赖属性
 -->
 <package name="crm" namespace="/" extends="struts-default">
   <action name="UserAction_*" class="userAction" method="{1}">
     <result name="success">/success.jsp</result>
   </action>
 </package>

applicationContext.xml

<bean name="userAction" class="zl.action.UserAction" scope="prototype">
  <property name="userService" ref="userService"></property>
</bean>

<bean name="userService" class="zl.service.impl.UserServiceImpl"></bean>

UserAction代码


public class UserAction extends ActionSupport{

    private UserService userService;

    public String login() throws Exception {
        System.out.println(userService);
        return super.execute();
    }
    
    public void setUserService(UserService userService) {
        this.userService = userService;
    } 
}

单独配置hibernate

导入实体类&orm元数据

image

比如:
User.java:

package zl.domain;

import java.util.HashSet;
import java.util.Set;

public class User {
    /*
     * CREATE TABLE `sys_user` (
      `user_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '用户id',
      `user_code` varchar(32) NOT NULL COMMENT '用户账号',
      `user_name` varchar(64) NOT NULL COMMENT '用户名称',
      `user_password` varchar(32) NOT NULL COMMENT '用户密码',
      `user_state` char(1) NOT NULL COMMENT '1:正常,0:暂停',
      PRIMARY KEY (`user_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
     */
    private Long user_id;
    private String user_code;
    private String user_name;
    private String user_password;
    private Character user_state;
    public Long getUser_id() {
        return user_id;
    }
    public void setUser_id(Long user_id) {
        this.user_id = user_id;
    }
    public String getUser_code() {
        return user_code;
    }
    public void setUser_code(String user_code) {
        this.user_code = user_code;
    }
    public String getUser_name() {
        return user_name;
    }
    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }
    public String getUser_password() {
        return user_password;
    }
    public void setUser_password(String user_password) {
        this.user_password = user_password;
    }
    public Character getUser_state() {
        return user_state;
    }
    public void setUser_state(Character user_state) {
        this.user_state = user_state;
    }
    @Override
    public String toString() {
        return "User [user_id=" + user_id + ", user_code=" + user_code + ", user_name=" + user_name + ", user_password="
                + user_password + "]";
    }
}

User.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="zl.domain" >
    <class name="User" table="sys_user" >
        <id name="user_id"  >
            <generator class="native"></generator>
        </id>
        <property name="user_code"  ></property>
        <property name="user_name"  ></property>
        <property name="user_password"  ></property>
        <property name="user_state"  ></property>
    
    </class>
</hibernate-mapping>

配置主配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
    
        <!-- 
        #hibernate.dialect org.hibernate.dialect.MySQLDialect
        #hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
        #hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
        #hibernate.connection.driver_class com.mysql.jdbc.Driver
        #hibernate.connection.url jdbc:mysql:///test
        #hibernate.connection.username gavin
        #hibernate.connection.password
         -->
         <!-- 数据库驱动 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
         <!-- 数据库url -->
        <property name="hibernate.connection.url">jdbc:mysql:///test01</property>
         <!-- 数据库连接用户名 -->
        <property name="hibernate.connection.username">root</property>
         <!-- 数据库连接密码 -->
        <property name="hibernate.connection.password">123456</property>
        <!-- 数据库方言
            不同的数据库中,sql语法略有区别. 指定方言可以让hibernate框架在生成sql语句时.针对数据库的方言生成.
            sql99标准: DDL 定义语言  库表的增删改查
                      DCL 控制语言  事务 权限
                      DML 操纵语言  增删改查
            注意: MYSQL在选择方言时,请选择最短的方言.
         -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        
        
        <!-- #hibernate.show_sql true 
             #hibernate.format_sql true
        -->
        <!-- 将hibernate生成的sql语句打印到控制台 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 将hibernate生成的sql语句格式化(语法缩进) -->
        <property name="hibernate.format_sql">true</property>
        <!-- 
        ## auto schema export  自动导出表结构. 自动建表
        #hibernate.hbm2ddl.auto create      自动建表.每次框架运行都会创建新的表.以前表将会被覆盖,表数据会丢失.(开发环境中测试使用)
        #hibernate.hbm2ddl.auto create-drop 自动建表.每次框架运行结束都会将所有表删除.(开发环境中测试使用)
        #hibernate.hbm2ddl.auto update(推荐使用) 自动生成表.如果已经存在不会再生成.如果表有变动.自动更新表(不会删除任何数据).
        #hibernate.hbm2ddl.auto validate    校验.不自动生成表.每次启动会校验数据库中表是否正确.校验失败.
         -->
        <property name="hibernate.hbm2ddl.auto">update</property>

        
        <mapping resource="zl/domain/Customer.hbm.xml" />
        <mapping resource="zl/domain/LinkMan.hbm.xml" />
        <mapping resource="zl/domain/User.hbm.xml" />
        
    </session-factory>
</hibernate-configuration>

测试插入一条数据

@Test
public void fun1() {
  Configuration configuration = new Configuration().configure();
  SessionFactory sessionFactory = configuration.buildSessionFactory();
  Session session = sessionFactory.openSession();
  
  Transaction transaction = session.beginTransaction();
  
  User u = new User();
  u.setUser_name("tttt");
  u.setUser_password("1234");
  session.save(u);
  
  transaction.commit();
  
  session.close();
  sessionFactory.close();
}

整合hibernate和Spring

整合需求

将sessionFactory对象交给spring容器管理

方案一

applicationContextxml

<!-- 加载配置方案1:仍然使用外部的hibernate.cfg.xml配置信息 -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  <property name="configLocation" value="classpath:hibernate.cfg.xml" ></property>
</bean>

方案二

applicationContextxml

<!-- 加载配置方案2:在spring配置中放置hibernate配置信息 -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
  <!-- 配置hibernate基本信息 -->
  <property name="hibernateProperties">
    <props>
      <!--  必选配置 -->
      <prop key="hibernate.connection.driver_class" >com.mysql.jdbc.Driver</prop>
      <prop key="hibernate.connection.url" >jdbc:mysql:///test01</prop>
      <prop key="hibernate.connection.username" >root</prop>
      <prop key="hibernate.connection.password" >123456</prop>
      <prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
      
      <!--  可选配置 -->
      <prop key="hibernate.show_sql" >true</prop>
      <prop key="hibernate.format_sql" >true</prop>
      <prop key="hibernate.hbm2ddl.auto" >update</prop>
    </props>
  </property>
  
  
  <!-- 引入orm元数据,指定orm元数据所在的包路径,spring会自动读取包中的所有配置 -->
  <property name="mappingDirectoryLocations" value="classpath:zl/domain" ></property>
  
  <!--或者不使用mappingDirectoryLocations,使用mappingLocations,在使用maven的时候,后者更好。-->
  <!-- 注入hibernate的映射文件 -->
  <property name="mappingLocations">
    <list>
      <value>classpath:com/itheima/bos/domain/*.xml</value>
    </list>
  </property>
</bean>

代码测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class hibernateTest {

    @Resource(name = "sessionFactory")
    private SessionFactory sessionFactory;

    @Test
    public void fun2() {
        Session session = sessionFactory.openSession();
        
        Transaction transaction = session.beginTransaction();
        
        User u = new User();
        u.setUser_name("oooo");
        u.setUser_password("2222");
        session.save(u);
        
        transaction.commit();
        session.close();
    }
}

Spring整合c3p0连接池

1.配置db.properties

jdbc.jdbcUrl=jdbc:mysql:///test01
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456

2.读取db.properties文件并引入连接池到spring中

<!-- 读取db.properties文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置c3p0连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
  <property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
  <property name="driverClass" value="${jdbc.driverClass}" ></property>
  <property name="user" value="${jdbc.user}" ></property>
  <property name="password" value="${jdbc.password}" ></property>
</bean>

3.将连接池注入给SessionFactory

<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
<!-- 将连接池注入到sessionFactory, hibernate会通过连接池获得连接 -->
  <property name="dataSource" ref="dataSource" ></property>
  <!-- 配置hibernate基本信息 -->
  ...
  ...

spring整合hibernate环境操作数据库

//HibernateDaoSupport 为dao注入sessionFactory
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
    
    
    @Override
    public User getByUserCode(final String usercode) {
        //HQL
        return getHibernateTemplate().execute(new HibernateCallback<User>() {
            @Override
            public User doInHibernate(Session session) throws HibernateException {
                    String hql = "from User where user_code = ? ";
                    Query query = session.createQuery(hql);
                    query.setParameter(0, usercode);
                    User user = (User) query.uniqueResult();
                return user;
            }
        });
        //Criteria
        DetachedCriteria dc = DetachedCriteria.forClass(User.class);
        
        dc.add(Restrictions.eq("user_code", usercode));
        
        List<User> list = (List<User>) getHibernateTemplate().findByCriteria(dc);
            
        if(list != null && list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }
}
<!-- dao -->
<bean name="userDao" class="cn.itcast.dao.impl.UserDaoImpl" >
  <!-- 注入sessionFactory -->
  <property name="sessionFactory" ref="sessionFactory" ></property>
</bean>

Spring AOP 事务

准备工作

<!-- 核心事务管理器 -->
<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" >
  <property name="sessionFactory" ref="sessionFactory" ></property>
</bean>

xml配置aop事务

<!-- 配置通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
  <tx:attributes>
    <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
    <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
    <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
    <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
    <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
    <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
    <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
    <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
  </tx:attributes>
</tx:advice>
<!-- 配置将通知织入目标对象
配置切点
配置切面 -->
<aop:config>
  <aop:pointcut expression="execution(* cn.itcast.service.impl.*ServiceImpl.*(..))" id="txPc"/>
  <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
</aop:config>

注解配置aop事务

<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class UserServiceImpl implements UserService{
    
    private UserDao ud;

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

    public void setUd(UserDao ud) {
        this.ud = ud;
    }

}

扩大session作用范围

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

web.xml

<!-- 扩大session作用范围
  注意: 任何filter一定要在struts的filter之前调用
 -->
 <filter>
  <filter-name>openSessionInView</filter-name>
  <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>

<filter-mapping>
  <filter-name>openSessionInView</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
上一篇下一篇

猜你喜欢

热点阅读