<v:shapetype o:spt="75" coordsize="21600,21600" filled="f" stroked="f" id="_x0000_t75" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:extrusionok="f" o:connecttype="rect" gradientshapeok="t"></v:path><o:lock v:ext="edit" aspectratio="t"></o:lock></v:shapetype><v:shape id="_x0000_s1026" type="#_x0000_t75" wrapcoords="-90 0 -90 21459 21600 21459 21600 0 -90 0" style="MARGIN-TOP: 5.4pt; Z-INDEX: -1; LEFT: 0px; MARGIN-LEFT: 0px; WIDTH: 179.25pt; POSITION: absolute; HEIGHT: 114.75pt; TEXT-ALIGN: left"><v:imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image001.jpg" o:title="spring+hibernate"></v:imagedata><w:wrap type="tight"></w:wrap></v:shape>à总体结构布局
à存放所有文件的包
àStudent POJO
àStudent DAO
àDAO 实现类
à实现类测试
àStudent数据表映射文件
àApplicationContext.xml配置文件
<o:p> </o:p>
1.首先加入我们的框架支持<o:p></o:p>
先Spring后hibernate,将hibernate-cfg.xml整合到applicationContext中,注意sessionFactory和dataSource文件的创建。
2.从数据表中建立我们需要的POJO DAO POJO.hbm.xml文件<o:p></o:p>
①POJO<o:p></o:p>
package all;
public class Student implements java.io.Serializable {
private String id;
private String name;
private String age;
public Student() {
}
public Student(String name, String age) {
this.name = name;
this.age = age;
}
…..setter()…..getter()…..
}
②DAO<o:p></o:p>
package all;<o:p></o:p>
<o:p> </o:p>
import java.util.List;<o:p></o:p>
import org.apache.commons.logging.Log;<o:p></o:p>
import org.apache.commons.logging.LogFactory;<o:p></o:p>
import org.hibernate.LockMode;<o:p></o:p>
import org.springframework.context.ApplicationContext;<o:p></o:p>
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;<o:p></o:p>
public class StudentDAO extends HibernateDaoSupport {<o:p></o:p>
private static final Log log = LogFactory.getLog(StudentDAO.class);<o:p></o:p>
public static final String NAME = "name";<o:p></o:p>
public static final String AGE = "age";<o:p></o:p>
protected void initDao() {<o:p></o:p>
//do nothing<o:p></o:p>
}<o:p></o:p>
//保存数据<o:p></o:p>
public void save(Student transientInstance) {<o:p></o:p>
log.debug("saving Student instance");<o:p></o:p>
try {<o:p></o:p>
getHibernateTemplate().save(transientInstance);<o:p></o:p>
log.debug("save successful");<o:p></o:p>
} catch (RuntimeException re) {<o:p></o:p>
log.error("save failed", re);<o:p></o:p>
throw re;<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
//删除对象<o:p></o:p>
public void delete(Student persistentInstance) {<o:p></o:p>
log.debug("deleting Student instance");<o:p></o:p>
try {<o:p></o:p>
getHibernateTemplate().delete(persistentInstance);<o:p></o:p>
log.debug("delete successful");<o:p></o:p>
} catch (RuntimeException re) {<o:p></o:p>
log.error("delete failed", re);<o:p></o:p>
throw re;<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
//按照ID查询对象<o:p></o:p>
public Student findById( java.lang.String id) {<o:p></o:p>
log.debug("getting Student instance with id: " + id);<o:p></o:p>
try {<o:p></o:p>
Student instance = (Student) getHibernateTemplate()<o:p></o:p>
.get("all.Student", id);<o:p></o:p>
return instance;<o:p></o:p>
} catch (RuntimeException re) {<o:p></o:p>
log.error("get failed", re);<o:p></o:p>
throw re;<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
<o:p></o:p>
//按照例子查询对象<o:p></o:p>
public List findByExample(Student instance) {<o:p></o:p>
log.debug("finding Student instance by example");<o:p></o:p>
try {<o:p></o:p>
List results = getHibernateTemplate().findByExample(instance);<o:p></o:p>
log.debug("find by example successful, result size: " + results.size());<o:p></o:p>
return results;<o:p></o:p>
} catch (RuntimeException re) {<o:p></o:p>
log.error("find by example failed", re);<o:p></o:p>
throw re;<o:p></o:p>
}<o:p></o:p>
} <o:p></o:p>
//照一个属性的名字和对应的值查询对象<o:p></o:p>
public List findByProperty(String propertyName, Object value) {<o:p></o:p>
log.debug("finding Student instance with property: " + propertyName<o:p></o:p>
+ ", value: " + value);<o:p></o:p>
try {<o:p></o:p>
String queryString = "from Student as model where model." <o:p></o:p>
+ propertyName + "= ?";<o:p></o:p>
return