Spring结合Hibernate
一、建立项目
(1)建立数据库
DROP DATABASE mldn_hibernate;
CREATE DATABASE mldn_hibernate;
USE mldn_hibernate;
DROP TABLE news ;
CREATE TABLE news (
id int primary key auto_increment ,
title VARCHAR(50) not null,
post_date datetime not null,
abstractnote VARCHAR(200) not null,
content text not null,
imgurl VARCHAR(50)
);
(2)项目:SpringHibernateDemo01
SSH(Spring <wbr>+ <wbr>Struts <wbr>+ <wbr>Hibernate)鈥斺 <wbr>上
二、加入Spring和Hibernate的支持
注意:因为现在是需要借助Spring来管理或者组合其他框架,所以需要先加入Spring的支持。
1、加入Spring的支持
(1)项目—右键—MyEclipse—Add Spring Capabilities…
SSH(Spring <wbr>+ <wbr>Struts <wbr>+ <wbr>Hibernate)鈥斺 <wbr>上
(2)加入Spring核心jar包以及和Hibernate结合需要的jar包
并且注意加入jar包的方式,还是选择放到lib目录下
SSH(Spring <wbr>+ <wbr>Struts <wbr>+ <wbr>Hibernate)鈥斺 <wbr>上
(3)添加Spring的管理配置文件
一般采用默认名称applicationContext.xml
SSH(Spring <wbr>+ <wbr>Struts <wbr>+ <wbr>Hibernate)鈥斺 <wbr>上
(4)Finish完成
2、加入Hibernate的支持
(1)项目—右键—MyEclipse—Add Hibernate Capabilities…
SSH(Spring <wbr>+ <wbr>Struts <wbr>+ <wbr>Hibernate)鈥斺 <wbr>上
(2)加入Hibernate的支持jar包
注意:比之前没有使用Spring的时候多了一个jar包
SSH(Spring <wbr>+ <wbr>Struts <wbr>+ <wbr>Hibernate)鈥斺 <wbr>上
(3)选择Hibernate的配置文件
因为此处使用了Spring,所以,选择在已有的Spring的配置文件中进行Hibernate的配置
SSH(Spring <wbr>+ <wbr>Struts <wbr>+ <wbr>Hibernate)鈥斺 <wbr>上
(4)选择Spring的核心配置文件及填写SessionFactory的ID
选择是重新创建一个Spring的核心配置文件还是使用已经存在的,这里使用已经存在的即可
并且填写SessionFactory的ID,即定义SessionFactory的bean标签的id值
SSH(Spring <wbr>+ <wbr>Struts <wbr>+ <wbr>Hibernate)鈥斺 <wbr>上
(5)配置数据库连接及数据源的bean的id
SSH(Spring <wbr>+ <wbr>Struts <wbr>+ <wbr>Hibernate)鈥斺 <wbr>上
(6)是否由Hibernate自动创建SessionFactory类
取消选择
SSH(Spring <wbr>+ <wbr>Struts <wbr>+ <wbr>Hibernate)鈥斺 <wbr>上
(7)对于重复添加的jar的处理方式
选择保留现有的或者替换都可以
SSH(Spring <wbr>+ <wbr>Struts <wbr>+ <wbr>Hibernate)鈥斺 <wbr>上
3、查看加入Hibernate支持后Spring的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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/mldn_hibernate">
</property>
<property name="username" value="root"></property>
<property name="password" value="mysqladmin"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"></ref>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>
</beans>
4、选择数据源连接方式
(1)现在默认的是使用公用的数据源连接池的连接方式
使用该中方式,需要再加入commons-pool.jar ,否则会报找不到类的错误。
在Spring开发包的spring-framework-2.0-m3\lib\jakarta-commons中可以找到该jar包。
(2)使用c3p0方式进行数据源处理
需要修改Spring配置文件如下:
<bean id="dataSource"
class="com.mchange.v2.c3p0.DriverManagerDataSource">
<property name="driverClass"
value="org.gjt.mm.mysql.Driver">
</property>
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/testdb">
</property>
<property name="user" value="root"></property>
<property name="password" value="mysqladmin"></property>
</bean>
需要注意:当实现类继承HibernateDaoSupport,并通过Spring注入了SessionFactory,使用该方法对于c3p0的连接池,可以直接通过getSession 进行处理,但如果使用common-pool的连接池则还需要手工关闭连接
5、加入Hibernate的show_sql属性配置
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"></ref>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
</bean>
6、生成数据映射,建立pojo类
生成映射没有变化
SSH(Spring <wbr>+ <wbr>Struts <wbr>+ <wbr>Hibernate)鈥斺 <wbr>上
SSH(Spring <wbr>+ <wbr>Struts <wbr>+ <wbr>Hibernate)鈥斺 <wbr>上
SSH(Spring <wbr>+ <wbr>Struts <wbr>+ <wbr>Hibernate)鈥斺 <wbr>上
SSH(Spring <wbr>+ <wbr>Struts <wbr>+ <wbr>Hibernate)鈥斺 <wbr>上
SSH(Spring <wbr>+ <wbr>Struts <wbr>+ <wbr>Hibernate)鈥斺 <wbr>上
7、编写dao
package mldn.lin.dao;
import java.util.List;
import mldn.lin.pojo.News;
public interface NewsDAO {
public boolean doInsert(News news) throws Exception;
public boolean doUpdate(News news) throws Exception;
public boolean doRemove(int id) throws Exception;
public List<News> findAll() throws Exception;
public List<News> findAll(int cp,int ls,String keyword) throws Exception;
public int getAllCount() throws Exception;
public News findById(int id) throws Exception;
}
SSH(Spring + Struts + Hibernate
猜你喜欢
转载自dapeng.iteye.com/blog/1612319
今日推荐
周排行