使用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,运行结果如下:

展开阅读全文

没有更多推荐了,返回首页