实体类省略
PersonDAO层
@Repository
public class PersonDao {
// 如何获取和当前事务相关联的EntityManager对象呢
// 通过@PersistenceContext注解来标记成员变量
@PersistenceContext
private EntityManager entityManager;
public void save(Person person) {
entityManager.persist(person);
}
}
PersonService层
@Service
public class PersonService {
@Autowired
private PersonDao personDao;
@Transactional
public void savePersons(Person person) {
personDao.save(person);
}
}
测试层
private ApplicationContext ctx = null;
private PersonService personService = null;
{
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
personService = ctx.getBean(PersonService.class);
}
测试层核心代码
@Test
public void testPersonService(){
Person p=new Person();
p.setAge(12);
p.setEmail("131@.com");
p.setLastName("AA");
personService.savePersons(p);
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。