1,mybati-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="cacheEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="true"/>
</settings>
<!-- 类型别名-->
<typeAliases>
<package name="org.springmybatis.entity"/>
</typeAliases>
<mappers>
<!-- 注意:package需要放置在mapper之后
(元素类型为 "mappers" 的内容必须匹配 "(mapper*,package*)"。)
绑定接口所在的包,注册其中的所有接口,如果有接口映射的XML配置文件,里面却没有配置内容的话就会报错,
lineNumber 1,columnNumber 1 出错,因为package会扫描dao所有的映射文件,但是没扫描到-->
<package name="org.springmybatis.dao"/>
</mappers>
</configuration>
2,spring-context.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:p="http://www.springframework.org/schema/p"
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">
<!-- 1,引入jdbc配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2,加载jdbc数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 3,创建SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--设置dataSource属性来源于jdbc数据源-->
<property name="dataSource" ref="dataSource"/>
<!--加载mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 4,扫描并创建DAO中的mapper对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描mapper所在的包-->
<property name="basePackage" value="org.springmybatis.dao"/>
<!--引用会话工厂创建会话进而创建mapper-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- 5,通过DI方式给需要的类创建对象-->
<bean id="userService" class="org.springmybatis.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
<!--由于servlet在web项目中是有由Tomcat创建的,所以应在servlet的初始化方法中就给其属性(userService)赋值,否则就会NPE-->
<!-- <bean id="userServlet" class="org.springmybatis.servlet.UserServlet">-->
<!-- <!–同上–>-->
<!-- <property name="userService" ref="userService"/>-->
<!-- </bean>-->
</beans>
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">
<!-- 6,引入Spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context.xml</param-value>
</context-param>
<!-- 7,利用监听器帮助加载Spring配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>