//Dao层
package com.bear.cakeonline.test;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;
import com.bear.cakeonline.entity.UserInfo;
import com.bear.cakeonline.entity.UserLogin;
@Repository
public class TestDaoImpl {
@Resource
private SessionFactory sessionFactory;
public void saveUser(){
UserLogin ul=new UserLogin();
ul.setLoginName("zs");
ul.setPassword("123");
UserInfo ui=new UserInfo();
ui.setRealName("zhangsan");
ul.setUserInfo(ui);
ui.setUserLogin(ul);
Session s=this.sessionFactory.openSession();
Transaction t=s.beginTransaction();
s.save(ul);
t.commit();
s.close();
}
}
//service层
package com.bear.cakeonline.test;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly=false)
public class TestServiceImpl {
@Resource
private TestDaoImpl testDaoImpl;
public void regist(){
this.testDaoImpl.saveUser();
}
}
//controller层
package com.bear.cakeonline.test;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class TestController {
@Resource
private TestServiceImpl testServiceImpl;
@RequestMapping("/regist")
public String regist(){
System.out.println("lskdfj");
this.testServiceImpl.regist();
return "";
}
}