Spring5+SpringMvc+Hibernate5整合的實(shí)現(xiàn)
在進(jìn)行環(huán)境搭建的時(shí)候我發(fā)現(xiàn)國內(nèi)的Spring+SpringMvc+Hibernate整合資料比較少,即使有的話要么就是將所有配置放在一個(gè)配置文件,不易于理解結(jié)構(gòu),要么就是版本太舊,因此這篇文章簡單講解了如何配置相關(guān)的開發(fā)環(huán)境,使用的版本為jdk1.8+spring5+hibernate5
1 分層整合
我們都知道在Spring可以通過<import>
標(biāo)簽來將不同的配置文件進(jìn)行整合的,因此我們就用這個(gè)思路來進(jìn)行整合,我們將全部的配置文件分為dao層,service層和view層,這樣整合起來比較方便,也比較容易懂
2 創(chuàng)建項(xiàng)目引入依賴
我們首先創(chuàng)建一個(gè)名字為Spring5+SpringMvc+Hibernate5的項(xiàng)目,從原型中找到如下的選項(xiàng)
idea給我們創(chuàng)建的項(xiàng)目默認(rèn)是不完整的
少了一些文件,我們只需要在src上右鍵,選擇創(chuàng)建對應(yīng)的文件夾即可
有些低版本的idea可能沒有這么只能,你只能手動(dòng)創(chuàng)建并設(shè)置資源和源碼目錄
創(chuàng)建好了之后我們就可以修改一些pom文件中的配置了,以下的文件可以根據(jù)自己的情況進(jìn)行修改
2.1 引入依賴
spring依賴
<!--spring依賴--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>5.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.0.RELEASE</version> <scope>provided</scope> </dependency>
hibernate依賴
<!-- hibernate核心依賴--> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.17.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.2.0.Final</version> </dependency>
數(shù)據(jù)庫和連接池依賴
<!--連接池依賴--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!-- 數(shù)據(jù)庫依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.12</version> </dependency>
引入jsp
<!-- jsp依賴 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
jsp其實(shí)不是必須,但是你如果需要用到進(jìn)行不分離的開發(fā),那么該依賴最好還是引入一下
其他的依賴可以根據(jù)自己的情況引入
2.2 構(gòu)建配置修改
在build的時(shí)候有可能會(huì)出現(xiàn)靜態(tài)資源沒有build的情況,因此可以采用以下方法
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> <filtering>true</filtering> </resource> </resources> ....... </build>
3 準(zhǔn)備數(shù)據(jù)庫
我們在這里準(zhǔn)備一個(gè)名字為hibernate的數(shù)據(jù)庫,請自行創(chuàng)建,至于數(shù)據(jù)庫表我們則不需要?jiǎng)?chuàng)建
4 配置dao層
我們首先在resources下準(zhǔn)備一個(gè)db-mysql.properties文件,內(nèi)容如下
jdbc.driverClassName=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/hibernate?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf-8 jdbc.username=root jdbc.password=123456
請根據(jù)自己的數(shù)據(jù)庫版本和配置進(jìn)行書寫,當(dāng)然這一步不是必須的,但是為了方便找到對應(yīng)配置項(xiàng),你可準(zhǔn)備這個(gè)文件
好了我們可以開始進(jìn)行spring的配置了,在resources文件夾下創(chuàng)建一個(gè)spring-dao,xml
,這里的名字可以自行決定,在里面輸入以下內(nèi)容
<?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:context="http://www.springframework.org/schema/context" 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/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"> <!-- 開啟注解掃描--> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.fzu.pojo"></context:component-scan> <!-- 加載配置文件--> <context:property-placeholder location="classpath:db-mysql.properties"></context:property-placeholder> <!-- 配置數(shù)據(jù)源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" lazy-init="false"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="initialSize" value="1" /> <property name="maxActive" value="50" /> <property name="maxWait" value="30000" /> <property name="filters" value="stat,wall" /> <property name="timeBetweenEvictionRunsMillis" value="3000" /> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x'" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> </bean> <!--配置sessionFactory--> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="packagesToScan" value="com.fzu.pojo"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL55Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> <!-- 配置事務(wù)管理--> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 開啟注解事務(wù)管理 --> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> <!--配置hibernatetemplate--> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
請根據(jù)自己的情況創(chuàng)建對應(yīng)的包和選擇相應(yīng)的屬性例如數(shù)據(jù)庫方言,我這里是選擇了com.fzu.pojo下存放實(shí)體類,我創(chuàng)建了一個(gè)Student實(shí)體類,其中關(guān)于hibernate注解的內(nèi)容我們不贅述
package com.fzu.pojo; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table public class Student { @Id private Long studentid; private String name; private Integer age; // 這里請不要用desc,這個(gè)是mysql保留字 private String description; @Override public String toString() { return "Student{" + "studentid=" + studentid + ", name='" + name + '\'' + ", age=" + age + ", description='" + description + '\'' + '}'; } public Long getStudentid() { return studentid; } public void setStudentid(Long studentid) { this.studentid = studentid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
最后在applicationContext.xml中引入該文件
<?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:context="http://www.springframework.org/schema/context" 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/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"> <!-- 開啟注解掃描--> <context:component-scan base-package="com.fzu"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <context:annotation-config></context:annotation-config> <import resource="classpath:spring-dao.xml"/> <import resource="classpath:spring-mvc.xml"/> </beans>
由于這里我們service層的內(nèi)容比較少我們就不單獨(dú)創(chuàng)建一個(gè)配置文件了
5 配置mvc層
創(chuàng)建spring-mvc.xml
文件,并輸入以下內(nèi)容
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/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 http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--注解驅(qū)動(dòng)--> <mvc:annotation-driven></mvc:annotation-driven> <!--過濾靜態(tài)資源 --> <mvc:default-servlet-handler></mvc:default-servlet-handler> <!--掃描controller--> <context:component-scan base-package="com.fzu.controller"></context:component-scan> <!--視圖解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
其中視圖解析器路徑和掃描的包可以自行確定,不要忘記在applicationContext.xml
中引入
6 配置web.xml
我們需要在web.xml中配置spring容器,視圖servlet和編碼servlet
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> <!-- 配置請求分發(fā)器 --> <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:spring-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> <!-- 配置字符過濾器 --> <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> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 監(jiān)聽spring容器自動(dòng)啟動(dòng) --> <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> <!-- 配置session過期時(shí)間--> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app>
在創(chuàng)建完相應(yīng)的類之后,請不要忘記配置tomcat服務(wù)器,再啟動(dòng)之后我們就可以看到相應(yīng)的表創(chuàng)建了
7 總結(jié)
我們首先看一下最后的文件結(jié)構(gòu)
可以看到我們的整個(gè)配置過程是模塊化的,從dao層往上,每一層都可以單獨(dú)拆裝,除此之外我們項(xiàng)進(jìn)行其他配置也是很簡單的,例如如果我們想要新增一個(gè)redis配置
我們可以按照如下步驟
- 引入相關(guān)依賴
- 創(chuàng)建db-redis.properties配置文件
- 創(chuàng)建spring-redis.xml配置文件并進(jìn)行配置
- 在applicationContext中引入即可
可以看到整個(gè)過程邏輯很清晰
到此這篇關(guān)于Spring5+SpringMvc+Hibernate5整合的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Spring5+SpringMvc+Hibernate5整合內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java編程實(shí)現(xiàn)向文本文件中讀取數(shù)據(jù)之Scanner用法示例
這篇文章主要介紹了Java編程實(shí)現(xiàn)向文本文件中讀取數(shù)據(jù)之Scanner用法,結(jié)合實(shí)例形式分析了java使用Scanner類讀取文本文件相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-03-03Java8 HashMap擴(kuò)容算法實(shí)例解析
這篇文章主要介紹了Java8 HashMap擴(kuò)容算法實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Java中常用的設(shè)計(jì)模式之責(zé)任鏈模式詳解
這篇文章主要為大家詳細(xì)介紹了Java中常用的設(shè)計(jì)模式之責(zé)任鏈模式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02Java 定時(shí)器(Timer)及線程池里使用定時(shí)器實(shí)例代碼
這篇文章主要介紹了Java 定時(shí)器(Timer)及線程池里使用定時(shí)器實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-12-12SpringBoot基于Sentinel在服務(wù)上實(shí)現(xiàn)接口限流
這篇文章主要介紹了SpringBoot基于Sentinel在服務(wù)上實(shí)現(xiàn)接口限流,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Java中Timer的schedule()方法參數(shù)詳解
今天小編就為大家分享一篇關(guān)于Java中Timer的schedule()方法參數(shù)詳解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03使用Java實(shí)現(xiàn)百萬Excel數(shù)據(jù)導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)百萬Excel數(shù)據(jù)導(dǎo)出,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考一下2024-03-03