当前位置: 代码迷 >> Eclipse >> SSH的学习有关问题?不太通求教。20110729
  详细解决方案

SSH的学习有关问题?不太通求教。20110729

热度:326   发布时间:2016-04-23 18:59:21.0
SSH的学习问题?不太通求教。20110729
开始
111111
package cn.itcast.bean;

public class Employee {

private String username;
private String password;
private Gender gender = Gender.MAN;

public Employee() {
//TODO
}

public Employee(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
}




222222
package cn.itcast.service;

import java.util.List;

import cn.itcast.bean.Employee;

public interface EmployeeService {
public void save(Employee employee);
public void update(Employee employee);
public Employee find(String username);
public void delete(String... username);
public List<Employee> list(Employee employee);
}



333333
package cn.itcast.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bean.Employee;
import cn.itcast.service.EmployeeService;

@Service @Transactional
public class EmployeeServiceBean implements EmployeeService {

@Resource SessionFactory factory;


public void save(Employee employee) {
factory.getCurrentSession().persist(employee);
}

public void update(Employee employee) {
factory.getCurrentSession().update(employee);

}

@Transactional(propagation=Propagation.NOT_SUPPORTED)
public Employee find(String username) {
return (Employee)factory.getCurrentSession().get(Employee.class, username);
}


public void delete(String... username) {
for (String name : username) {
factory.getCurrentSession().delete(factory.getCurrentSession().load(Employee.class, name));
}

}

@SuppressWarnings("unchecked")
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public List<Employee> list(Employee employee) {
return factory.getCurrentSession().createQuery("from Employee").list();

}

}


444444
package junit.test;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.bean.Employee;
import cn.itcast.service.EmployeeService;

/**
 * @author Administrator
 *
 */
public class EmployeeTest {
private static EmployeeService employeeService;
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {

try {
ApplicationContext act = new ClassPathXmlApplicationContext(
"beans.xml");
employeeService = (EmployeeService) act.getBean("employeeServiceBean");
} catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void save() {
employeeService.save(new Employee("liming","12345"));
}
@Test
public void update() {
new ClassPathXmlApplicationContext("beans.xml");
}
@Test
public void delete() {