MyEclipse6Java开发中文教程 public Student (Integer id, String username, String password, Intege this, username username public Integer getId() t public void d(Integer id) this. id id public string getUsername() i eturn this username this. username username public string getPassword() t eturn this password this password passwor ublic, Integer return this agei public c void setAge(Integer age)( DAO层核心类dao. StudentDAO.java 刘长炯著
MyEclipse 6 Java 开发中文教程 16 刘长炯著 public Student(Integer id, String username, String password, Integer age) { this.id = id; this.username = username; this.password = password; this.age = age; } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return this.age; } public void setAge(Integer age) { this.age = age; } } DAO 层核心类 dao.StudentDAO.java: package dao;
MyEclipse6Java开发中文教程 ort org. apache commons logging. Logfact import org. hibernate. LockMode import org. springframework context. Applicationcontex IPOT org. springframework orm hibernate3 support. HibernateDaoSupport: import org. springframework transaction annotation Transactional * Student的DAo层,加入了事务标注 TRansactional 8 public class Student DAO extends HibernateDaoSupport t private static final Log log Log Factory getLog(student DAo class) // property constants public static final String USERNAME use public static final String PAsSWORD =pa public static final String AGE =age protected void initDao()( public void save (Student transientInstance)( log debug("saving Student/instance getHibernateTemplate(). save(transientInstance)i catch (RuntimeException re) log error("save failed throw re public void delete(student persistentInstance)( log debug (" deleting student instance")i getHibernateTemplate(). delete(persistentInstance) y catch (RuntimeException re) log error( delete failed", re) 刘长炯著
MyEclipse 6 Java 开发中文教程 17 刘长炯著 import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.LockMode; import org.springframework.context.ApplicationContext; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.transaction.annotation.Transactional; /** * Student 的 DAO 层,加入了事务标注。 */ @Transactional public class StudentDAO extends HibernateDaoSupport { private static final Log log = LogFactory.getLog(StudentDAO.class); // property constants public static final String USERNAME = "username"; public static final String PASSWORD = "password"; public static final String AGE = "age"; protected void initDao() {} public void save(Student transientInstance) { log.debug("saving Student instance"); try { getHibernateTemplate().save(transientInstance); log.debug("save successful"); } catch (RuntimeException re) { log.error("save failed", re); throw re; } } public void delete(Student persistentInstance) { log.debug("deleting Student instance"); try { getHibernateTemplate().delete(persistentInstance); log.debug("delete successful"); } catch (RuntimeException re) { log.error("delete failed", re); throw re; } }
MyEclipse6Java开发中文教程 public Student findById(java. lang Integer id)I log debug ("getting student instance with id: id) Student instance Student) getHibernateTemplate(). get( dao. Student", id)i return instance catch (RuntimeException re) logerror ("get failed", re)i public List findByExample(Student instance) log debug ("finding Student instance by example try i List results getHibernateTemplate(). findByExample (instance logdebug("find by example successful, result size results size()) return results 1 catch (RuntimeException re) log error("find by example failed", re)i public List findBy Property(string propertyName, Object value)( log debug ("finding student instance with property:"+ value:"+ value)i try String querystring ="from Student as model where model return getHibernateTemplate(). find (querystring, value) catch (RuntimeException re)t log error("find by property name failed", re)i public List findByUsername(object username) return findBy Property(USERNAME, username) 刘长炯著
MyEclipse 6 Java 开发中文教程 18 刘长炯著 public Student findById(java.lang.Integer id) { log.debug("getting Student instance with id: " + id); try { Student instance = (Student) getHibernateTemplate().get( "dao.Student", id); return instance; } catch (RuntimeException re) { log.error("get failed", re); throw re; } } public List findByExample(Student instance) { log.debug("finding Student instance by example"); try { List results = getHibernateTemplate().findByExample(instance); log.debug("find by example successful, result size: " + results.size()); return results; } catch (RuntimeException re) { log.error("find by example failed", re); throw re; } } public List findByProperty(String propertyName, Object value) { log.debug("finding Student instance with property: " + propertyName + ", value: " + value); try { String queryString = "from Student as model where model." + propertyName + "= ?"; return getHibernateTemplate().find(queryString, value); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } public List findByUsername(Object username) { return findByProperty(USERNAME, username); }
MyEclipse6Java开发中文教程 return findBy Property( PASSWORD, password)i public List findByAge(object age) i return findBy Property(AGe, age) public List findAll()( log debug(" finding all Student instances") try i return getHibernateTemplate(). find(querystring 3 catch (RuntimeException re) t logerror("find all failed",re)i public Student merge(Student detache ("merging Student ins Student result = (Student) getHibernateTemplate().merge detachedInstance)i log debug ("merge successful return resultit 3 catch (RuntimeException re) log error(merge failed e) th ¥。wre public void attachDirty(student instance) log debug ("attaching dirty student instance") getHibernateTemplate(). saveOrUpdate(instance) log debug("attach successful")i catch (RuntimeException re)[ logerror(" attach failed", re) throw re public void attachclean(Student instance)( 刘长炯著
MyEclipse 6 Java 开发中文教程 19 刘长炯著 public List findByPassword(Object password) { return findByProperty(PASSWORD, password); } public List findByAge(Object age) { return findByProperty(AGE, age); } public List findAll() { log.debug("finding all Student instances"); try { String queryString = "from Student"; return getHibernateTemplate().find(queryString); } catch (RuntimeException re) { log.error("find all failed", re); throw re; } } public Student merge(Student detachedInstance) { log.debug("merging Student instance"); try { Student result = (Student) getHibernateTemplate().merge( detachedInstance); log.debug("merge successful"); return result; } catch (RuntimeException re) { log.error("merge failed", re); throw re; } } public void attachDirty(Student instance) { log.debug("attaching dirty Student instance"); try { getHibernateTemplate().saveOrUpdate(instance); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public void attachClean(Student instance) {
MyEclipse6Java开发中文教程 log debug("attaching clean Student instance")i getHibernateTemplate().lock(instance, LockMode. NONE); log debug("attach successful)i 3 catch (RuntimeException re) t log error(" attach failed", re) throw re public static StudentDAO getFromApplicationcontext(ApplicationContext ctx) return (StudentDAo)ctx getBean("StudentDAO") 测试类 test. StudentDAOTest java import org. springframework context. Applicationcontexti org. springframework context support Class PathXmlApplicationContext import dao. *i public class StudentDAOTest i public static void main(String[] args) Applicationcontext ctx et ClassPathXmlApplicationContext("applicationContext. xml")i Stude (StudentDAo)ctx getBean("StudentDAO") Student user new student ()i user. setusername(" spring hibernate标注事务测试"); user staGe(200)i dao. save(user)i 在本节开发完毕后我们应该进行一些测试,运行最后给出的测试类 test studentDAo rest,然后到数据库中去检查记录是否已经成功插入数据库。实际开发中 我们也应该这样,确保新加入的功能都没有问题然后再继续往下进行进行后续的开发。如果 刘长炯著
MyEclipse 6 Java 开发中文教程 20 刘长炯著 log.debug("attaching clean Student instance"); try { getHibernateTemplate().lock(instance, LockMode.NONE); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public static StudentDAO getFromApplicationContext(ApplicationContext ctx) { return (StudentDAO) ctx.getBean("StudentDAO"); } } 测试类 test.StudentDAOTest.java: package test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import dao.*; public class StudentDAOTest{ public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); StudentDAO dao = (StudentDAO)ctx.getBean("StudentDAO"); Student user = new Student(); user.setPassword("密码"); user.setUsername("spring hibernate 标注事务测试"); user.setAge(200); dao.save(user); } } 在本节开发完毕后我们应该进行一些测试,运行最后给出的测试类 test.StudentDAOTest,然后到数据库中去检查记录是否已经成功插入数据库。实际开发中 我们也应该这样,确保新加入的功能都没有问题然后再继续往下进行进行后续的开发。如果