1.添加依赖包
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--Spring的依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.3.RELEASE</version> </dependency> <!--c3p0的连接池--> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!--mybatis的包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!--spring与mybatis整合需要的包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.21</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> </dependencies>
spring与mybatis整合中mybatis-spring是一个关键的依赖包
2.配置mybatis
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://192.168.133.166:3306/system_security_management?useSSL=false"/> <property name="user" value="layman"/> <property name="password" value="123456"/> </bean> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath*:mybatis/mapper/sqlmapper/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.layman.study.mybatis.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/> </bean>
这里其实只需要配置一个sqlSessionFactoryBean 和 MapperScannerConfigurer
注意:sqlSessionFactoryBeanName属性很重要 一定使用sqlSessionFactoryBeanName 而不是sqlSessionFactory,如是使用sqlSessionFactory 可能导致有的properties文件不能加载,发生一些未知错误。
sqlSessionFactoryBean 中的属性 mapperLocations 指定了需要扫描的mapper接口的实现的xml文件,mapper文件包括插件帮我们生成的也包括我们自己定义的mapper(其实就是我们自定义的Dao的实现)
<?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="com.layman.study.mybatis.dao.SysUserDao"> <resultMap id="BaseResultMap" type="com.layman.study.mybatis.pojo.SysUser"> <id column="id" property="id" jdbcType="BIGINT"/> <result column="user_name" property="userName" jdbcType="VARCHAR"/> <result column="password" property="password" jdbcType="VARCHAR"/> <result column="nick_name" property="nickName" jdbcType="VARCHAR"/> <result column="salt" property="salt" jdbcType="VARCHAR"/> </resultMap> <select id="selectSysUserByPrimaryKey" parameterType="int" resultMap="BaseResultMap"> SELECT * FROM sys_user WHERE id = #{id} </select> </mapper>
MapperScannerConfigurer 中的属性 basePackage 指定了我们的mapper接口
public interface SysUserDao { SysUser selectSysUserByPrimaryKey(@Param(value = "id") Integer id); }
3.测试
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath*:spring/Application-*.xml") public class SysUserTest { private static final Logger LOGGER = LoggerFactory.getLogger(SysUserTest.class); private static final ObjectMapper objectMapper = new ObjectMapper(); @Resource private SysUserService sysUserService; @Test public void test1() throws Exception { SysUser sysUser = sysUserService.selectSysUserByPrimaryKey(1); LOGGER.info(objectMapper.writeValueAsString(sysUser)); } }
结果:
2017-02-16 16:07:50,321 [main] INFO mybatis.SysUserTest - {"id":1,"userName":"Layman","password":"737b0be0e65ddbc20664b377a003c7bd","nickName":"聖殿罗刹","salt":"SWLwSE"}