<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 加載配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 數據源,使用dbcp --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="5" /> </bean> <!-- sqlSessinFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 加載mybatis的配置文件 --> <property name="configLocation" value="mybatis/SqlMapConfig.xml" /> <!-- 數據源 --> <property name="dataSource" ref="dataSource" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定掃描的包名 若是掃描多個包,每一個包中間使用半角逗號分隔 --> <property name="basePackage" value="cn.com.sm.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
<?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--> <settings> <!-- 打開延遲加載的開關 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 將積極加載改成消極加載(及按需加載) --> <setting name="aggressiveLazyLoading" value="false"/> <!-- 打開全局緩存開關(二級緩存)默認值就是true --> <setting name="cacheEnabled" value="true"/> <!--執行MyBatis 所用日誌的具體實現,未指定時將指定查找--> <setting name="logImpl" value="LOG4J" /> </settings> <!-- 別名定義 --> <typeAliases> <package name="cn.com.sm.po"/> </typeAliases> <!-- 加載映射文件 --> <mappers> <!-- 經過resource方法一次加載一個映射文件 --> <!-- <mapper resource="sqlmap/UserMapper.xml"/>--> <!-- 批量加載mapper <package name="cn.com.sm.mapper"/>--> </mappers> </configuration>
日誌配置文件"log4j.properties":java
log4j.rootLogger = debug, stdout log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %5p [%t] - %m%n
數據庫資源文件"db.properties":mysql
jdbc.driver=org.gjt.mm.mysql.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis_test?characterEncoding=utf-8 jdbc.username=root jdbc.password=123456
在pom.xml中添加:spring
<build> <!-- 若是不添加此節點mybatis的mapper.xml文件都會被漏掉。 --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
在Spring全局配置文件applicationContext.xml中添加Spring的Mapper掃描器,用於掃描Mapper代理接口:sql
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定掃描的包名 若是掃描多個包,每一個包中間使用半角逗號分隔 --> <property name="basePackage" value="cn.com.sm.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean>
因爲使用了spring的掃描類,因此Mapper配置文件要和Mapper代理接口位於一個包下,且名稱同樣。自動掃描出來的Mapper接口的bean的id爲Mapper類名(首字母小寫)。數據庫
新建UserQueryMapper.xml配置文件:apache
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.com.sm.mapper.UserQueryMapper"> <select id="findUserById" parameterType="int" resultType="user"> SELECT * FROM USER WHERE id=#{id} </select> </mapper>
定義namespace下的Mapper代理接口:api
package cn.com.sm.mapper; import cn.com.sm.po.User; public interface UserQueryMapper{ public User findUserById(int id) throws Exception; }
編寫測試類:spring-mvc
package cn.com.sm.test; import cn.com.sm.mapper.UserQueryMapper; import cn.com.sm.po.User; import org.junit.Before; import org.junit.jupiter.api.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserMapperTest { private ApplicationContext applicationContext; @Before//是在執行本類全部測試方法以前先調用這個方法 public void setup() throws Exception{ applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); } @Test public void testFindUserById() throws Exception{ UserQueryMapper userQueryMapper=(UserQueryMapper)applicationContext.getBean("userQueryMapper"); User user=userQueryMapper.findUserById(1); System.out.println(user.getId()+":"+user.getUsername()); } }
測試結果:緩存