搭建Spring+SpringMVC+Hibernate的框架的思路如下:

     1、创建Maven项目,按需映入Maven包依赖。

     2、搭建Spring:配置Spring对控件层Bean的注入。

     3、搭建Hibernate:配置数据源,配置SessionFactory,配置事务。

     4、映入SpringMVC:配置SpringMVC配置信息。

     5、配置web.xml容器

     6、测试三个框架的整合:。

一、创建Maven项目、导入依赖

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3     <modelVersion>4.0.0</modelVersion>
  4     <groupId>com.li</groupId>
  5     <artifactId>SSHDemo</artifactId>
  6     <packaging>war</packaging>
  7     <version>0.0.1-SNAPSHOT</version>
  8     <name>SSHDemo Maven Webapp</name>
  9     <url>http://maven.apache.org</url>
 10     <dependencies>
 11         <dependency>
 12             <groupId>junit</groupId>
 13             <artifactId>junit</artifactId>
 14             <version>3.8.1</version>
 15             <scope>test</scope>
 16         </dependency>
 17         <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
 18         <dependency>
 19             <groupId>org.springframework</groupId>
 20             <artifactId>spring-context</artifactId>
 21             <version>5.0.7.RELEASE</version>
 22         </dependency>
 23 
 24         <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
 25         <dependency>
 26             <groupId>org.springframework</groupId>
 27             <artifactId>spring-beans</artifactId>
 28             <version>5.0.7.RELEASE</version>
 29         </dependency>
 30         <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
 31         <dependency>
 32             <groupId>org.springframework</groupId>
 33             <artifactId>spring-core</artifactId>
 34             <version>5.0.7.RELEASE</version>
 35         </dependency>
 36         <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
 37         <dependency>
 38             <groupId>org.springframework</groupId>
 39             <artifactId>spring-web</artifactId>
 40             <version>5.0.7.RELEASE</version>
 41         </dependency>
 42         <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
 43         <dependency>
 44             <groupId>org.springframework</groupId>
 45             <artifactId>spring-webmvc</artifactId>
 46             <version>5.0.7.RELEASE</version>
 47         </dependency>
 48         <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
 49         <dependency>
 50             <groupId>org.springframework</groupId>
 51             <artifactId>spring-aop</artifactId>
 52             <version>5.0.7.RELEASE</version>
 53         </dependency>
 54         <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
 55         <dependency>
 56             <groupId>org.springframework</groupId>
 57             <artifactId>spring-jdbc</artifactId>
 58             <version>5.0.7.RELEASE</version>
 59         </dependency>
 60         <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
 61         <dependency>
 62             <groupId>org.springframework</groupId>
 63             <artifactId>spring-context-support</artifactId>
 64             <version>5.0.7.RELEASE</version>
 65         </dependency>
 66         <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
 67         <dependency>
 68             <groupId>org.springframework</groupId>
 69             <artifactId>spring-expression</artifactId>
 70             <version>5.0.7.RELEASE</version>
 71         </dependency>
 72         <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
 73         <dependency>
 74             <groupId>mysql</groupId>
 75             <artifactId>mysql-connector-java</artifactId>
 76             <version>5.1.46</version>
 77         </dependency>
 78         <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
 79         <dependency>
 80             <groupId>commons-logging</groupId>
 81             <artifactId>commons-logging</artifactId>
 82             <version>1.2</version>
 83         </dependency>
 84         <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
 85         <dependency>
 86             <groupId>org.hibernate</groupId>
 87             <artifactId>hibernate-core</artifactId>
 88             <version>5.3.1.Final</version>
 89         </dependency>
 90         <!-- https://mvnrepository.com/artifact/org.hibernate.common/hibernate-commons-annotations -->
 91         <dependency>
 92             <groupId>org.hibernate.common</groupId>
 93             <artifactId>hibernate-commons-annotations</artifactId>
 94             <version>5.0.3.Final</version>
 95         </dependency>
 96         <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 -->
 97         <dependency>
 98             <groupId>org.hibernate</groupId>
 99             <artifactId>hibernate-c3p0</artifactId>
100             <version>5.3.1.Final</version>
101         </dependency>
102         <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
103         <dependency>
104             <groupId>dom4j</groupId>
105             <artifactId>dom4j</artifactId>
106             <version>1.6.1</version>
107         </dependency>
108         <!-- https://mvnrepository.com/artifact/com.mchange/mchange-commons-java -->
109         <dependency>
110             <groupId>com.mchange</groupId>
111             <artifactId>mchange-commons-java</artifactId>
112             <version>0.2.15</version>
113         </dependency>
114         <!-- https://mvnrepository.com/artifact/org.jboss.logging/jboss-logging -->
115         <dependency>
116             <groupId>org.jboss.logging</groupId>
117             <artifactId>jboss-logging</artifactId>
118             <version>3.3.2.Final</version>
119         </dependency>
120         <!-- https://mvnrepository.com/artifact/org.jboss.spec.javax.transaction/jboss-transaction-api_1.2_spec -->
121         <dependency>
122             <groupId>org.jboss.spec.javax.transaction</groupId>
123             <artifactId>jboss-transaction-api_1.2_spec</artifactId>
124             <version>1.0.0.Final</version>
125         </dependency>
126         <!-- https://mvnrepository.com/artifact/com.fasterxml/classmate -->
127         <dependency>
128             <groupId>com.fasterxml</groupId>
129             <artifactId>classmate</artifactId>
130             <version>1.4.0</version>
131         </dependency>
132         <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
133         <dependency>
134             <groupId>org.springframework</groupId>
135             <artifactId>spring-orm</artifactId>
136             <version>5.0.7.RELEASE</version>
137         </dependency>
138         <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
139         <dependency>
140             <groupId>com.mchange</groupId>
141             <artifactId>c3p0</artifactId>
142             <version>0.9.5.2</version>
143         </dependency>
144         <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
145         <dependency>
146             <groupId>org.springframework</groupId>
147             <artifactId>spring-test</artifactId>
148             <version>5.0.7.RELEASE</version>
149             <scope>test</scope>
150         </dependency>
151         <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
152         <dependency>
153             <groupId>org.springframework</groupId>
154             <artifactId>spring-tx</artifactId>
155             <version>5.0.7.RELEASE</version>
156         </dependency>
157         <!-- https://mvnrepository.com/artifact/org.eclipse.persistence/javax.persistence -->
158         <dependency>
159             <groupId>org.eclipse.persistence</groupId>
160             <artifactId>javax.persistence</artifactId>
161             <version>2.2.0</version>
162         </dependency>
163         <!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
164         <dependency>
165             <groupId>org.javassist</groupId>
166             <artifactId>javassist</artifactId>
167             <version>3.22.0-GA</version>
168         </dependency>
169         <!-- https://mvnrepository.com/artifact/org.jboss/jandex -->
170         <dependency>
171             <groupId>org.jboss</groupId>
172             <artifactId>jandex</artifactId>
173             <version>2.0.5.Final</version>
174         </dependency>
175         <!-- https://mvnrepository.com/artifact/com.fasterxml/classmate -->
176         <dependency>
177             <groupId>com.fasterxml</groupId>
178             <artifactId>classmate</artifactId>
179             <version>1.4.0</version>
180         </dependency>
181         <!-- https://mvnrepository.com/artifact/antlr/antlr -->
182         <dependency>
183             <groupId>antlr</groupId>
184             <artifactId>antlr</artifactId>
185             <version>2.7.7</version>
186         </dependency>
187         <!-- https://mvnrepository.com/artifact/net.bytebuddy/byte-buddy -->
188         <dependency>
189             <groupId>net.bytebuddy</groupId>
190             <artifactId>byte-buddy</artifactId>
191             <version>1.8.12</version>
192         </dependency>
193 
194     </dependencies>
195     <build>
196         <finalName>SSHDemo</finalName>
197     </build>
198 </project>

二、配置web.xml

1.配置Spring的IOC容器

1     <context-param>
2         <param-name>contextConfigLocation</param-name>
3         <param-value>classpath:applicationContext.xml</param-value>
4     </context-param>
5     <listener>
6         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
7     </listener>

  在src/main/resources下新建文件config,将所有的配置文件放在里面。创建spring配置文件applicationContext.xml

2.配置SpringMVC的核心控制器

 1     <servlet>
 2         <servlet-name>dispatcherServlet</servlet-name>
 3         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 4         <init-param>
 5             <param-name>contextConfigLocation</param-name>
 6             <param-value>classpath:springmvc.xml</param-value>
 7         </init-param>
 8         <load-on-startup>1</load-on-startup>
 9     </servlet>
10     <servlet-mapping>
11         <servlet-name>dispatcherServlet</servlet-name>
12         <url-pattern>/*</url-pattern>
13     </servlet-mapping>

  在config目录中创建SpringMVC的配置文件springmvc.xml

3.配置编码方式(过滤器),这个过滤器必须放在所有过滤器前。

 1     <filter>
 2         <filter-name>characterEncodingFilter</filter-name>
 3         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 4         <init-param>
 5             <param-name>encoding</param-name>
 6             <param-value>UTF-8</param-value>
 7         </init-param>
 8     </filter>
 9     <filter-mapping>
10         <filter-name>characterEncodingFilter</filter-name>
11         <url-pattern>/*</url-pattern>
12     </filter-mapping>

4.为了使用SpringMVC框架实现REST,需配置HiddenHttpMethodFilter。

1     <filter>
2         <filter-name>hiddenHttpMethodFilter</filter-name>
3         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
4     </filter>
5     <filter-mapping>
6         <filter-name>hiddenHttpMethodFilter</filter-name>
7         <url-pattern>/*</url-pattern>
8     </filter-mapping>

web.xml

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_1529217958650">
 6     <!-- 配置Spring IOC 容器 -->
 7     <context-param>
 8         <param-name>contextConfigLocation</param-name>
 9         <param-value>classpath:applicationContext.xml</param-value>
10     </context-param>
11     <listener>
12         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
13     </listener>
14 
15     <!-- 配置Springmvc的核心控制器 -->
16     <servlet>
17         <servlet-name>dispatcherServlet</servlet-name>
18         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
19         <init-param>
20             <param-name>contextConfigLocation</param-name>
21             <param-value>classpath:springmvc.xml</param-value>
22         </init-param>
23         <load-on-startup>1</load-on-startup>
24     </servlet>
25     <servlet-mapping>
26         <servlet-name>dispatcherServlet</servlet-name>
27         <url-pattern>/*</url-pattern>
28     </servlet-mapping>
29     <!-- 配置编码方式过滤器,且必须配置在所有的过滤器最前面 -->
30     <filter>
31         <filter-name>characterEncodingFilter</filter-name>
32         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
33         <init-param>
34             <param-name>encoding</param-name>
35             <param-value>UTF-8</param-value>
36         </init-param>
37     </filter>
38     <filter-mapping>
39         <filter-name>characterEncodingFilter</filter-name>
40         <url-pattern>/*</url-pattern>
41     </filter-mapping>
42     <!-- 为了使用SpringMVC框架实现REST,需配置HiddenHttpMethodFilter -->
43     <filter>
44         <filter-name>hiddenHttpMethodFilter</filter-name>
45         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
46     </filter>
47     <filter-mapping>
48         <filter-name>hiddenHttpMethodFilter</filter-name>
49         <url-pattern>/*</url-pattern>
50     </filter-mapping>
51     
52 </web-app>

 

三、配置SpringMVC

1.配置自动扫描的包

1     <context:component-scan base-package="com.Spittr.dao.impl"
2         use-default-filters="false">
3         <context:include-filter type="annotation"
4             expression="org.springframework.stereotype.Controller" />
5         <context:include-filter type="annotation"
6             expression="org.springframework.web.bind.annotation.ControllerAdvice" />
7     </context:component-scan>

2.配置视图解析器

  应用中所有的视图即JSP文件都放入WEB-INF下的view文件夹中。

1     <bean id="viewResolver"
2         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
3         <property name="prefix" value="/WEB-INF/view/">
4         </property>
5         <property name="suffix" value=".jsp">
6         </property>
7     </bean>

3.配置静态资源

1 <mvc:default-servlet-handler/>

4.配置注解

1 <mvc:annotation-driven></mvc:annotation-driven>

springmvc.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 8     http://www.springframework.org/schema/context 
 9     http://www.springframework.org/schema/context/spring-context-3.0.xsd
10     http://www.springframework.org/schema/aop
11     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
12     http://www.springframework.org/schema/tx 
13     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
14     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
15 
16     <!-- 配置自动扫描的包 -->
17     <context:component-scan base-package="com.Spittr.dao.impl"
18         use-default-filters="false">
19         <context:include-filter type="annotation"
20             expression="org.springframework.stereotype.Controller" />
21         <context:include-filter type="annotation"
22             expression="org.springframework.web.bind.annotation.ControllerAdvice" />
23     </context:component-scan>
24 
25     <!-- 配置视图解析器 -->
26     <bean id="viewResolver"
27         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
28         <property name="prefix" value="/WEB-INF/view/">
29         </property>
30         <property name="suffix" value=".jsp">
31         </property>
32     </bean>
33     <!-- 配置静态资源 -->
34     <mvc:default-servlet-handler/>
35     <!-- 配置注解 -->
36     <mvc:annotation-driven></mvc:annotation-driven>
37 </beans> 

四、配置Spring

1.配置自动扫描的包

  包含Controller和ControllerAdvice注解的bean交给SpringMVC来扫描,Spring就不再扫描了,所以要把这两个注解排除在外。

1     <context:component-scan base-package="com.Spittr"
2         use-default-filters="false">
3         <!-- 去除由Springmvc负责扫描的注解 -->
4         <context:exclude-filter type="annotation"
5             expression="org.springframework.stereotype.Controller" />
6         <context:exclude-filter type="annotation"
7             expression="org.springframework.web.bind.annotation.ControllerAdvice" />
8     </context:component-scan>

2.配置数据源

数据源配置文件:db.properties,也放在config文件夹中。

1 jdbc.user=root
2 jdbc.password=123456
3 jdbc.driverClass=com.mysql.jdbc.Driver
4 jdbc.jdbcUrl=jdbc\:mysql\://localhost\:3306/spittr
1 <context:property-placeholder location="classpath:/config/db.properties" />

3.配置DataSource,使用c3p0连接池。

1     <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
2         <property name="user" value="${jdbc.user}"></property>
3         <property name="password" value="${jdbc.password}"></property>
4         <property name="driverClass" value="${jdbc.driverClass}"></property>
5         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
6     </bean>

五、配置Hibernate

1.配置SeeionFactory

1     <bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
2         id="sessionFactory">
3 
4     </bean>

在SessionFactory中需要添加以下三个property:

1.1.配置数据源

1 <property name="dataSource" ref="dataSource"></property>

1.2.配置实体包(pojo)

1 <property name="PhysicalNamingStrategy">
2    <bean class="org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl"></bean>
3 </property>
4 <property name="packagesToScan" value="com.Spittr.entity"></property>

1.3.配置Hibernate的常用属性

1 <property name="hibernateProperties">
2             <props>
3                 <!-- 数据库的方言 -->
4                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
5                 <prop key="hibernate.show_sql">true</prop>
6                 <prop key="hibernate.format_sql">true</prop>
7                 <prop key="hibernate.hbm2ddl.auto">update</prop>
8             </props>
9 </property>

2.配置hibernate的事务管理器

1 <bean class="org.springframework.orm.hibernate5.HibernateTransactionManager">
2         <property name="sessionFactory" ref="sessionFactory"></property>
3 </bean>

applicationContext.xml

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5             http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 6             http://www.springframework.org/schema/context
 7             http://www.springframework.org/schema/context/spring-context-4.1.xsd">
 8 
 9     <!-- 配置自动扫描的包 -->
10     <context:component-scan base-package="com.Spittr"
11         use-default-filters="false">
12         <!-- 去除由Springmvc负责扫描的注解 -->
13         <context:exclude-filter type="annotation"
14             expression="org.springframework.stereotype.Controller" />
15         <context:exclude-filter type="annotation"
16             expression="org.springframework.web.bind.annotation.ControllerAdvice" />
17     </context:component-scan>
18 
19     <!-- 配置数据源 -->
20     <context:property-placeholder location="classpath:/config/db.properties" />
21 
22     <!-- 配置DataSource -->
23     <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
24         <property name="user" value="${jdbc.user}"></property>
25         <property name="password" value="${jdbc.password}"></property>
26         <property name="driverClass" value="${jdbc.driverClass}"></property>
27         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
28     </bean>
29 
30     <!-- 配置SeeionFactory -->
31     <bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
32         id="sessionFactory">
33         <!-- 配置数据源 -->
34         <property name="dataSource" ref="dataSource"></property>
35         <!-- 配置实体包(pojo) -->
36         <property name="PhysicalNamingStrategy">
37             <bean
38                 class="org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl"></bean>
39         </property>
40         <property name="packagesToScan" value="com.Spittr.entity"></property>
41 
42         <!-- 配置Hibernate的常用属性 -->
43         <property name="hibernateProperties">
44             <props>
45                 <!-- 数据库的方言 -->
46                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
47                 <prop key="hibernate.show_sql">true</prop>
48                 <prop key="hibernate.format_sql">true</prop>
49                 <prop key="hibernate.hbm2ddl.auto">update</prop>
50             </props>
51         </property>
52     </bean>
53 
54     <!-- 配置hibernate的事务管理器 -->
55     <bean class="org.springframework.orm.hibernate5.HibernateTransactionManager">
56         <property name="sessionFactory" ref="sessionFactory"></property>
57     </bean>
58 </beans>

 

六、测试

1.测试是否能得到配置文件applicationContext.xml

1 private ApplicationContext ctx = null;
2 ctx = new ClassPathXmlApplicationContext("/config/applicationContext.xml");
3 System.out.println(ctx);

2.测试是否能获取数据源

1 DataSource dataSource = ctx.getBean(DataSource.class);
2 System.out.println(dataSource.getConnection().toString());

3.测试Hibernate中SessionFactory是否能获取

1 SessionFactory sessionFactory = ctx.getBean(SessionFactory.class);
2 System.out.println(sessionFactory);

4.对数据库进行操作

首先创建实体类:Spitter.java

 1 package com.Spittr.entity;
 2 
 3 import java.io.Serializable;
 4 
 5 import javax.persistence.Entity;
 6 import javax.persistence.GeneratedValue;
 7 import javax.persistence.GenerationType;
 8 import javax.persistence.Id;
 9 import javax.persistence.Table;
10 
11 @Entity
12 @Table(name = "spitter")
13 public class Spitter implements Serializable {
14     @Id
15     @GeneratedValue(strategy = GenerationType.IDENTITY)
16     int id;
17     String username;
18     String password;
19     String email;
20 
21     public Spitter() {
22         // TODO Auto-generated constructor stub
23     }
24 
25     public Spitter(String username, String password, String email) {
26         super();
27         this.username = username;
28         this.password = password;
29         this.email = email;
30     }
31 
32     public int getId() {
33         return id;
34     }
35 
36     public void setId(int id) {
37         this.id = id;
38     }
39 
40     public String getUsername() {
41         return username;
42     }
43 
44     public void setUsername(String username) {
45         this.username = username;
46     }
47 
48     public String getPassword() {
49         return password;
50     }
51 
52     public void setPassword(String password) {
53         this.password = password;
54     }
55 
56     public String getEmail() {
57         return email;
58     }
59 
60     public void setEmail(String email) {
61         this.email = email;
62     }
63 }

测试

1         Session session = sessionFactory.openSession();
2         Transaction transaction = session.beginTransaction();
3         Spitter spitter = new Spitter("222qqqq222", "123123", "123213");
4         session.save(spitter);
5         transaction.commit();
6         session.close();

testSSH.java

 1 package com.Spittr.test;
 2 
 3 import java.sql.SQLException;
 4 
 5 import javax.sql.DataSource;
 6 import javax.transaction.HeuristicMixedException;
 7 import javax.transaction.HeuristicRollbackException;
 8 import javax.transaction.RollbackException;
 9 import javax.transaction.SystemException;
10 
11 import org.hibernate.Session;
12 import org.hibernate.SessionFactory;
13 import org.hibernate.Transaction;
14 import org.junit.Test;
15 import org.springframework.context.ApplicationContext;
16 import org.springframework.context.support.ClassPathXmlApplicationContext;
17 
18 import com.Spittr.entity.Spitter;
19 
20 public class testSSH {
21 
22     private ApplicationContext ctx = null;
23 
24     @Test
25     public void testDataSource() throws SQLException, SecurityException, RollbackException, HeuristicMixedException,
26             HeuristicRollbackException, SystemException {
27 
28         ctx = new ClassPathXmlApplicationContext("/config/applicationContext.xml");
29         System.out.println(ctx);
30         // System.out.println();
31         // 测试数据源
32         DataSource dataSource = ctx.getBean(DataSource.class);
33         System.out.println(dataSource.getConnection().toString());
34 
35         // 测试SessionFactory
36         SessionFactory sessionFactory = ctx.getBean(SessionFactory.class);
37         System.out.println(sessionFactory);
38 
39         // 测试操作数据库表(session)
40         Session session = sessionFactory.openSession();
41         Transaction transaction = session.beginTransaction();
42         Spitter spitter = new Spitter("222qqqq222", "123123", "123213");
43         session.save(spitter);
44         transaction.commit();
45         session.close();
46 
47     }
48 
49 }

七、配置过程中遇到的问题

1.classpath的相关问题

前缀 说明
classpath 优先本项目class路径查找,没有的话依次在其他jar包中class路径查找,找到第一个匹配的为止
classpath* 加载到所有class路径下的配置文件,包括jar包的class路径下,最终加载所有匹配的
file 通过URL加载,来源为文件系统(注意文件读取权限)
http 通过URL加载,来源为网络

  在maven项目中,classpath对应的目录是Webapp\target\classes,也就是把配置文件都放入src/main/resources目录下即可,本例中是把config文件夹放入src/main/resources中,所以在读取config中的配置文件时,所以才会使用classpath:/config/***形式。

2.hibernate的配置

  org.hibernate.cfg.NamingStrategy接口允许你为数据库中的对象和schema 元素指定一个“命名标准”. 你可能会提供一些通过Java标识生成数据库标识或将映射定义文件中”逻辑”表/列名处理成”物理”表/列名的规则. 这个特性有助于减少冗长的映射定义文件。

  但是在Hibernate到5.1的后,hibernate.ejb.naming_strategy将不再被支持,而是被替换成了两个属性:

      • hibernate.physical_naming_strategy:隐式命名策略,使用此属性当我们使用的表或列没有明确指定一个使用的名称。 
      • hibernate.implicit_naming_strategy:物理命名策略,用于转换“逻辑名称”(隐式或显式)的表或列成一个物理名称。

①对于physical_naming_strategy有两个常用的配置:

    • org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy  
    • org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

对于PhysicalNamingStrategyStandardImpl有DefaultNamingStrategy的效果;对于SpringPhysicalNamingStrategy有

ImprovedNamingStrategy的效果。

 ②在sessionFactory的bean里配置。

 

1 <property name="PhysicalNamingStrategy">  
2   <bean class="org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl"></bean>  
3 </property>  

 

1 <property name="ImplicitNamingStrategy">  
2     <bean class="org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl" />  
3 </property>  

八、代码下载

https://github.com/lyj8330328/SSHDemo-Maven-Webapp

版权声明:本文为lyj-gyq原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/lyj-gyq/p/9193407.html