关于Springmvc+spring+Springjpa三大框架整合

三大框架整合

很早的 — ssh(三大框架):struts2(过时) Spring hibernate(过期)–大公司
三大框架:
ssj:Spingmvc+Sping+jpa
回顾一下:
第一个项目:ssj–>Spingmvc+Sping+Spingjdbc

第二个项目: ssdj(结构 中小型的项目)–>Spingmvc+Sping+Spingdatajps

第三个项目: ssm(现在比较流行结构)–>Spingmvc+Sping+mybatis

第四个项目: Spingboot+Spingcloud+ssm

SSJ(整合)

Spring项目管理专家,所有的bean都可以交给Spring管理
(1)先整合Spring和jpa
步骤:a 导入依赖包
-----包倒不进去(网络)
----解决导包问题:
检查idea配置maven路径
在这里插入图片描述
(1)根据出现的错误,检查一下仓库里面是否已经存在该jar包
一般有错,仓库里面应该没有该jar包—有问题
删除仓库里面的文件
(2)先清理一下项目
在这里插入图片描述
(3)reimport重新导入
在这里插入图片描述

如果下载的速度实在太慢 配置阿里云镜像 setting.xml

​ b)配置

​ 配置文件applicationContext.xml

​ jdbc.properties --》dataSource(连接池)–》EntityManagerFactory–》EntityManager -->事务

​ c) 测试

(2)在整合 spring和springmvc

​ a)导包

​ b)配置 web.xml和applicationContext-mvc.xml里面配置

Spring与JPA的整合

配置JPA映射

@Entity
@Table(name = “t_product”)
public class Product {
@Id
@GeneratedValue
private Long id;
private String name;
}

Bean对象注入的顺序.

jdbc.properties->dataSource->entityManagerFactory->dao->service->junit->action

配置jdbc.properties

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

加载jdbc.properties、配置连接池

   	<!--加载jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 配置连接池dataSource bean生命周期方法 销毁方法close 用来连接之后 还给链接池-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

配置entityManagerFactory

<!-- 得到EntityManagerFactory-->
    <bean id="entityManagerFactory"
         class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <!-- 配置属性 setDataSource-->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 扫描实体类的配置 entity-->
        <property name="packagesToScan" value="cn.itsource.ssj.domain"></property>
        <property name="jpaVendorAdapter" >
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <!-- 是否显示sql-->
                <property name="showSql" value="true"></property>
                <!-- 是否创建表-->
                <property name="generateDdl" value="true"></property>
                <!--数据库方言-->
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"></property>
            </bean>
        </property>
    </bean>

ProductDaoImpl

@Repository
public class ProductDaoImpl implements IProductDao {
	// @Autowired 不能使用这个注入
	@PersistenceContext // 持久层上下文管理器
	private EntityManager entityManager;
/*@PersistenceContext
注入的是实体管理器,执行持久化操作的,需要配置文件persistence.xml。
注入一堆保存实体类状态的数据结构,针对实体类的不同状态(四种,managedh或detached等)
可以做出不同的反应(merge,persist等等),其实就是把数据从数据库里提出,然后在内存里处
理的,再返回数据库的法则。*/
/*@Resource
是注入容器提供的资源对象,比如SessionContext MessageDrivenContext。或者你那个name指定
的JNDI对象
可以理解为资源->数据源->也就是数据连接,基本上就是告诉程序数据库在哪里*/
}

组件扫描

<!-- 扫描dao、service、action组件 -->
<!-- 可以处理@Repository, @Service,@Controller,@Autowired,@PersistenceContext 注解-->
<context:component-scan base-package="cn.itsource.ssj" />

声明式事务管理(注解版本)

添加一个tx命名空间

xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd"

添加事务配置

    <!-- 事务管理器-->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"></property>
    </bean>
    <!-- 开启事务 扫描@Transaction这种注解-->
    <tx:annotation-driven/>

事务配置

默认事务配置
@Transactional
上面配置等价于下面配置
@Transactional(propagation = Propagation.REQUIRED)
涉及到事务的方法都需要加上@Transactional

Spring集成SpringMVC

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID" version="3.1">
    <filter>
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter>
        <filter-name>openSession</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>openSession</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <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>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

配置applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<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.ssj.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>
    //上传解析器
    <!--<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">-->
        <!--<property name="maxUploadSize">-->
            <!--<value>${1024*1024*10}</value>-->
        <!--</property>-->
    <!--</bean>-->
</beans>

添加一个监听器

<!-- 添加一个监听器,来实例化spring容器 -->
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

添加一个上下文的初始化参数

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext.xml</param-value>
</context-param>

解决延迟加载的异常

<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>

在Product类里面配置

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="dir_id")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
private ProductDir productDir;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值