Mybatis+Spring整合后Mapper测试类编写

【Mybatis+Spring整合后Mapper测试类编写】仰天大笑出门去,我辈岂是蓬蒿人。这篇文章主要讲述Mybatis+Spring整合后Mapper测试类编写相关的知识,希望能为你提供帮助。

public class UserMapperTest {private ApplicationContext applicationContext; @Before public void init() throws Exception { //初始化spring容器 applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); }@Test public void testDeleteByPrimaryKey() { fail("Not yet implemented"); }@Test public void testInsert() { UserMapper userMapper = applicationContext.getBean(UserMapper.class); User user = new User(); user.setUsername("武大郎"); user.setSex("1"); user.setBirthday(new Date()); user.setAddress("河北清河县"); userMapper.insert(user); }@Test public void testSelectByExample() { UserMapper userMapper = applicationContext.getBean(UserMapper.class); UserExample example = new UserExample(); //Criteria criteria = example.createCriteria(); //criteria.andUsernameLike("%张%"); //执行查询 List< User> list = userMapper.selectByExample(example); for (User user : list) { System.out.println(user); } }@Test public void testSelectByPrimaryKey() { UserMapper userMapper = applicationContext.getBean(UserMapper.class); User user = userMapper.selectByPrimaryKey(10); System.out.println(user); }@Test public void testUpdateByPrimaryKey() { fail("Not yet implemented"); }}

applicationContext.xml:
< ?xml version="1.0" encoding="UTF-8"?> < beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> < !-- 加载配置文件 --> < context:property-placeholder location="classpath:db.properties" /> < !-- 数据库连接池 --> < bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> < property name="driverClassName" value="https://www.songbingjia.com/android/${jdbc.driver}" /> < property name="url" value="https://www.songbingjia.com/android/${jdbc.url}" /> < property name="username" value="https://www.songbingjia.com/android/${jdbc.username}" /> < property name="password" value="https://www.songbingjia.com/android/${jdbc.password}" /> < property name="maxActive" value="https://www.songbingjia.com/android/10" /> < property name="maxIdle" value="https://www.songbingjia.com/android/5" /> < /bean> < !-- sqlSessonFactory的配置 --> < bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> < !-- 配置数据库连接池 --> < property name="dataSource" ref="dataSource"> < /property> < !-- 加载配置文件 --> < property name="configLocation" value="https://www.songbingjia.com/android/classpath:mybatis/SqlMapConfig.xml"> < /property> < /bean> < !-- 传统dao的配置方法 --> < bean id="userDaoImpl" class="cn.itheima.mybatis.dao.impl.UserDaoImpl"> < !-- 注入sqlSessionFactory --> < property name="sqlSessionFactory" ref="sqlSessionFactory"/> < /bean> < !-- mapper代理形式dao的配置 --> < !-- 第一种方法,配置代理对象 --> < !-- < bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 设置代理的mapper接口 < property name="mapperInterface" value="https://www.songbingjia.com/android/cn.itheima.mybatis.mapper.UserMapper"> < /property> 注入sqlSessionFactory < property name="sqlSessionFactory" ref="sqlSessionFactory"/> < /bean> --> < !-- 配置包扫描器 --> < bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> < !-- 配置要扫描的包 ,如果扫描多个包使用半角逗号分隔--> < !-- < property name="basePackage" value="https://www.songbingjia.com/android/cn.itheima.mybatis.mapper,com.itheima.mybatis.mapper"/> --> < property name="basePackage" value="https://www.songbingjia.com/android/com.itheima.mybatis.mapper"/> < /bean> < /beans>

Mybatis+Spring整合后Mapper测试类编写

文章图片

 

    推荐阅读