1. 程式人生 > >使用Spring操作資料庫的步驟

使用Spring操作資料庫的步驟

(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是對映檔案的根元素 -->

<hibernate-mapping>

<!-- 每個class元素對應一個持久化物件

-->

<class name="com.chpt12.model.News" table="news_table">

<!-- id元素定義持久化類的標識屬性 -->

<id name="id" type="int" column="news_id">

<generator class="identity"/>

</id>

<!-- property元素定義普通屬性 -->

<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<News> findByName(String title);

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<News> findByName(String title) {

return (List<News>) getHibernateTemplate().find(

"from News n where n.title like ?", title);

}

public List findAllNews() {

return (List<News>) getHibernateTemplate().find(" from News");

}

}

(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,使用C3P0資料來源實現 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close">

<!-- 指定連線資料庫的驅動 -->

<property name="driverClass" value="com.mysql.jdbc.Driver"/>

<!-- 指定連線資料庫的URL -->

<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>

<!-- 定義HibernateSessionFactory -->

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<!-- 依賴注入資料來源dataSource -->

<property name="dataSource" ref="dataSource"/>

<!-- mappingResouces屬性用來列出全部對映檔案 -->

<property name="mappingResources">

<list>

<!-- 以下用來列出Hibernate對映檔案 -->

<value>com/chpt12/model/News.hbm.xml</value>

</list>

</property>

<!-- 定義HibernateSessionFactory的屬性 -->

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

<prop key="hibernate.hbm2ddl.auto">update</prop>

<prop key="javax.persistence.validation.mode">none</prop>

</props>

</property>

</bean>

<!-- 配置Hibernate事務管理器,該類實現PlatformTransactionManager介面  -->

<bean id="transactionManager" 

        class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

<!-- 定義DAO Bean-->

<bean id="newsDao" class="com.chpt12.dao.impl.NewsDaoImpl">

<!-- 注入持久化操作所需的SessionFactory -->

<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<News> list = ndao.findAllNews();

for (News n:list)

{

System.out.println(n.getContent());

}

}

}

6)執行測試類HibernateTest.java,執行結果如下: