首页 > 其他分享 >Maven搭建SpringMvc+Spring+Hibernate框架

Maven搭建SpringMvc+Spring+Hibernate框架

时间:2022-12-30 11:00:53浏览次数:44  
标签:www Hibernate http SpringMvc Spring springframework hibernate spring org


假设你已经在Myeclipse上面创建好啦一个Maven项目,现在我们开始往pom.xml里面新增jar的配置。

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/xsd/maven-4.0.0.xsd">
3. <modelVersion>4.0.0</modelVersion>
4. <groupId>yfkj.gz</groupId>
5. <artifactId>task</artifactId>
6. <version>0.0.1-SNAPSHOT</version>
7. <packaging>war</packaging>
8. <properties>
9. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10. <spring.version>4.3.5.RELEASE</spring.version>
11. <hibernate.version>5.2.5.Final</hibernate.version>
12. <jackson.version>2.8.5</jackson.version>
13. <junit.version>4.12</junit.version>
14. <ehcache.version>2.10.3</ehcache.version>
15. <log4j.version>1.2.17</log4j.version>
16. <mysql.version>5.1.37</mysql.version>
17. <druid.version>1.0.27</druid.version>
18. <aspectjweaver.version>1.8.10</aspectjweaver.version>
19. <webVersion>3.0</webVersion>
20. </properties>
21. <dependencies>
22. <!-- junit 单元测试 -->
23. <dependency>
24. <groupId>junit</groupId>
25. <artifactId>junit</artifactId>
26. <version>${junit.version}</version>
27. <scope>test</scope>
28. </dependency>
29. <!-- spring -->
30. <dependency>
31. <groupId>org.springframework</groupId>
32. <artifactId>spring-core</artifactId>
33. <version>${spring.version}</version>
34. </dependency>
35.
36. <dependency>
37. <groupId>org.springframework</groupId>
38. <artifactId>spring-beans</artifactId>
39. <version>${spring.version}</version>
40. </dependency>
41.
42. <dependency>
43. <groupId>org.springframework</groupId>
44. <artifactId>spring-context</artifactId>
45. <version>${spring.version}</version>
46. </dependency>
47.
48. <dependency>
49. <groupId>org.springframework</groupId>
50. <artifactId>spring-tx</artifactId>
51. <version>${spring.version}</version>
52. </dependency>
53.
54. <dependency>
55. <groupId>org.springframework</groupId>
56. <artifactId>spring-web</artifactId>
57. <version>${spring.version}</version>
58. </dependency>
59.
60. <dependency>
61. <groupId>org.springframework</groupId>
62. <artifactId>spring-test</artifactId>
63. <version>${spring.version}</version>
64. <scope>test</scope>
65. </dependency>
66.
67. <!-- 使用SpringMVC需配置 -->
68. <dependency>
69. <groupId>org.springframework</groupId>
70. <artifactId>spring-webmvc</artifactId>
71. <version>${spring.version}</version>
72. </dependency>
73.
74. <!-- 关系型数据库整合时需配置 如hibernate jpa等 -->
75. <dependency>
76. <groupId>org.springframework</groupId>
77. <artifactId>spring-orm</artifactId>
78. <version>${spring.version}</version>
79. </dependency>
80.
81. <!-- hibernate -->
82. <dependency>
83. <groupId>org.hibernate</groupId>
84. <artifactId>hibernate-core</artifactId>
85. <version>${hibernate.version}</version>
86. </dependency>
87.
88. <dependency>
89. <groupId>org.hibernate</groupId>
90. <artifactId>hibernate-ehcache</artifactId>
91. <version>${hibernate.version}</version>
92. </dependency>
93.
94.
95. <!-- 二级缓存ehcache -->
96. <dependency>
97. <groupId>net.sf.ehcache</groupId>
98. <artifactId>ehcache</artifactId>
99. <version>${ehcache.version}</version>
100. </dependency>
101.
102. <!-- log4j -->
103. <dependency>
104. <groupId>log4j</groupId>
105. <artifactId>log4j</artifactId>
106. <version>${log4j.version}</version>
107. </dependency>
108.
109. <!-- mysql连接 -->
110. <dependency>
111. <groupId>mysql</groupId>
112. <artifactId>mysql-connector-java</artifactId>
113. <version>${mysql.version}</version>
114. </dependency>
115.
116. <!-- 阿里巴巴数据源 -->
117. <dependency>
118. <groupId>com.alibaba</groupId>
119. <artifactId>druid</artifactId>
120. <version>${druid.version}</version>
121. </dependency>
122.
123. <dependency>
124. <groupId>com.fasterxml.jackson.core</groupId>
125. <artifactId>jackson-annotations</artifactId>
126. <version>${jackson.version}</version>
127. </dependency>
128.
129. <dependency>
130. <groupId>com.fasterxml.jackson.core</groupId>
131. <artifactId>jackson-core</artifactId>
132. <version>${jackson.version}</version>
133. </dependency>
134.
135. <dependency>
136. <groupId>com.fasterxml.jackson.core</groupId>
137. <artifactId>jackson-databind</artifactId>
138. <version>${jackson.version}</version>
139. </dependency>
140.
141. <!-- aop -->
142. <dependency>
143. <groupId>org.aspectj</groupId>
144. <artifactId>aspectjweaver</artifactId>
145. <version>${aspectjweaver.version}</version>
146. </dependency>
147.
148. <!-- servlet -->
149. <dependency>
150. <groupId>javax.servlet</groupId>
151. <artifactId>servlet-api</artifactId>
152. <version>3.0-alpha-1</version>
153. <scope>provided</scope>
154. </dependency>
155.
156. <dependency>
157. <groupId>javax.servlet</groupId>
158. <artifactId>jstl</artifactId>
159. <version>1.2</version>
160. </dependency>
161. </dependencies>
162. <build>
163. <plugins>
164. <plugin>
165. <artifactId>maven-compiler-plugin</artifactId>
166. <configuration>
167. <source>1.8</source>
168. <target>1.8</target>
169. </configuration>
170. </plugin>
171. </plugins>
172. </build>
173. </project>

我们来看看项目的结构图:

Maven搭建SpringMvc+Spring+Hibernate框架_hibernate

下面开始各种框架的配置文件

数据库config.properties的配置:

 

1. url.driver=com.mysql.jdbc.Driver  
2. url.jdbc=jdbc:mysql://192.168.1.150:3306/wms?useUnicode=true&characterEncoding=UTF-8
3. url.name=****
4. url.pass=****
5.
6. #hibernate config
7. hibernate.dialect = org.hibernate.dialect.MySQLDialect
8. hibernate.show_sql = true
9. hibernate.format_sql = true
10. hibernate.hbm2ddl.auto = update
11. hibernate.cache.use_second_level_cache = true
12. hibernate.cache.use_query_cache = true
13. hibernate.cache.region.factory_class = org.hibernate.cache.ehcache.EhCacheRegionFactory
14. hibernate.cache.provider_configuration_file_resource_path =ehcache.xml

缓存配置文件ehcache.xml:

1. <?xml version="1.0" encoding="UTF-8"?>    
2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
3. <diskStore path="java.io.tmpdir/ehcache" />
4. <!-- DefaultCache setting. -->
5. <defaultCache
6. maxElementsInMemory="1000"
7. eternal="false"
8. timeToIdleSeconds="120"
9. timeToLiveSeconds="120"
10. maxElementsOnDisk="1000000"
11. overflowToDisk="true"
12. memoryStoreEvictionPolicy="LRU">
13.
14. </defaultCache>
15.
16.
17. </ehcache>

spring配置文件:

 

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.3.xsd ​​
6. ​​ http://www.springframework.org/schema/context ​​
7. ​​ http://www.springframework.org/schema/context/spring-context-4.3.xsd"​​>
8. <!-- 加载配置文件 -->
9.
10. <context:property-placeholder location="classpath:config.properties"/>
11. <!-- 扫描service自动注入为bean -->
12. <context:component-scan base-package="yfkj.gz.task.service.impl,yfkj.gz.task.dao.impl" />
13.
14. </beans>

spring-hibernate.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:tx="http://www.springframework.org/schema/tx"
4. xmlns:aop="http://www.springframework.org/schema/aop"
5. xsi:schemaLocation="
6. ​​ http://www.springframework.org/schema/beans ​​
7. ​​ http://www.springframework.org/schema/beans/spring-beans-4.3.xsd ​​
8. ​​ http://www.springframework.org/schema/tx ​​
9. ​​ http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ​​
10. ​​ http://www.springframework.org/schema/aop ​​
11. ​​ http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"​​>
12.
13. <!-- 配置数据源 Druid -->
14. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
15. init-method="init" destroy-method="close">
16. <!-- MySQL数据库配置 -->
17. <property name="url"
18. value="${url.jdbc}" />
19. <property name="username" value="${url.name}" />
20. <property name="password" value="${url.pass}" />
21. <!-- SQL Server数据库配置 -->
22. <!-- <property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=yfkj"
23. /> <property name="username" value="sa" /> <property name="password" value="123456"
24. /> -->
25. <!-- Oracle数据库配置 -->
26. <!-- <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:yfkj"
27. /> <property name="username" value="SYSTEM" /> <property name="password"
28. value="SkyNet888999" /> -->
29. <!-- 配置初始化大小、最小、最大 -->
30. <property name="initialSize" value="1" />
31. <property name="minIdle" value="1" />
32. <property name="maxActive" value="20" />
33. <!-- 配置获取连接等待超时的时间 -->
34. <property name="maxWait" value="60000" />
35. <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
36. <property name="timeBetweenEvictionRunsMillis" value="60000" />
37. <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
38. <property name="minEvictableIdleTimeMillis" value="300000" />
39. <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
40. <property name="poolPreparedStatements" value="true" />
41. <property name="maxPoolPreparedStatementPerConnectionSize"
42. value="20" />
43. <!-- 配置监控统计拦截的filters -->
44. <property name="filters" value="stat,log4j,wall" />
45. </bean>
46.
47. <!-- 配置hibernate的SessionFactory -->
48. <bean id="sessionFactory"
49. class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
50. <!-- 注入数据源 相关信息看源码 -->
51. <property name="dataSource" ref="dataSource" />
52. <!-- hibernate配置信息 -->
53. <property name="hibernateProperties">
54. <props>
55. <prop key="hibernate.dialect">${hibernate.dialect}</prop>
56. <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
57. <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
58. <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
59.
60. <!-- 开启二级缓存 ehcache -->
61. <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
62. <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
63. <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
64. <prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.cache.provider_configuration_file_resource_path}
65. </prop>
66. </props>
67. </property>
68. <!-- 扫描hibernate注解配置的entity -->
69. <property name="packagesToScan" value="yfkj.gz.task.entity" />
70. </bean>
71.
72. <!-- 配置事务管理器 -->
73. <bean id="transactionManager"
74. class="org.springframework.orm.hibernate5.HibernateTransactionManager">
75. <property name="sessionFactory" ref="sessionFactory" />
76. </bean>
77.
78. <!-- 配置事务增强处理Bean,指定事务管理器 -->
79. <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
80. <!-- 配置详细事务处理语义 -->
81. <tx:attributes>
82. <tx:method name="insert*" propagation="REQUIRED" />
83. <tx:method name="update*" propagation="REQUIRED" />
84. <tx:method name="delete*" propagation="REQUIRED" />
85.
86. <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
87. <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
88. <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
89. <tx:method name="load*" propagation="SUPPORTS" read-only="true" />
90.
91. <!-- 其他采用默认事务方式 -->
92. <tx:method name="*" />
93.
94. </tx:attributes>
95. </tx:advice>
96.
97. <!-- Spring aop事务管理 -->
98. <aop:config>
99. <!-- 配置切入点 -->
100. <aop:pointcut id="transactionPointcut"
101. expression="execution(* yfkj.gz.task.service..*impl.*(..))" />
102. <!-- 指定在txAdvice切入点应用txAdvice事务增强处理 -->
103. <aop:advisor pointcut-ref="transactionPointcut"
104. advice-ref="transactionAdvice" />
105. </aop:config>
106.
107.
108. <!-- 使用annotation定义事务:如果没有用到注解事物~可以关闭 -->
109. <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
110.
111. </beans>

spring-mvc.xml配置文件:

 

1. <?xml version="1.0" encoding="UTF-8"?>  
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
5. xsi:schemaLocation="http://www.springframework.org/schema/beans
6. ​​ http://www.springframework.org/schema/beans/spring-beans-4.3.xsd ​​
7. ​​ http://www.springframework.org/schema/context ​​
8. ​​ http://www.springframework.org/schema/context/spring-context-4.3.xsd ​​
9. ​​ http://www.springframework.org/schema/mvc ​​
10. ​​ http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"​​>
11.
12. <!-- 自动扫描@Controller注入为bean -->
13. <context:component-scan base-package="yfkj.gz.task.controller" />
14.
15. <!-- 以下为SpringMVC配置 -->
16. <mvc:annotation-driven>
17. <!-- 返回json数据,@response使用 -->
18. <mvc:message-converters register-defaults="true">
19. <bean
20. class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
21. <property name="supportedMediaTypes">
22. <list>
23. <value>text/html;charset=UTF-8</value>
24. <value>application/json;charset=UTF-8</value>
25. </list>
26. </property>
27.
28. </bean>
29. </mvc:message-converters>
30. </mvc:annotation-driven>
31.
32. <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
33. <bean
34. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
35. <property name="viewClass"
36. value="org.springframework.web.servlet.view.JstlView" />
37. <property name="prefix" value="/WEB-INF/pages/" />
38. <property name="suffix" value=".jsp" />
39. </bean>
40.
41. </beans>

最后是我们的web.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 加载配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,classpath:spring-hibernate.xml</param-value>
</context-param>
<!-- 中文过滤器 -->
<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>

<!-- openSessionInView配置 作用是延迟session关闭到view层 -->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<!-- 监听servletContext,启动contextConfigLocation中的spring配置信息 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 防止spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<servlet>
<description>spring mvc servlet</description>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 此处配置的是SpringMVC的配置文件 -->
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<!-- druid数据源拦截配置,如果不需要druid提供的数据库日志,就可以直接注释下面的代码 -->
<filter>
<filter-name>DruidWebStatFilter</filter-name>
<filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
<init-param>
<param-name>exclusions</param-name>
<param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value>
</init-param>
<init-param>
<param-name>profileEnable</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>sessionStatEnable</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>principalSessionName</param-name>
<param-value>SESSION_SYS_USER</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>DruidWebStatFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>DruidStatView</servlet-name>
<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
<init-param>
<param-name>resetEnable</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>loginUsername</param-name>
<param-value>gzyfkjDruid</param-value>
</init-param>
<init-param>
<param-name>loginPassword</param-name>
<param-value>gzyfkj@Druid2016$$$</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DruidStatView</servlet-name>
<url-pattern>/druid/*</url-pattern>
</servlet-mapping>

<!-- 配置session超时时间,单位分钟 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>

<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>

</web-app>

标签:www,Hibernate,http,SpringMvc,Spring,springframework,hibernate,spring,org
From: https://blog.51cto.com/u_13363083/5979271

相关文章

  • 简单hibernate5.2.10入门配置
    1、官网下载hibernate5相关jar包​​http://hibernate.org/orm/​​2、解压后,在lib目录中找到required目录下的jar包添加至项目。再添加数据库驱动包,博主用MySQLjar包添加......
  • Hibernate环境部署与注解简单使用
    Hibernate是对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,对对象进行持久化。它可以自动生成SQL语句,自动执行。简单的说就是帮你把XXXDAO的繁琐工作都给自动完成了,要......
  • SpringDataRedis:第一章:简介
    SpringDataRedis简介项目常见问题思考我们目前的系统已经实现了广告后台管理和广告前台展示,但是对于首页每天有大量的人访问,对数据库造成很大的访问压力,甚至是瘫痪。那如......
  • SpringBoot(九):整合Mybatis
    一、导入依赖<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId>......
  • 0016 常见错误com.example.adminspringboot.mapper.UserMapper.update
    错误截图  在使用mybatis中容易出现解决方法:在配置文件中加上路径书写:mybatis:mapper-locations:classpath:mapper/*.xml#扫描所有mapper下的xml文件 ......
  • springboot项目打包后上传服务器失败,但是在本地可以运行。
    近期开发一个项目,在本地运行无问题,发到服务器上无法访问,最后排查发现,springboot使用的是内嵌的tomcat,而项目要使用外部的tomcat。1.在pom文件里面去掉打包不参与的。<!--......
  • Spring Security开发实践
    SpringSecurity是Spring家族中用于提供认证、授权和攻击防护功能的一套安全框架。它也是Spring应用在安全框架方面的公认标准。SpringSecurity安全框架适合为Sp......
  • springboot简单了解
    先创建一下springboot工程,可以在idea里面创建,也可以在spring.io的官网里面创建,因为idea很卡,所以我一般用官网里面创建。  勾选起步依赖: 然后在程序中,对于控制器......
  • 模拟spring-kafka实现kafka的consumer监听
    背景:因为某些原因,无法直接使用springboot提供的@KafkaListener,改为模拟springboot注解的方式搬过来实现首先创建一个业务处理的service,这个service主要用于消费下来的消息......
  • [Spring] Spring 中bean的生命周期
    在平时的工作中,我们的很多项目都是利用Spring进行搭建的。最近有空,基于源码好好分析一下,Bean在Spring中的生命周期这里我们先写一个简单的小例子<?xmlversion="1.0"e......