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. HTML做的网页 如何使当前页面跳转到另一页面锚点处

    当前页面a.html另一页面b.html当前页面: <a href="b.html#aaa">跳转到b页面aaa处</a>另一页面:<a name=& ...

  2. 关于HashMap的一些深入探索与理解

    在java中.大家差点儿是离不开对集合的使用的,像Map系列,List系列.Set系列.可是非常多人没有了解过或者研究过这些集合类究竟能够用在什么地方,而且有什么注意的地方.因此本文分Map系列和Li ...

  3. 创建线程时如果既传入了runnable对象,又继承thread重写了run方法,会执行的哪里的代码

    1 使用线程的方式,继承thread类,重写run方法 new Thread() { @Override public void run() { System.out.println("我是 ...

  4. UVA437-The Tower of Babylon(动态规划基础)

    Problem UVA437-The Tower of Babylon Accept: 3648  Submit: 12532Time Limit: 3000 mSec Problem Descrip ...

  5. Oracle补丁术语介绍

    在使用Oracle的技术支持服务的时候,经常会遇到补丁相关的术语.现在对这些术语做些解释说明: Interim patch/One-off patch:是我们常说的小补丁,为了修复某(几)个Bug而发 ...

  6. nginx进行反向代理,80端口使用

    环境说明:centos7 安装版本:tengine2.2.2 实现目的: 想用本地80端口,代理其它服务器的非80端口,实现80端口复用 [root@tiaobanji conf]# cat ngin ...

  7. Nginx入门讲解——初步认识了解nginx.conf配置文件以及配置多个虚拟主机

    本文引自网络进攻学习之用https://blog.csdn.net/weixin_38111957/article/details/81080539 一. 引言上节文章讲述了如何用信号控制Nginx服 ...

  8. ESP32:mdns协议

    mdns(即多播dns,Multicast DNS)主要实现了在没有传统DNS服务器的情况下使局域网内的主机实现相互发现和通信,使用的端口为5353,遵从dns协议,使用现有的DNS信息结构.语法和资 ...

  9. 吴恩达课后作业学习1-week4-homework-two-hidden-layer -1

    参考:https://blog.csdn.net/u013733326/article/details/79767169 希望大家直接到上面的网址去查看代码,下面是本人的笔记 两层神经网络,和吴恩达课 ...

  10. MySQL 主主配置

    一.准备 1.两个数据库版本最好保持一致(因为官方就是这么建议的,主要的问题就是考虑到兼容性问题) 2.连个数据库的数据保持一致,若不一致,可手动调整,比如A比B多一个库,那就将这个库导入到B库,达到 ...