项目二总结

SSJ(Spring+SpringMvc+JPA)集成

一. pom.xml(导包,插件配置)

导包

1<dependencies>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>4.2.5.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>4.2.5.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>4.2.5.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-orm</artifactId>
  <version>4.2.5.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>4.3.8.Final</version>
</dependency>

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>4.3.8.Final</version>
</dependency>

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.6</version>
</dependency>

<dependency>
  <groupId>commons-dbcp</groupId>
  <artifactId>commons-dbcp</artifactId>
  <version>1.2.2</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>4.2.5.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.8</version>
</dependency>

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.6.5</version>
</dependency>

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>
<dependencies>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.2.5.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.2.5.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.2.5.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>4.2.5.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>4.3.8.Final</version>
    </dependency>

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>4.3.8.Final</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>

    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.2.2</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.2.5.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.8</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.6.5</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

更改JDK版本
org.apache.maven.plugins maven-compiler-plugin 3.1 1.8 1.8

二. SpringMVC+JPA的集成
2.1 创建jdbc.properties文件(数据库连接信息)

1
2
3
4

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

2.2 创建applicationContext.xml
2.2.1 配置数据源

<context:property-placeholder location=“classpath:jdbc.properties”/>

2.2.2 配置entityManagerFactory

        </bean>
    </property>

</bean>

2.2.3 扫描Spring的注解

<context:component-scan base-package=“cn.pj.ssj.dao.impl”/>
<context:component-scan base-package=“cn.pj.ssj.service.impl”/>

2.3 dao层

@Repository
public class ProductDaoImpl implements IProductDao {
/**
* PersistenceContext:持久上下文
* 上下文(一次请求(线程))只有一个EntityManager
* xml配置的是LocalContainerEntityManagerFactoryBean,如果上下文中没有EntityManager,就创建
* 如果有,就直接拿
*/
@PersistenceContext
private EntityManager entityManager;
@Override
public void save(Product product) {
entityManager.persist(product);
}

2.4 事务管理器配置

<tx:annotation-driven transaction-manager=“transactionManager”/>

2.5 service层

事务加在service层

propagation:事务传播机制

    REQUIRED(默认配置):如果调用我的方法是有事务的,那么我们就使用同一个事务
    REQUIRES_NEW: 不管谁调用我,我都会开一个自己的新事务
    NEVER: 绝对不开事务,看到事务就报错给你看
    SUPPORTS: 支持(调用我的方法有事务,就有,如果没有事务,就算了)
    @Service
    @Transactional//表示这个类的所有方法加上事务
    public class ProductServiceImpl implements IProductService {

查询不加事务


//表示不加事务
    /**
     * 
     */
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public Product findOne(Serializable id) {
        return productDao.findOne(id);
    }

三. Spring+SpringMVC集成

上面已经集成了JPA了,在此基础上集成SpringMVC
3.1 创建applicationContext-mvc.xml

<context:component-scan base-package=“cn.pj.ssj.controller”/>

mvc:default-servlet-handler/

mvc:annotation-driven/

3.2 web.xml配置

注意:要用2.4版本以上的,否则不支持EL表达式
<?xml version="1.0" encoding="UTF-8"?>

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

<!--springmvc核心控制器配置-->
dispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:applicationContext-mvc.xml 1 dispatcherServlet /
<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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值