(1)SSJ整合(掌握)
spring项目管理专家,所有的bean都可以交给spring管理
(1)spring和jpa整合
1) 导入依赖包
2) 配置文件applicationContext.xml
jdbc.properties --》dataSource(连接池)--》EntityManagerFactory--》EntityManager -->事务
<!-- 扫描service 可以处理@Repository, @Service, and @Controller,@Autowired,@PersistenceContext 注解 -->
<context:component-scan base-package="cn.itsource"></context:component-scan>
<!--Bean对象注入的顺序-->
<!--jdbc.properties->dataSource->entityManagerFactory->dao->service->junit->action-->
<!--读取jdbc.properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--连接池-->
<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>
<!--entityManagerFactory-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- 注入连接池-->
<property name="dataSource" ref="dataSource"></property>
<!--扫描注解 entity-->
<property name="packagesToScan" value="cn.itsource.domain"></property>
<!-- 方言 是否显示sql 建表策略-->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<!-- private boolean generateDdl = true;底层使用update策略-->
<property name="generateDdl" value="true" />
<!-- private String databasePlatform;原来方言 -->
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
</bean>
</property>
</bean>
<!-- 事务控制 id必须这样取名 底层就是找名字 -->
<!-- transaction-manager="transactionManager"默认找bean.id=transactionManager事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<!-- 扫描的注解 @Transaction-->
<tx:annotation-driven></tx:annotation-driven>
(2)spring和springmvc整合
1)导包
2)配置
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--监听器 Listener javaweb 可以监听四大作用生命周期 和 作用域属性操作
就可以读取的配置文件 contextConfigLocation 为key value applicationContext.xml
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 核心控制器配置-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<!-- 优先加载servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置过滤器 解决懒加载延迟关闭问题-->
<filter>
<filter-name>openEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--编码过滤器配置-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
applicationContext-mvc.xml
<!-- 扫描controller 静态资源放行 视图解析器 上传下载解析-->
<context:component-scan base-package="cn.itsource.controller" />
<!--静态资源放行-->
<mvc:default-servlet-handler />
<!-- 扫描@RequestMapping-->
<mvc:annotation-driven />
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
(2)声明式事务
required:如果有事务,就使用事务方式运行,如果没有事务,新创建一个事务
support:如果有事务,就使用事务方式运行,如果没有,就非事务运行
(3)解决懒加载问题
配置过滤器
<filter>
<filter-name>openEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
配置注解
@Transactional(propagation = Propagation.SUPPORTS ,readOnly = true)
public class ProductDirServiceImpl implements IProductDirService
@Transactional
public void save(ProductDir productDir) {
productDirDao.save(productDir);
}