日期:2014-05-19 浏览次数:20992 次
public class CMSUser {
private Integer userid;
private String username;
private String password;
public CMSUser(){}
public CMSUser(String username, String password){
this.username = username;
this.password = password;
}
public Integer getUserid() {
return userid;
}
....//以下省略get,set方法.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ailincms.bean">
<class name="CMSUser">
<id name="userid" type="java.lang.Integer">
<generator class="native" />
</id>
<property name="username" length="20" not-null="true" unique="true"/>
<property name="password" length="20" not-null="true" />
</class>
</hibernate-mapping>
@Service(value="cMSUserServiceBean") @Transactional
public class CMSUserServiceBean implements CMSUserService {
@Resource SessionFactory factory;
public void createCMSUser(CMSUser cmsuser) {
factory.getCurrentSession().persist(cmsuser);
}
public void updateCMSUser(CMSUser cmsuser) {
factory.getCurrentSession().merge(cmsuser);
}
public void deleteByUsername(String... usernames) {
for(String username : usernames){
factory.getCurrentSession().delete(factory.getCurrentSession().load(CMSUser.class, username));
}
}
public void deleteByUserid(Integer userid) {
factory.getCurrentSession().delete(factory.getCurrentSession().get(CMSUser.class, userid));
}
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public CMSUser findByUsername(String username) {
//return (CMSUser)factory.getCurrentSession().delete(factory.getCurrentSession().get(CMSUser.class, userid));
return (CMSUser)factory.getCurrentSession().createQuery("from CMSUser as user where user.username='"+username+"'");
//两种方法都试了,都报错。
}
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public CMSUser findByUserid(Integer userid) {
return (CMSUser)factory.getCurrentSession().get(CMSUser.class, userid);
}
@SuppressWarnings("unchecked")
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public List<CMSUser> getAllCMSUser() {
return factory.getCurrentSession().createQuery("from CMSUser").list();
}
}
public class CMSUser_CRUD {
private static CMSUserService userService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
userService = (CMSUserService)act.getBean("cMSUserServiceBean");
}
@Test
public void createCMSUser() {
userService.createCMSUser(new CMSUser(