一、环境 mac + Idea + 数据库 mysql
二、项目整合过程
1⃣️. 创建动态Web项目,请参考:https://www.cnblogs.com/KennyWang0314/p/12261890.html
2⃣️.引入 jar 包 === ***注意:c3p0包依赖 mchange-commons 记得导入

3⃣️.文件配置
- web.xml
<?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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-base.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
- 在WEB-INF 下配置 spring-servlet.xml
*** 注意 :通常为了让 MVC 独立管理,会配置 context:component-scan 属性为 use-default-filters="false"并配合 include-filter使用,但是当applicationContext.xml 该属性设置为 true并配合exclude-使用
具体情况会在之后说明
<?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-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<!-- 该配置只扫描控制器 -->
<context:component-scan base-package="com.kenny.mybatis" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven></mvc:annotation-driven><!-- 正确处理动态资源 -->
<mvc:default-servlet-handler></mvc:default-servlet-handler ><!-- 正确处理静态资源 -->
</beans>
- 配置 Mapp
***注意:使用Idea做开发,如果需要将mapp.xml做单独文件管理,需要放到src文件夹下,因此需要在 Idea 下创建一个文件夹,并且通过 Make Directory As 设置为 Sources Root
- Mybatis 配置
<?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设置项的名字,value设置项的取值
mapUnderscroeToCamelCase 数据中字段默认按照驼峰规则与属性做映射
-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 显示的指定需要设置的属性值,不要因为默认而不设置 -->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<!--cacheEnable 是否开启二级缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
</configuration>
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:tx="http://www.springframework.org/schema/tx"
xmlns:mytabis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<!-- 希望管理所有的逻辑 -->
<context:component-scan base-package="com.kenny.mybatis">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- -->
<context:property-placeholder location="classpath:dbconnection.properties"></context:property-placeholder>
<!-- Spring用来控制业务逻辑。数据源,事物控制,aop -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<!-- -->
<bean name="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"></tx:annotation-driven>
<!-- 整合Mybatis
1。Spring管理所有组建
Service ===》Dao @AutoWire:自动注入
2。用Spring来管理事物
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 配置 mybatis 的配置文件位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 指定mapper xml 文件的位置 -->
<property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml" ></property>
</bean>
<!-- 扫描所有DAO接口 -->
<mytabis-spring:scan base-package="com.kenny.mybatis.dao"></mytabis-spring:scan>
</beans>
### 创建完成 ###
来源:https://www.cnblogs.com/KennyWang0314/p/12261885.html