1 spring 文件夾下 db.propertieshtml
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/zy_coupon jdbc.user=root jdbc.password=111
2 mybatis 文件夾下 mybatis-config.xmljava
<?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="mapUnderscoreToCamelCase" value="true" /> </settings> </configuration>
3 spring 文件夾下spring-dao.xmlmysql
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-- 掃描並識別Spring 相關的註解: @service @Componnent @Repository...--> <context:component-scan base-package="com.zhiyou100" /> <!-- 加載 db.properties--> <context:property-placeholder location="classpath:spring/db.properties"/> <!-- c3p0 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.user}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 配置sqlSessionFactory的相關配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入數據源 --> <property name="dataSource" ref="dataSource"></property> <!-- 設置Mybatis配置文件的路徑 --> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property> <!-- 開啓起別名,默認爲首字母小寫 --> <property name="typeAliasesPackage" value="com.zhiyou100.model"></property> <!-- 設置mapper 文件路徑 --> <property name="mapperLocations" value="classpath:com/zhiyou100/dao/*.xml"></property> </bean> <!-- 把dao 接口的實現類注入到Spring容器中,用過名字或者類型獲取對象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 把dao包下的Interface 的實現類注入到spring容器中 --> <property name="basePackage" value="com.zhiyou100.dao"></property> </bean> </beans>
4 spring 文件夾下 spring-service.xmlweb
<?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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!-- 針對Service層的Spring配置 --> <!-- 1.包掃描 @service --> <context:component-scan base-package="com.zhiyou100.service"></context:component-scan> <!-- 2. 爲方法提供事務支持 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入數據庫鏈接池,事務是數據庫中的 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 使用最簡單的聲明式事務操做,經過註解來完成 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
5 spring 文件夾下 spring-mvc.xmlspring
<?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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 包掃描 --> <context:component-scan base-package="com.zhiyou100.controller" /> <!-- 開啓註解 --> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 返回的jsp文件的路徑前綴 --> <!-- 放在WEB-INF下的.jsp別人不能經過url訪問,能夠提升數據的安全性 --> <property name="prefix" value="/WEB-INF/view/"></property> <!-- 返回的jsp文件的路徑後綴 --> <property name="suffix" value=".jsp"></property> </bean> <!-- 使用默認的handler ,支持靜態的訪問 --> <mvc:default-servlet-handler/> </beans>
6 webapp 下的 WEB-INF下的web.xmlsql
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>SpringTest5</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 對Spring的配置 --> <!-- 在服務器啓動的時候去加載和解析Spring配置文件 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定Spring的文件路徑 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </context-param> <!-- 使用Spring 的字符編碼轉換式過濾器比使用Tomcat的更好 --> <!-- 讓咱們的程序具備可移植性,在全部的服務器下均可以編碼轉換 --> <!-- 使用和tomcat 的過濾器是同樣的 --> <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> <!-- 對SpringMVC的配置 --> <!-- SpringMVC 中全部請求都提交給一個Servlet 進行處理 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定SpringMVC 配置文件的位置,默認去找:和servlet-name 同名的配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> </servlet> <!-- 配置servlet的url 的請求 --> <!-- / 和/* 對應了兩種不一樣的狀況 --> <!-- / 匹配了全部的url, 除了.jsp 文件 --> <!-- /* 匹配了全部的url --> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>