Spring_SSJ集成

文章目录

    • 1 三大框架
      • 1.1 ssh
      • 1.2 ssm(最流行的框架)
      • 1.3 ssj(Spring springMvc jpa)
    • 2 Spring集成JPA
      • 2.1 加载jdbc.properties
      • 2.2 配置ApplicationContext.xml
      • 2.3 spring事物传播机制
        • 2.3.1 事物配置
    • 3 SpringMVC和spring集成或者整合
      • 3.1 配置web.xml
      • 3.2 配置ApplicationContext-mvc.xml
      • 3.3 修改web.xml,解决延迟加载的异常

1 三大框架

1.1 ssh

struts
spring
hibernate
(struts2 Spring Hibernate)

1.2 ssm(最流行的框架)

SpringMVC
Spring
MyBatis

1.3 ssj(Spring springMvc jpa)

Struts2 Spring JPA
SpringMVC Spring JPA
SpringMVC Spring spring Data JPA(spring的全家桶)

2 Spring集成JPA

<dependencies>

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-webartifactId>
    <version>4.2.5.RELEASEversion>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-webmvcartifactId>
    <version>4.2.5.RELEASEversion>
dependency>

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-jdbcartifactId>
    <version>4.2.5.RELEASEversion>
dependency>

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-ormartifactId>
    <version>4.2.5.RELEASEversion>
dependency>
<dependency>
    <groupId>org.hibernategroupId>
    <artifactId>hibernate-coreartifactId>
    <version>4.3.8.Finalversion>
dependency>
<dependency>
    <groupId>org.hibernategroupId>
    <artifactId>hibernate-entitymanagerartifactId>
    <version>4.3.8.Finalversion>
dependency>

<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <version>5.1.6version>
dependency>

<dependency>
    <groupId>commons-dbcpgroupId>
    <artifactId>commons-dbcpartifactId>
    <version>1.2.2version>
dependency>

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-testartifactId>
    <version>4.2.5.RELEASEversion>
dependency>

<dependency>
    <groupId>org.aspectjgroupId>
    <artifactId>aspectjweaverartifactId>
    <version>1.8.9version>
dependency>

<dependency>
    <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-databindartifactId>
    <version>2.6.5version>
dependency>

<dependency>
    <groupId>junitgroupId>
    <artifactId>junitartifactId>
    <version>4.11version>
    <scope>testscope>
dependency>
dependencies>

2.1 加载jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///ssj
jdbc.username=root
jdbc.password=root

2.2 配置ApplicationContext.xml


<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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    
    <context:component-scan base-package="cn.itsource" />
    
    
    <context:property-placeholder location="classpath:jdbc.properties" />
    
    
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        
        
        
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    bean>

    
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        
        <property name="dataSource" ref="dataSource" />
        
        <property name="packagesToScan" value="cn.itsource.domain" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                
                <property name="showSql" value="true" />
                
                <property name="generateDdl" value="true" />
                
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
            bean>
        property>
    bean>

    
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    bean>
    
    <tx:annotation-driven />

beans>

2.3 spring事物传播机制

事物的传播机制 Propagation
Propagation.REQUIRED
表示当前方法必须运行在事物中

Propagation.SUPPORTS
表示当前不需要事物,如果存在当前事物 那么就在这个事物中运行

Propagation.MANDATORY
表示该方法必须在事务中运行,如果当前事务不存在,则会抛出一个异常

Propagation.REQUIRES_NEW
表示当前方法必须运行在它自己的事务中。一个新的事务将被启动。如果存在当前事务,在该方法执行期间,
当前事务会被挂起。如果使用JTATransactionManager的话,则需要访问TransactionManager

Propagation.NOT_SUPPORTED
表示该方法不应该运行在事务中。如果存在当前事务,在该方法运行期间,当前事务将被挂起。
如果使用JTATransactionManager的话,则需要访问TransactionManager

Propagation.NEVER
表示当前方法不应该运行在事务上下文中。如果当前正有一个事务在运行,则会抛出异常

Propagation.NESTED
表示如果当前已经存在一个事务,那么该方法将会在嵌套事务中运行。嵌套的事务可以独立于当前事务进行单独地提交或回滚。
如果当前事务不存在,那么其行为与PROPAGATION_REQUIRED一样。注意各厂商对这种传播行为的支持是有所差异的。

2.3.1 事物配置

readOnly = true
只读

@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class ProductServiceImpl implements IProductService

如果自身的方法配置传播机制 方法上面就采用自己配置注解

@Transactional(propagation = Propagation.REQUIRED)
public void save(Product product) {
    productDao.save(product);
}

3 SpringMVC和spring集成或者整合

3.1 配置web.xml

导入jar --spring-web /spring-webmvc

  1. 配置springmvc配置文件
  2. 配置前缀 后缀 (视图解析器)
  3. 静态资源放行
  4. 上传解析器
  5. 扫描@RequestMapping注解
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    
    <filter>
        <filter-name>OpenEntityManagerInViewFilterfilter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilterfilter-class>
    filter>

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


    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:ApplicationContext.xmlparam-value>
    context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <filter>
        <filter-name>characterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        <init-param>
            <param-name>forceEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>characterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>


    
    <servlet>
        <servlet-name>dispatcherServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:ApplicationContext-mvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServletservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
web-app>

3.2 配置ApplicationContext-mvc.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="cn.itsource.web.controller" />

    
    <mvc:default-servlet-handler />

    
    <mvc:annotation-driven />

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    bean>
beans>

3.3 修改web.xml,解决延迟加载的异常


<filter>
	<filter-name>OpenEntityManagerInViewFilterfilter-name>
	<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilterfilter-class>
filter>

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

在Product类里面配置

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

你可能感兴趣的:(java_SSJ,学习)