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>

最新文章

  1. nodejs 安装
  2. StringHelper
  3. LeetCode之104. Maximum Depth of Binary Tree
  4. [应用][js+css3]3D盒子导航[PC端]
  5. 备忘:maven 错误信息: Plugin execution not covered by lifecycle configuration
  6. poj3294 出现次数大于n/2 的公共子串
  7. js 数组 转
  8. 在Linux系统中如何把文件拷贝到U盘
  9. eclipse中安装genymotion
  10. Eclipse + CDT + YAGARTO + J-Link,STM32开源开发环境搭建与调试
  11. HTML+CSS笔记 CSS进阶续集
  12. IT 名企招聘信息
  13. QTableView 添加进度条 添加按钮 TreeWidget 增删改
  14. 使用Angular CLI进行单元测试和E2E测试
  15. MIUI9系统怎么卡刷开发版获取ROOT超级权限
  16. git 远程
  17. Java容器解析系列(8) Comparable Comparator
  18. BZOJ 3561 DZY Loves Math VI
  19. [Beego模型] 六、事务处理
  20. iOS 阅读唐巧博客心得

热门文章

  1. postgreSQL 应用case when的例子
  2. Jenkins的环境部署
  3. SQlite的结构——存储管理
  4. pip freeze 打包依赖库及setup.py
  5. 牛客网训练1--------矩阵 (二份+二维矩阵hash)
  6. 【转】bios与CMOS有什么区别
  7. Python:Day17 生成器、time和random模块
  8. 【window】window10永久关闭更新
  9. ORA-28040: No matching authentication protocol
  10. argparse模块