1. public class UserMapperTest {
  2.  
  3. private ApplicationContext applicationContext;
  4.  
  5. @Before
  6. public void init() throws Exception {
  7. //初始化spring容器
  8. applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
  9. }
  10.  
  11. @Test
  12. public void testDeleteByPrimaryKey() {
  13. fail("Not yet implemented");
  14. }
  15.  
  16. @Test
  17. public void testInsert() {
  18. UserMapper userMapper = applicationContext.getBean(UserMapper.class);
  19. User user = new User();
  20. user.setUsername("武大郎");
  21. user.setSex("1");
  22. user.setBirthday(new Date());
  23. user.setAddress("河北清河县");
  24. userMapper.insert(user);
  25. }
  26.  
  27. @Test
  28. public void testSelectByExample() {
  29. UserMapper userMapper = applicationContext.getBean(UserMapper.class);
  30. UserExample example = new UserExample();
  31. //Criteria criteria = example.createCriteria();
  32. //criteria.andUsernameLike("%张%");
  33. //执行查询
  34. List<User> list = userMapper.selectByExample(example);
  35. for (User user : list) {
  36. System.out.println(user);
  37. }
  38. }
  39.  
  40. @Test
  41. public void testSelectByPrimaryKey() {
  42. UserMapper userMapper = applicationContext.getBean(UserMapper.class);
  43. User user = userMapper.selectByPrimaryKey(10);
  44. System.out.println(user);
  45. }
  46.  
  47. @Test
  48. public void testUpdateByPrimaryKey() {
  49. fail("Not yet implemented");
  50. }
  51.  
  52. }

applicationContext.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  8. 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
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  10. <!-- 加载配置文件 -->
  11. <context:property-placeholder location="classpath:db.properties" />
  12. <!-- 数据库连接池 -->
  13. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  14. destroy-method="close">
  15. <property name="driverClassName" value="${jdbc.driver}" />
  16. <property name="url" value="${jdbc.url}" />
  17. <property name="username" value="${jdbc.username}" />
  18. <property name="password" value="${jdbc.password}" />
  19. <property name="maxActive" value="10" />
  20. <property name="maxIdle" value="5" />
  21. </bean>
  22. <!-- sqlSessonFactory的配置 -->
  23. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  24. <!-- 配置数据库连接池 -->
  25. <property name="dataSource" ref="dataSource"></property>
  26. <!-- 加载配置文件 -->
  27. <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
  28. </bean>
  29.  
  30. <!-- 传统dao的配置方法 -->
  31. <bean id="userDaoImpl" class="cn.itheima.mybatis.dao.impl.UserDaoImpl">
  32. <!-- 注入sqlSessionFactory -->
  33. <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
  34. </bean>
  35. <!-- mapper代理形式dao的配置 -->
  36. <!-- 第一种方法,配置代理对象 -->
  37. <!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  38. 设置代理的mapper接口
  39. <property name="mapperInterface" value="cn.itheima.mybatis.mapper.UserMapper"></property>
  40. 注入sqlSessionFactory
  41. <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
  42. </bean> -->
  43. <!-- 配置包扫描器 -->
  44. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  45. <!-- 配置要扫描的包 ,如果扫描多个包使用半角逗号分隔-->
  46. <!-- <property name="basePackage" value="cn.itheima.mybatis.mapper,com.itheima.mybatis.mapper"/> -->
  47. <property name="basePackage" value="com.itheima.mybatis.mapper"/>
  48. </bean>
  49. </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. MySQL高级知识(七)——索引面试题分析

    前言:该篇随笔通过一些案例,对索引相关的面试题进行分析. 0.准备 #1.创建test表(测试表). drop table if exists test; create table test( id ...

  2. Linux 27 岁了!盘点 Linux 的 27 件趣事

    Linux 27 岁了!盘点 Linux 的 27 件趣事 许多人认为10月5日是 Linux 系统的周年纪念日,因为这是 Linux 在1991年首次对外公布的时间.不过,你可能不知道的是,早在19 ...

  3. ElasticSearch(一):CentOS7 安装 ElasticSearch6.4.0

    一.安装jre支持 在CentOS中安装ElasticSearch需要Java1.8.0,可执行命令#  java -version查看当前系统所安装Java版本是否为1.8.0版本. 若非1.8.0 ...

  4. w3m 在ubuntu中的使用

    w3m 使用总结 安装 sudo apt install w3m终端 w3m www.baidu.com 即可打开w3m是个开放源代码的命令行下面的网页浏览器.一般的linux系统都会自带这个工具,可 ...

  5. 转://创建oracle索引时需要注意的7个事项

    在创建Oracle索引时,有一些问题使我们需要注意的,下面就为您介绍创建oracle索引的一些注意事项,希望对您学习创建Oracle索引方面能有所帮助. 1.一般来说,不需要为比较小的表创建索引: 2 ...

  6. mongodb创建用户(转发)

    参考文档: https://www.cnblogs.com/itxiongwei/p/5520863.html MongoDB 缺省是没有设置鉴权的,业界大部分使用 MongoDB 的项目也没有设置访 ...

  7. Linux:Day6(下) vim编辑器

    vim编辑器 简介: vi:Visual Interface,文本编辑器 文本:ASCII,Unicode 文本编辑种类: 行编辑器:sed 全屏编辑器:nano,vi VIM - Vi IMprov ...

  8. 【vue】钩子函数生命周期

    图1 图2: 图3 相关资料:http://www.zhimengzhe.com/Javascriptjiaocheng/236707.html    https://segmentfault.com ...

  9. esp8266 免费wifi强推广告神器(0) 项目介绍

    某宝产品 WIFI SSID广告终端路由推广宝 简单来说,手机连接免费wifi,自动弹出广告页面,有二维码和电话,点击电话直接打电话给商家客服,用户点击链接跳转到商家网页. 同时存在设置页面,使用者可 ...

  10. RabbitMq 6种使用模式

    RabbitMQ的5种模式与实例 1.1 简单模式Hello World 功能:一个生产者P发送消息到队列Q,一个消费者C接收 生产者实现思路: 创建连接工厂ConnectionFactory,设置服 ...