标签:
在上篇文章中,我建立了工程并配置了spring + hibernate。今天我们检验下上篇文章的成果,如何检查呢?那就是进行单元测试。本篇文章就让大家和我一起来就前面的建的工程进行单元测试。
package com.ympcsoft.sys.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
//注解,说明这是实体类
@Entity
@Table(name = "SysUser")
public class SysUser {
//定义字段
//主键字段,统一叫id
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY) //字段自增长
@Column(name = "id")
private Long id;
//用户名,32长度,不能为空
@Column(name="USER_NAME", length=32, nullable = false)
private String userName;
//用户密码,32长度,不能为空
@Column(name="USER_PWD", length=32, nullable=false)
private String userPwd;
//构造函数
public SysUser() {
super();
// TODO Auto-generated constructor stub
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
}
package com.ympcsoft.sys.dao;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import com.ympcsoft.sys.domain.SysUser;
/**
* 此处分层来讲应该是SysUserDao接口,然后实现,现在学习的需要,直接访问SessoinFactory
* */
@Repository
public class SysUserDao{
//注解来源
@Resource(name="sessionFactory")
private SessionFactory sessionFactory;
//保存用户
public void addUser(SysUser sysUser) {
//获取Session
Session session = sessionFactory.getCurrentSession();
//持久到数据库
session.persist(sysUser);
}
}
package com.ympcsoft.sys.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ympcsoft.sys.dao.SysUserDao;
import com.ympcsoft.sys.domain.SysUser;
@Transactional
@Service
public class SysUserService {
private SysUserDao sysUserDao;
@Resource
public void setSysUerDao(SysUserDao sysUserDao){
this.sysUserDao = sysUserDao;
}
public void addUser(SysUser sysUser){
sysUserDao.addUser(sysUser);
}
}
package services;
import javax.annotation.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.ympcsoft.sys.dao.SysUserDao;
import com.ympcsoft.sys.domain.SysUser;
import com.ympcsoft.sys.service.SysUserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:spring.xml" })
public class SysUserTest {
@Resource
private SysUserDao sysUserDao;
@Resource
private SysUserService sysUserService;
@Before
public void setUp() throws Exception {
}
@Test
public final void testSave() {
SysUser sysUser = new SysUser();
sysUser.setUserName("admin");
sysUser.setUserPwd("123456");
sysUserService.addUser(sysUser);
}
}
spring mvc+spring + hibernate 整合(二)
标签:
原文地址:http://www.cnblogs.com/ympcsoft/p/5244195.html