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="${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>
<!-- sqlSessonFactory的配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据库连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载配置文件 -->
<property name="configLocation" value="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="cn.itheima.mybatis.mapper.UserMapper"></property>
注入sqlSessionFactory
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean> -->
<!-- 配置包扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置要扫描的包 ,如果扫描多个包使用半角逗号分隔-->
<!-- <property name="basePackage" value="cn.itheima.mybatis.mapper,com.itheima.mybatis.mapper"/> -->
<property name="basePackage" value="com.itheima.mybatis.mapper"/>
</bean>
</beans>

Mybatis+Spring整合后Mapper测试类编写的更多相关文章

  1. MyBatis Spring整合配置映射接口类与映射xml文件

    本文转自http://blog.csdn.net/zht666/article/details/38706083 Spring整合MyBatis使用到了mybatis-spring,在配置mybati ...

  2. 通过模拟Mybatis动态代理生成Mapper代理类,讲解Mybatis核心原理

    本文将通过模拟Mybatis动态代理生成Mapper代理类,讲解Mybatis原理 1.平常我们是如何使用Mapper的 先写一个简单的UserMapper,它包含一个全表查询的方法,代码如下 pub ...

  3. IntelliJ IDEA 2017版 SpringBoot测试类编写

    SpringBoot的测试类编写Demo 源码见 https://github.com/liushaoye/baseone.git

  4. 解决spring mybatis 整合后mapper接口注入失败

    spring整合mybatis,在dao层我们只写一个接口,配置相应的*mapper.xml文件, 报如下错误: org.springframework.beans.factory.Unsatisfi ...

  5. Mybatis 和Spring整合之mapper代理开发

    F:\1ziliao\mybatis\代码 1.1 SqlMapConfig.xml <?xml version="1.0" encoding="UTF-8&quo ...

  6. SpringMVC+Mybatis+Spring整合

    Maven引入需要的JAR包 pom.xml <properties> <!-- spring版本号 --> <spring.version>4.0.2.RELEA ...

  7. springMVC+MyBatis+Spring 整合(3)

    spring mvc 与mybatis 的整合. 加入配置文件: spring-mybaits.xml <?xml version="1.0" encoding=" ...

  8. Mybatis——Spring整合

    一.引入依赖 Spring的相关jar包 mybatis-3.4.1.jar mybatis-spring-1.3.0.jar mysql-connector-java-5.1.37-bin.jar ...

  9. springMVC+mybatis+spring整合案例

    1.web.xml a:配置spring监听,使web容器在启动时加载spring的applicationContext.xml <listener> <listener-class ...

随机推荐

  1. P3399 丝绸之路 dp

    题目背景 张骞于公元前138年曾历尽艰险出使过西域.加强了汉朝与西域各国的友好往来.从那以后,一队队骆驼商队在这漫长的商贸大道上行进,他们越过崇山峻岭,将中国的先进技术带向中亚.西亚和欧洲,将那里的香 ...

  2. [NOI2017]蔬菜

    [NOI2017]蔬菜 题目描述 大意就是有\(n\)种物品,第\(i\)个物品有\(c_i\)个,单价是\(a_i\).然后每天你可以卖出最多\(m\)个物品.每天结束后第\(i\)种物品会减少\( ...

  3. 什么是数据库ACID?

    原子性:由于操作失败导致的数据碎片错误: 一致性:由于并发导致的数据库数据错误(与预期不一致): 隔离性:由于并发导致的当前使用数据(应用端)错误: 事务在当今的企业系统无处不在,即使在高并发环境下也 ...

  4. sqlite线程模式的设置

    (1)编译阶段 这几种模式可以通过参数SQLITE_THREADSAFE在编译阶段指定,可以取值0,1,2,默认是1.这三种取值的含义如下: 0:单线程模式,即内部不做mutex保护,多线程运行sql ...

  5. java读取properties中文乱码

    1 确认properties文件的编码是utf-8 2 采用流的方式读取文件,设置编码为utf-8 public class ErrorCodeConfig { static Properties p ...

  6. mysql之grant权限说明

    mysql中给一个用户授权如select,insert,update,delete等其中的一个或者多个权限,主要使用grant命令,格式为: 给没有用户授权 grant 权限 on 数据库对象 to ...

  7. linux命令之df

    功能:查看文件系统的磁盘空间使用情况 常用选项: -a 包含虚拟文件系统 -h 可易读单位显示 -i 显示 inode 信息而非块使用量 -k 1K 块的数量 -t 只显示指定文件系统为指定类型的信息 ...

  8. ENQ: KO - FAST OBJECT CHECKPOINT tips

    ENQ: KO - FAST OBJECT CHECKPOINT tips Question: What does the wait event ENQ: KO - FAST OBJECT CHECK ...

  9. PyCharm 中使用 Pylint 控制代码质量

    1) Pylint安装 - Windows下: 直接在 cmd 下使用 pip install pylint 即可(如果 pip 不可用,首先安装最新版 Python,会默认安装 pip,或者找到 p ...

  10. if选择语句与switch选择语句的比较、区别及应用场景

    if选择语句和switch选择语句的比较: 1.switch语句只支持常量值相等的分支判断,而if语句支持更为灵活,任意布尔表达式均可: 2.switch语句通常比一系列嵌套if语句效率更高:逻辑更加 ...