Spring整合mybatis 主要是将mybatis的配置文件取消,在spring中的配置文件中配置
需要将spring以及mybatis的先关jar包导入 以及整合spring和mybatis的jar包
mybatis之前需要配置的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="logImpl" value="LOG4J"/>
</settings>
<typeAliases> // 起别名 为后面的mapper.xml书写类型方便
<package name="com.yyl.pojo"/>
</typeAliases>
<environments default="default"> //配置相关连接数据库的信息
<environment id="default">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db_study"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers> //配置相关mapper.xml文件
<mapper resource="com/yyl/Mapper/StudentMapper.xml"/>
<mapper resource="com/yyl/Mapper/TeacherMapper.xml"/>
</mappers>
</configuration>
在spring中需要编写 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" >
<bean id="datasource" //配置数据库连接信息 class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/db_study"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
//配置SqlSession相关信息
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"></property>
</bean>
//配置mapper相关信息
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yyl.mapper"></property>
<property name="sqlSessionFactory" ref="factory"></property>
</bean>
</beans>
在web.xml中配置一下信息
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
以上是spring 整合 mybatis所需要配置的相关文件信息