1 spring 文件夹下 db.properties
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/zy_coupon jdbc.user=root jdbc.password=111
2 mybatis 文件夹下 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="mapUnderscoreToCamelCase" value="true" /> </settings> </configuration>
3 spring 文件夹下spring-dao.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.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.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: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.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: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.xml
<?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>