zoukankan      html  css  js  c++  java
  • 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="${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>

  • 相关阅读:
    什么是动态链接库
    <<TCP/IP高效编程>>读书笔记
    C++ 函数
    我的vim配置
    FastReport4.6程序员手册_翻译
    DUnit研究初步
    ADO BUG之'无法为更新定位行....' 解决之道
    极限编程的集成测试工具Dunit
    总结
    项目管理检查清单项目启动
  • 原文地址:https://www.cnblogs.com/niwotaxuexiba/p/10292376.html
Copyright © 2011-2022 走看看