1. 创建/src/main/resources/jdbc.properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/o2o?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456

2. 创建/src/main/resources/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>
<!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值️-->
<setting name="useGeneratedKeys" value="true" />
<!-- 使用列别名替换列名-->
<setting name="useColumnLabel" value="true" />
<!-- 开启驼峰命名替换:Table(create_time)->Entity{createTime}-->
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
</configuration>

3. 创建/src/main/resources/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">
<!--配置整合mybatis过程-->
<!--1.配置数据库相关参数properties的属性:${url}-->
<context:property-placeholder location="classpath:jdbc.properties" />
<!--2.数据库连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<!--配置连接池属性-->
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!--c3p0连接池的私有属性-->
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="10" />
<!--关闭连接后不自动commit-->
<property name="autoCommitOnClose" value="false" />
<!--获取连接超时时间-->
<property name="checkoutTimeout" value="10000" />
<!--当获取连接失败后,重试次数-->
<property name="acquireRetryAttempts" value="2" />
</bean>
<!--3.配置SqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource" />
<!--配置MyBatis全局配置文件:mybatis-config.xml-->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!--扫描entity包,使用别名-->
<property name="typeAliasesPackage" value="com.csj2018.o2o.entity" />
<!--扫描sql配置文件:mapper需要的xml文件-->
<property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>
<!--4.配置扫描Dao接口包,动态实现Dao接口,注入到Spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!--给出需要扫描Dao接口包-->
<property name="basePackage" value="com.csj2018.o2o.dao" />
</bean>
</beans>

4. 创建/src/main/resources/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包下所有使用注解的类型 -->
<context:component-scan base-package="com.csj2018.o2o.service" /> <!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
</bean> <!-- 配置基于注解的声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

6. 创建/src/main/resources/spring/spring-web.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-3.2.xsd">
<!-- 配置SpringMVC -->
<!-- 1.开启SpringMVC注解模式 自动识别controller类,不需要xml中做bean的配置-->
<mvc:annotation-driven />
<!-- 2.静态资源默认servlet配置
(1)加入对静态资源的处理:js,gif,png
(2)允许使用"/"做整体映射 -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:default-servlet-handler />
<!-- 3.定义视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/WEB_INF/html/"></property>
<property name="suffix" value=".html"></property>
</bean>
<!-- 4.扫描web相关的bean -->
<context:component-scan base-package="com.csj2018.o2o.web" />
</beans>

7. 修改/src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8" ?>
<web-app version="3.1"
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_3_1.xsd"
metadata-complete="true">
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<!-- 默认匹配所有的请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

8. 总结

*    1.首先在pom.xml中指定工程所依赖的jar包
* 2.在jdbc.properties中指定了数据库的连接方式
* 3.创建mybatis-config文件对mybatis做配置
* 4.创建spring-dao文件,将jdbc.properties和mybatiso-config都加载了进来,创建dataSource连接池,同时配置了mybatis与数据库进行交互的方式
* 5.创建spring-service配置文件,用于事务管理将spring-dao配置到的dataSource注入到事务管理器里面,便于service层做操纵
* 6.创建spring-web文件,定义dispatcher如何响应我们的url请求,即定义了controller的一些行为
* 7.在web.xml中dispatcher作为servlet注册进来响应前端请求,同时将spring打头的文件都注册进来

最新文章

  1. .NET ORM 的 “SOD蜜”--零基础入门篇
  2. sql server中的 SET NOCOUNT ON 的含义
  3. POJ 3140 Contestants Division
  4. ObjectiveC 文件操作一
  5. pytesser图片文本识别
  6. Map集合。
  7. RobotFramework自动化测试框架-Selenium Web自动化(-)-Open Browser和Close Browser
  8. [PA2012] Tax
  9. Linux关机&amp;重启命令
  10. 20165205 2017-2018-2 《Java程序设计》实验二 Java面向对象程序设计
  11. 批量插入,批量修改的sql
  12. 一次典型的TFS故障处理:域控失联
  13. WEB服务器、应用程序服务器、HTTP服务器的区别
  14. LINQ学习:Select的用法
  15. 声卡由于其配置信息(注册表中的)不完整或已损坏,Windows 无法启动这个硬件设备。(代码 19),
  16. Java中的三元运算:a = (a &gt; b)?a:b
  17. leetcode 19. 删除链表的倒数第N个节点 JAVA
  18. .Net程序员玩转Android系列之二~Android Framework概要(1)
  19. appium 元素定位与操作:
  20. qtp录制时间控件不允许用户手动输入的解决办法

热门文章

  1. 在vue中使用高德地图开发,以及AMap的引入?
  2. JS切换
  3. Python学习笔记(九)——字符串
  4. python_django__验证码
  5. msql数据库基础
  6. mui请求数据接口问题
  7. Spring Data之Example&lt;&gt;
  8. thinkphp自动创建目录
  9. Go 程序开发的注意事项
  10. 堡垒机介绍及实现 (使用python django实现)(一)