我是靠谱客的博主 自由白开水,这篇文章主要介绍SpringMVC+Spring+Hibernate+Maven框架整合,现在分享给大家,希望可以做个参考。

maven项目进行ssh框架整合


pom.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<dependencies> <!-- spring核心模块 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.8.RELEASE</version> </dependency> <!-- springmvc模块 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.8.RELEASE</version> </dependency> <!-- springaop模块 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency> <!-- spring事务模块 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.2.8.RELEASE</version> </dependency> <!-- hibernate模块 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.11.Final</version> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <!-- c3p0连接池 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- 日志依赖 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- junit测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> </dependencies>

web.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!-- 懒加载 --> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <!-- springmvc模块 --> <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:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!-- spring模块 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spring提供的编码过滤器 --> <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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

mysql.properties

复制代码
1
2
3
4
5
6
7
mysql.driver=com.mysql.jdbc.Driver mysql.url=jdbc:mysql://localhost:3306/meeting?characterEncoding=UTF-8 mysql.username=root mysql.password=root mysql.initialPoolSize=10 mysql.maxPoolSize=100 mysql.acquireIncrement=5

spring.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- spring的核心配置 --> <!-- 加载连接池配置数据 --> <context:property-placeholder location="classpath:mysql.properties" /> <!-- 配置c3p0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${mysql.driver}"></property> <property name="jdbcUrl" value="${mysql.url}"></property> <property name="user" value="${mysql.username}"></property> <property name="password" value="${mysql.password}"></property> <property name="initialPoolSize" value="${mysql.initialPoolSize}"></property> <property name="maxPoolSize" value="${mysql.maxPoolSize}"></property> <property name="acquireIncrement" value="${mysql.acquireIncrement}"></property> </bean> <!-- 负责SessionFactory对象的创建 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 注入连接池对象 --> <property name="dataSource" ref="dataSource"></property> <!-- hibernate常用配置 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQL5Dialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 扫描实体类包 --> <property name="packagesToScan" value="com.ahut.entity"></property> </bean> <!-- a.事务配置 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- b.配置事务增强 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" read-only="false" /> </tx:attributes> </tx:advice> <!-- c.aop配置 --> <aop:config> <aop:pointcut expression="execution(* com.ahut.service.*.*(..))" id="pointcut" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" /> </aop:config> <!-- service开启注解扫描 --> <context:component-scan base-package="com.ahut.service"></context:component-scan> <!-- dao开启注解扫描 --> <context:component-scan base-package="com.ahut.dao"></context:component-scan> </beans>

log4j.properties

复制代码
1
2
3
4
log4j.rootLogger =info,console log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d %p %c.%M()-%m%n

springmvc.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- springmvc的配置 --> <!-- 开启注解扫描 --> <context:component-scan base-package="com.ahut.action"></context:component-scan> </beans>

测试

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import javax.sql.DataSource; import javax.transaction.TransactionManager; import org.hibernate.SessionFactory; import org.junit.Test; import com.ahut.utils.SpringUtil; /** * @ClassName SpringTest * @Description 测试Spring配置 * @author 大白能 * @date 2016-12-29 下午17:51 */ public class SpringTest { /** * @Title pointcutTest * @Description 测试获取spring ioc容器里的对象 */ @Test public void pointcutTest() { Object obj = SpringUtil.getBean("pointcut"); System.out.println(obj); } /** * @Title txAdviceTest * @Description 测试获取spring ioc容器里的对象 */ @Test public void txAdviceTest() { Object obj = SpringUtil.getBean("txAdvice"); System.out.println(obj); } /** * @Title transactionManagerTest * @Description 测试获取spring ioc容器里的TransactionManager对象 */ @Test public void transactionManagerTest() { TransactionManager transactionManager = SpringUtil .getBean(TransactionManager.class); System.out.println(transactionManager); } /** * @Title sessionFactoryTest * @Description 测试获取spring ioc容器里的SessionFactory对象 */ @Test public void sessionFactoryTest() { SessionFactory sessionFactory = SpringUtil .getBean(SessionFactory.class); System.out.println(sessionFactory); } /** * @Title dataSourceTest * @Description 测试获取spring ioc容器里的DataSource对象 */ @Test public void dataSourceTest() { // Object obj = SpringUtil.getBean("dataSource"); // Class<DataSource> clazz = DataSource.class; DataSource dataSource = SpringUtil.getBean(DataSource.class); System.out.println(dataSource); } }

整合完成

最后

以上就是自由白开水最近收集整理的关于SpringMVC+Spring+Hibernate+Maven框架整合的全部内容,更多相关SpringMVC+Spring+Hibernate+Maven框架整合内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(77)

评论列表共有 0 条评论

立即
投稿
返回
顶部