例子一:
package cn.itcast.service.impl; import java.util.List; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.transaction.annotation.Transactional; import cn.itcast.bean.Person; import cn.itcast.service.PersonService; public class PersonServiceBean implements PersonService{ private JdbcTemplate jdbcTemplate; public void setDataSource(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } public void delete(Integer personid) { jdbcTemplate.update("delete from person where id=?", new Object[]{personid}, new int[]{java.sql.Types.INTEGER}); } public Person getPerson(Integer personid) { return (Person)jdbcTemplate.queryForObject("select * from person where id=?", new Object[]{personid}, new int[]{java.sql.Types.INTEGER}, new PersonRowMapper()); } @SuppressWarnings("unchecked") public List<Person> getPersons() { return (List<Person>)jdbcTemplate.query("select * from person", new PersonRowMapper()); } public void save(Person person) { jdbcTemplate.update("insert into person(name) values(?)", new Object[]{person.getName()}, new int[]{java.sql.Types.VARCHAR}); throw new RuntimeException("运行期例外"); } public void update(Person person) { jdbcTemplate.update("update person set name=? where id=?", new Object[]{person.getName(), person.getId()}, new int[]{java.sql.Types.VARCHAR, java.sql.Types.INTEGER}); } }
例子二:
package cn.itcast.service.impl; import java.util.List; import javax.annotation.Resource; import org.hibernate.SessionFactory; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import cn.itcast.bean.Person; import cn.itcast.service.PersonService; @Transactional public class PersonServiceBean implements PersonService { @Resource private SessionFactory sessionFactory; @Transactional(readOnly=true) public void save(Person person){ sessionFactory.getCurrentSession().persist(person); } public void update(Person person){ sessionFactory.getCurrentSession().merge(person); } @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true) public Person getPerson(Integer personid){ return (Person)sessionFactory.getCurrentSession().get(Person.class, personid); } public void delete(Integer personid){ sessionFactory.getCurrentSession().delete( sessionFactory.getCurrentSession().load(Person.class, personid)); } @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true) @SuppressWarnings("unchecked") public List<Person> getPersons(){ return sessionFactory.getCurrentSession().createQuery("from Person").list(); } }
例子三:HibernateTemplate的用法。
q void delete(Object entity):删除指定持久化实例
q deleteAll(Collection entities):删除集合内全部持久化类实例
q find(String queryString):根据HQL查询字符串来返回实例集合
q findByNamedQuery(String queryName):根据命名查询返回实例集合
q get(Class entityClass, Serializable id):根据主键加载特定持久化类的实例
q save(Object entity):保存新的实例
q saveOrUpdate(Object entity):根据实例状态,选择保存或者更新
q update(Object entity):更新实例的状态,要求entity是持久状态
q setMaxResults(int maxResults):设置分页的大小