博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ssh注解开发
阅读量:6844 次
发布时间:2019-06-26

本文共 6870 字,大约阅读时间需要 22 分钟。

引入需要的jar包

@Entitypublic class Teacher {    @Id    @GeneratedValue(strategy=GenerationType.IDENTITY)  private   Integer  tId;  //编号  private   Integer  tAge; //年龄  private   String  tName;//姓名  private   Date  tDate;      @Overridepublic String toString() {    return "Teacher [tId=" + tId + ", tage=" + tAge + ", tName=" + tName            + ", tDate=" + tDate + "]";}public Teacher() {    super();}public Teacher(Integer tId, Integer tage, String tName, Date tDate) {    super();    this.tId = tId;    this.tAge = tage;    this.tName = tName;    this.tDate = tDate;}public Integer gettId() {    return tId;}public void settId(Integer tId) {    this.tId = tId;}public Integer gettAge() {    return tAge;}public void settAge(Integer tAge) {    this.tAge = tAge;}public String gettName() {    return tName;}public void settName(String tName) {    this.tName = tName;}public Date gettDate() {    return tDate;}public void settDate(Date tDate) {    this.tDate = tDate;}  }
Teacher实体类
public interface TeacherDao {    //新增    void  addTeacher(Teacher teacher);     //删除    void  deleteTeacher(Teacher teacher);    //修改    void  updateTeacher(Teacher teacher);    //查询    List
findTeachers(); //根据ID查询指定的teacher Teacher findById(Integer id); }
TeacherDao
@Repository("teacherDao")public class TeacherDaoImpl implements TeacherDao {    @Autowired   // byType    private SessionFactory sessionFactory;        // 新增    public void addTeacher(Teacher teacher) {        sessionFactory.getCurrentSession().save(teacher);    }    // 删除    public  void deleteTeacher(Teacher teacher){        sessionFactory.getCurrentSession().delete(teacher);    }    // 修改    public void updateTeacher(Teacher teacher){        sessionFactory.getCurrentSession().update(teacher);    }    // 查询    public List
findTeachers(){ return sessionFactory.getCurrentSession().createQuery("from Teacher").list(); } public Teacher findById(Integer id) { //OpenSessionInView //return (Teacher)sessionFactory.getCurrentSession().get(Teacher.class, id); return (Teacher) sessionFactory.getCurrentSession().load(Teacher.class, id); } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
TeacherDaoImpl
public interface TeacherService {    // 新增    void addTeacher(Teacher teacher);    // 删除    void deleteTeacher(Teacher teacher);    // 修改    void updateTeacher(Teacher teacher);    // 查询    List
findTeachers(); //根据ID查询指定的teacher Teacher findById(Integer id);}
TeacherService
@Service("teacherService")public class TeacherServiceImpl implements TeacherService {    @Resource(name="teacherDao")   //byName    private TeacherDao dao;        //新增    @Transactional    public void addTeacher(Teacher teacher) {       dao.addTeacher(teacher);    }    //删除    @Transactional    public void deleteTeacher(Teacher teacher) {        dao.deleteTeacher(teacher);    }    //修改    @Transactional    public void updateTeacher(Teacher teacher) {        dao.updateTeacher(teacher);    }    //查询所有    @Transactional(readOnly=true)    public List
findTeachers() { return dao.findTeachers(); } //查询指定的教师 @Transactional(readOnly=true) public Teacher findById(Integer id) { return dao.findById(id); } public TeacherDao getDao() { return dao; } public void setDao(TeacherDao dao) { this.dao = dao; } }
TeacherServiceImpl
@Namespace("/")@ParentPackage("struts-default")@Componentpublic class AddAction extends ActionSupport {    private  String  name;    private  Integer  age;    private  Integer  id;    @Autowired    @Qualifier("teacherService")   //@Resource(name="teacherService")     private TeacherService  service;         public  String add(){         System.out.println("进入ladd");         Teacher teacher=new Teacher();         teacher.settAge(age);         teacher.settName(name);         service.addTeacher(teacher);         return  SUCCESS;     }          @Action(value="AddServlet",results={@Result(location="/success.jsp")})     public  String find(){         Teacher teacher=service.findById(id);         System.out.println(teacher);         return  SUCCESS;     }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public TeacherService getService() {        return service;    }    public void setService(TeacherService service) {        this.service = service;    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }     }
AddAction
update
true
true
org.springframework.orm.hibernate5.SpringSessionContext
org.hibernate.dialect.MySQLInnoDBDialect
applicationContext.xml文件
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
open
org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
open
/*
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
AddServlet1
cn.bdqn.servlet.AddServlet
AddServlet
/AddServlet
index.jsp
web.xml文件

jdbc.properties文件自己定义即可

转载于:https://www.cnblogs.com/xtdxs/p/7097231.html

你可能感兴趣的文章