(1) 新建项目如Ch12Demo,在src文件夹下新建com.chpt12.model包,在此包中新建持久化对象类News和映射文件News.hbm.xml。
package com.chpt12.model;
public class News {
private int id;
private String title;
private String content;
public News(){}
public News(String title, String content) {
super();
this.title = title;
this.content = content;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return (this.id);
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return (this.title);
}
public void setContent(String content) {
this.content = content;
}
public String getContent() {
return (this.content);
}
}
映射文件News.hbm.xml如下:
xml version="1.0" encoding="UTF-8"?>
DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.chpt12.model.News" table="news_table">
<id name="id" type="int" column="news_id">
<generator class="identity"/>
id>
<property name="title" type="string"/>
<property name="content"/>
class>
hibernate-mapping>
(2) 在src文件夹下新建com.chpt12.dao包,在此包中新建NewsDao接口:
package com.chpt12.dao;
import java.util.List;
import com.chpt12.model.News;
public interface NewsDao {
public abstract News get(Integer id);
public abstract Integer save(News news);
public abstract void update(News news);
public abstract void delete(Integer id);
public abstract void delete(News news);
public abstract List
public abstract List findAllNews();
}
(3) 在src文件夹下新建com.chpt12.dao.impl包,在此包中新建NewsDaoImpl类:
package com.chpt12.dao.impl;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.chpt12.dao.NewsDao;
import com.chpt12.model.News;
public class NewsDaoImpl extends HibernateDaoSupport implements NewsDao {
public News get(Integer id) {
return (News) getHibernateTemplate().get(News.class, id);
}
public Integer save(News news) {
return (Integer) getHibernateTemplate().save(news);
}
public void update(News news) {
getHibernateTemplate().update(news);
}
public void delete(Integer id) {
getHibernateTemplate().delete(get(id));
}
public void delete(News news) {
getHibernateTemplate().delete(news);
}
public List
return (List
"from News n where n.title like ?", title);
}
public List findAllNews() {
return (List
}
}
(4) 在src文件夹下新建applicationContext.xml配置文件:
xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost/javaee"/>
<property name="user" value="root"/>
<property name="password" value="123"/>
<property name="maxPoolSize" value="40"/>
<property name="minPoolSize" value="1"/>
<property name="initialPoolSize" value="1"/>
<property name="maxIdleTime" value="20"/>
bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/chpt12/model/News.hbm.xmlvalue>
list>
property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>
<prop key="hibernate.hbm2ddl.auto">updateprop>
<prop key="javax.persistence.validation.mode">noneprop>
props>
property>
bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
bean>
<bean id="newsDao" class="com.chpt12.dao.impl.NewsDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
bean>
beans>
5)在src文件夹下新建test包,在test包中新建HibernateTest类:
package com.chpt7.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.chpt12.dao.NewsDao;
import com.chpt12.model.News;
public class HibernateTest
{
public static void main(String[] args)throws Exception
{
ApplicationContext ctx =
new ClassPathXmlApplicationContext("applicationContext.xml");
NewsDao ndao = (NewsDao)ctx.getBean("newsDao");
List
for (News n:list)
{
System.out.println(n.getContent());
}
}
}
(6)运行测试类HibernateTest.java,运行结果如下: