Spring+SpringMVC+MyBatis+easyUI整合基礎篇(三)搭建步驟
阿新 • • 發佈:2022-05-04
作者:13 GitHub:https://github.com/ZHENFENG13 版權宣告:本文為原創文章,未經允許不得轉載。
框架介紹
- Spring
- SpringMVC
- MyBatis
- easyUI
大家應該也都有所瞭解,概念性的東西就不寫了,有萬能的百度。
開發環境
- JDK 1.7
- Mysql 5.6
- IntelliJ IDEA 16
- tomcat 7
以上為本地的構建環境,當然,如果開發環境版本或者IDE與以上不同也是可以正常搭建環境的,比如你JDK是1.8啊,IDE用的是eclipse或者Myeclipse都是完全OK的。
專案整合
下圖為專案的目錄結構截圖:
- entity包中即為本專案的所有實體類
- dao包中為資料層介面
- mappers中為資料層介面的sql對映檔案,執行的sql語句是在這些檔案中定義的
- admin包中則是控制層程式碼
- WebRoot中則是jsp檔案及jar包等檔案。
以上包名都可以按照個人習慣來命名。
第一步,新建一個JavaWeb專案,接下來在src目錄下entity包、mappers包、dao包和service包,並新增配置Spring的檔案applicationContext.xml和mybatis的配置檔案mybatis-config.xml。
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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 自動掃描dao層和service層,將包中的類納入到Spring的管理中 --> <context:component-scan base-package="com.core.dao"/> <context:component-scan base-package="com.core.service"/> <!-- 配置資料來源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name=“url" value="jdbc:mysql://xx.xx.xx.xx:xx/db"/> <!-- 改為你的地址即可 --> <property name="username" value="xxxx"/> <property name="password" value="xxxx"/> </bean> <!-- 配置mybatis的sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 自動掃描mappers包下的所有xml檔案 --> <property name="mapperLocations" value="classpath:com/core/mappers/*.xml"></property> <!-- mybatis配置檔案 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- dao介面所在包名,Spring會自動查詢其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.core.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事務通知屬性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 定義事務傳播屬性 --> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="upd*" propagation="REQUIRED"/> <tx:method name="edit*" propagation="REQUIRED"/> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="new*" propagation="REQUIRED"/> <tx:method name="set*" propagation="REQUIRED"/> <tx:method name="remove*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="change*" propagation="REQUIRED"/> <tx:method name="check*" propagation="REQUIRED"/> <tx:method name="get*" propagation="REQUIRED" read-only="true"/> <tx:method name="search*" propagation="REQUIRED" read-only="true"/> <tx:method name="find*" propagation="REQUIRED" read-only="true"/> <tx:method name="load*" propagation="REQUIRED" read-only="true"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice> <!-- 配置事務切面,service包中的類會被代理 --> <aop:config> <aop:pointcut id="serviceOperation" expression="(execution(* com.core.service.*.*(..)))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/> </aop:config> </beans>
mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="logImpl" value="LOG4J"/> </settings> <typeAliases> <package name="com.core.entity"/> </typeAliases> </configuration>
spring-mvc.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 掃描admin包,將控制器納入Spring的管理中 -->
<context:component-scan base-package="com.core.admin"/>
<!-- 檢視解析器,檢視模式下的字首和字尾 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value=“/“/>
<property name="suffix" value=".jsp"></property>
</bean>
</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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>ssm-demo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring請求配置,指向springmvc的核心配置檔案,定義所有以.do結尾的請求都被springmvc攔截 -->
<servlet>
<servlet-name>springMVC</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>
<!--載入順序為1 -->
<load-on-startup>1</load-on-startup>
</servlet>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!— 錯誤頁面配置,這裡只是簡單的配置了一下 -->
<error-page>
<error-code>404</error-code>
<location>/main.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/main.jsp</location>
</error-page>
</web-app>
結語
整合過程中需要注意的點:
- 專案編碼設定正確
- jar包要齊全
- mysql要能正常登入
當然,也可以直接匯入原始碼, 點選這裡下載程式碼。
Github地址在這裡:https://github.com/ZHENFENG13
需要幫助的話,可以留言。
網站的持續執行需要各項基礎設施的搭建,而服務期的續費和維護及各種配套服務的購買也需要一定的費用,希望朋友們給予一點支援,謝謝!