Mybatis3+Spring3集成测试

阅读更多

一、简介

二、包结构展示

三、重点部分实现

********************************************************

一、简介

     因为大家对mybatis不是很熟悉,这里通过一个简单的实例展示一下,mybatis和spring集成后的开发过程。

二、包结构展示

Mybatis3+Spring3集成测试_第1张图片
 

三、重点部分实现

 

测试类:UserServiceTest

 

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:applicationContext-test.xml" })
public class UserServiceTest {

    private UserService userService;

    @Resource(name = "userService")
    public void setUserService(UserService userService) {
	this.userService = userService;
    }
        @Test
    public void testSaveUser() {
	User user = new User();
	user.setId(2);
	user.setAge(2);
	user.setUsername("张海兵");
	
	userService.saveUser(user);
    }
}

 

业务逻辑实现类:UserServiceImpl.java

 

@Component("userService")
public class UserServiceImpl implements UserService {

    private UserDao userDao;

    @Resource(name="userDao")
    public void setUserDao(UserDao userDao) {
	this.userDao = userDao;
    }

    @Override
    public void saveUser(User user) {
	userDao.save(user);
    }

 

 DAO实现类:

@Component("userDao")
public class UserDaoImpl implements UserDao {

    @Resource(name="userMapper")
    private UserMapper userMapper;

    @Override
    public void save(User user) {
	userMapper.save(user);
	System.out.println(user.getUsername() + " save successful!");
    }

 

配置文件:UserMapper.xml




	
	
		INSERT INTO user(id,age,gender,username,password,mail,nickname,site)
		VALUES(#{id},#{age},#{gender},#{username},#{password},#{mail},#{nickname},#{site})
	

 

  • Mybatis3+Spring3集成测试_第2张图片
  • 大小: 14.8 KB
  • 查看图片附件

你可能感兴趣的:(mybatis3,spring3,integration)