1,spring+mybatis配置文件
<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描各层包-->
<context:component-scan base-package="com.example.service,com.example.controller"/>
<!-- 指定属性配置文件位置-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--配置数据源-->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<property name="jdbcUrl" value="${url}"/>
<property name="driverClass" value="${driver}"/>
<property name="user" value="${uname}"/>
<property name="password" value="${pwd}"/>
</bean>
<!--解决静态资源加载问题-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--开启MVC注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置Mybatis的SQL会话工厂-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="factoryBean">
<!-- 指定数据源-->
<property name="dataSource" ref="dataSource"/>
<!-- 指定映射配置文件路径-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!-- 指定类型别名包-->
<property name="typeAliasesPackage" value="cn.wh.entity"/>
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<!-- 开启下划线转驼峰命名-->
<property name="mapUnderscoreToCamelCase" value="true"/>
</bean>
</property>
<property name="plugins">
<array>
<!-- 指定分页插件-->
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<!-- 指定SQL方言-->
<prop key="helperDialect">mysql</prop>
<!-- 开启分页合理化-->
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
<!--配置映射扫描配置对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.dao"/>
<property name="sqlSessionFactoryBeanName" value="factoryBean"/>
</bean>
</beans>
2,web.xml配置
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:webconfig.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3,需要的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.25</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.25</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
4,项目结构
![](https://i-blog.csdnimg.cn/blog_migrate/40cc9bea012683b3e885947593a87493.png)