Hibernate的描述文件可以是一个properties属性文件,也可以是一个xml文件。下面讲一下Hibernate.cfg.xml的配置。配置格式如下:
1. 配置数据源
在Hibernate.cfg.xml中既可以配置JDBC,也可以配置JNDI。在本小节中讲述数据源如何配置,hibernate.cfg.xml
上例的驱动类使用的是jtds的驱动类,因此读者需要将jtds的jar包(eg. jtds-1.2.jar)加入到classpath中。
7. Oracle连接配置
本小节讲述一下Sql Server数据库的hibernate连接设置,在此只给出连接部分的内容,其余部分与2.2.1.5一样,在此不再赘述。内容如下:
SSH中Hibernate的使用
Spring中的配置:
applicationContext.xml:
------连接数据库配置:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/mldn"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!-- 使spring自动管理关闭hibernate-->
<prop key="hibernate.connection.release_mode">auto</prop>
<!-- 表示允许自动提交 -->
<prop key="hibernate.connection.autocommit">true</prop>
<!-- 显示sql语句 -->
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>zhinangtuan/vo/User.hbm.xml</value>
<value>zhinangtuan/vo/Admin.hbm.xml</value></list>
</property></bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
-------Hibernate使用时的配置
-DAO接口:
<bean id="iadmindao" class="zhinangtuan.dao.IAdminDAO" abstract="true"></bean>
-DAO实现(Ipml)和注入:
<bean id="iadmindaoimpl" class="zhinangtuan.dao.impl.IAdminDAOImpl" parent="iadmindao">
<property name="hibernateTemplate">
<ref
bean="hibernateTemplate"/>
</property>
</bean>
-Action中的注入:
<bean name="/adminsave" class="zhinangtuan.struts.action.AdminAction">
<property name="iadmindao">
<ref bean="iadmindaoimpl"/>
</property>
</bean>
-------------
各个类
1.DAO:
public interface IAdminDAO {
public void save(Admin admin);
}
2.DAOImpl:
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class IAdminDAOImpl extends HibernateDaoSupport implements IAdminDAO{
public void save(Admin admin)
{
System.out.println("准备保存");
this.getSession().save(admin);
}
}
3.Action:
public class AdminAction extends DispatchAction {
private IAdminDAO iadmindao;
...
iadmindao.save(admin);
...
public IAdminDAO getIadmindao() {
return iadmindao;
}
public void setIadmindao(IAdminDAO iadmindao) {
this.iadmindao = iadmindao;
}
SSH中Hibernate的使用
(2012-12-17 11:25:17)
Spring中的配置:
applicationContext.xml:
------连接数据库配置:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/mldn"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!-- 使spring自动管理关闭hibernate-->
<prop key="hibernate.connection.release_mode">auto</prop>
<!-- 表示允许自动提交 -->
<prop key="hibernate.connection.autocommit">true</prop>
<!-- 显示sql语句 -->
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>zhinangtuan/vo/User.hbm.xml</value>
<value>zhinangtuan/vo/Admin.hbm.xml</value></list>
</property></bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
-------Hibernate使用时的配置
-DAO接口:
<bean id="iadmindao" class="zhinangtuan.dao.IAdminDAO" abstract="true"></bean>
-DAO实现(Ipml)和注入:
<bean id="iadmindaoimpl" class="zhinangtuan.dao.impl.IAdminDAOImpl" parent="iadmindao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
-Action中的注入:
<bean name="/adminsave" class="zhinangtuan.struts.action.AdminAction">
<property name="iadmindao">
<ref bean="iadmindaoimpl"/>
</property>
</bean>
-------------
各个类
1.DAO:
public interface IAdminDAO {
public void save(Admin admin);
}
2.DAOImpl:
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class IAdminDAOImpl extends HibernateDaoSupport implements IAdminDAO{
public void save(Admin admin)
{
System.out.println("准备保存");
this.getSession().save(admin);
}
}
3.Action:
public class AdminAction extends DispatchAction {
private IAdminDAO iadmindao;
...
iadmindao.save(admin);
...
public IAdminDAO getIadmindao() {
return iadmindao;
}
public void setIadmindao(IAdminDAO iadmindao) {
this.iadmindao = iadmindao;
}
SSH中Hibernate的使用
(2012-12-17 11:25:17)
Spring中的配置:
applicationContext.xml:
------连接数据库配置:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/mldn"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!-- 使spring自动管理关闭hibernate-->
<prop key="hibernate.connection.release_mode">auto</prop>
<!-- 表示允许自动提交 -->
<prop key="hibernate.connection.autocommit">true</prop>
<!-- 显示sql语句 -->
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>zhinangtuan/vo/User.hbm.xml</value>
<value>zhinangtuan/vo/Admin.hbm.xml</value></list>
</property></bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
-------Hibernate使用时的配置
-DAO接口:
<bean id="iadmindao" class="zhinangtuan.dao.IAdminDAO" abstract="true"></bean>
-DAO实现(Ipml)和注入:
<bean id="iadmindaoimpl" class="zhinangtuan.dao.impl.IAdminDAOImpl" parent="iadmindao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
-Action中的注入:
<bean name="/adminsave" class="zhinangtuan.struts.action.AdminAction">
<property name="iadmindao">
<ref bean="iadmindaoimpl"/>
</property>
</bean>
-------------
各个类
1.DAO:
public interface IAdminDAO {
public void save(Admin admin);
}
2.DAOImpl:
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class IAdminDAOImpl extends HibernateDaoSupport implements IAdminDAO{
public void save(Admin admin)
{
System.out.println("准备保存");
this.getSession().save(admin);
}
}
3.Action:
public class AdminAction extends DispatchAction {
private IAdminDAO iadmindao;
...
iadmindao.save(admin);
...
public IAdminDAO getIadmindao() {
return iadmindao;
}
public void setIadmindao(IAdminDAO iadmindao) {
this.iadmindao = iadmindao;
}
SSH中Hibernate的使用
(2012-12-17 11:25:17)
Spring中的配置:
applicationContext.xml:
------连接数据库配置:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/mldn"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!-- 使spring自动管理关闭hibernate-->
<prop key="hibernate.connection.release_mode">auto</prop>
<!-- 表示允许自动提交 -->
<prop key="hibernate.connection.autocommit">true</prop>
<!-- 显示sql语句 -->
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>zhinangtuan/vo/User.hbm.xml</value>
<value>zhinangtuan/vo/Admin.hbm.xml</value></list>
</property></bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
-------Hibernate使用时的配置
-DAO接口:
<bean id="iadmindao" class="zhinangtuan.dao.IAdminDAO" abstract="true"></bean>
-DAO实现(Ipml)和注入:
<bean id="iadmindaoimpl" class="zhinangtuan.dao.impl.IAdminDAOImpl" parent="iadmindao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
-Action中的注入:
<bean name="/adminsave" class="zhinangtuan.struts.action.AdminAction">
<property name="iadmindao">
<ref bean="iadmindaoimpl"/>
</property>
</bean>
-------------
各个类
1.DAO:
public interface IAdminDAO {
public void save(Admin admin);
}
2.DAOImpl:
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class IAdminDAOImpl extends HibernateDaoSupport implements IAdminDAO{
public void save(Admin admin)
{
System.out.println("准备保存");
this.getSession().save(admin);
}
}
3.Action:
public class AdminAction extends DispatchAction {
private IAdminDAO iadmindao;
...
iadmindao.save(admin);
...
public IAdminDAO getIadmindao() {
return iadmindao;
}
public void setIadmindao(IAdminDAO iadmindao) {
this.iadmindao = iadmindao;
}
SSH中Hibernate的使用
(2012-12-17 11:25:17)
Spring中的配置:
applicationContext.xml:
------连接数据库配置:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/mldn"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!-- 使spring自动管理关闭hibernate-->
<prop key="hibernate.connection.release_mode">auto</prop>
<!-- 表示允许自动提交 -->
<prop key="hibernate.connection.autocommit">true</prop>
<!-- 显示sql语句 -->
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>zhinangtuan/vo/User.hbm.xml</value>
<value>zhinangtuan/vo/Admin.hbm.xml</value></list>
</property></bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
-------Hibernate使用时的配置
-DAO接口:
<bean id="iadmindao" class="zhinangtuan.dao.IAdminDAO" abstract="true"></bean>
-DAO实现(Ipml)和注入:
<bean id="iadmindaoimpl" class="zhinangtuan.dao.impl.IAdminDAOImpl" parent="iadmindao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
-Action中的注入:
<bean name="/adminsave" class="zhinangtuan.struts.action.AdminAction">
<property name="iadmindao">
<ref bean="iadmindaoimpl"/>
</property>
</bean>
-------------
各个类
1.DAO:
public interface IAdminDAO {
public void save(Admin admin);
}
2.DAOImpl:
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class IAdminDAOImpl extends HibernateDaoSupport implements IAdminDAO{
public void save(Admin admin)
{
System.out.println("准备保存");
this.getSession().save(admin);
}
}
3.Action:
public class AdminAction extends DispatchAction {
private IAdminDAO iadmindao;
...
iadmindao.save(admin);
...
public IAdminDAO getIadmindao() {
return iadmindao;
}
public void setIadmindao(IAdminDAO iadmindao) {
this.iadmindao = iadmindao;
}
SSH中Hibernate的使用
(2012-12-17 11:25:17)
Spring中的配置:
applicationContext.xml:
------连接数据库配置:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/mldn"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!-- 使spring自动管理关闭hibernate-->
<prop key="hibernate.connection.release_mode">auto</prop>
<!-- 表示允许自动提交 -->
<prop key="hibernate.connection.autocommit">true</prop>
<!-- 显示sql语句 -->
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>zhinangtuan/vo/User.hbm.xml</value>
<value>zhinangtuan/vo/Admin.hbm.xml</value></list>
</property></bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
-------Hibernate使用时的配置
-DAO接口:
<bean id="iadmindao" class="zhinangtuan.dao.IAdminDAO" abstract="true"></bean>
-DAO实现(Ipml)和注入:
<bean id="iadmindaoimpl" class="zhinangtuan.dao.impl.IAdminDAOImpl" parent="iadmindao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
-Action中的注入:
<bean name="/adminsave" class="zhinangtuan.struts.action.AdminAction">
<property name="iadmindao">
<ref bean="iadmindaoimpl"/>
</property>
</bean>
-------------
各个类
1.DAO:
public interface IAdminDAO {
public void save(Admin admin);
}
2.DAOImpl:
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class IAdminDAOImpl extends HibernateDaoSupport implements IAdminDAO{
public void save(Admin admin)
{
System.out.println("准备保存");
this.getSession().save(admin);
}
}
3.Action:
public class AdminAction extends DispatchAction {
private IAdminDAO iadmindao;
...
iadmindao.save(admin);
...
public IAdminDAO getIadmindao() {
return iadmindao;
}
public void setIadmindao(IAdminDAO iadmindao) {
this.iadmindao = iadmindao;
}
SSH中Hibernate的使用
(2012-12-17 11:25:17)
Spring中的配置:
applicationContext.xml:
------连接数据库配置:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/mldn"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!-- 使spring自动管理关闭hibernate-->
<prop key="hibernate.connection.release_mode">auto</prop>
<!-- 表示允许自动提交 -->
<prop key="hibernate.connection.autocommit">true</prop>
<!-- 显示sql语句 -->
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>zhinangtuan/vo/User.hbm.xml</value>
<value>zhinangtuan/vo/Admin.hbm.xml</value></list>
</property></bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
-------Hibernate使用时的配置
-DAO接口:
<bean id="iadmindao" class="zhinangtuan.dao.IAdminDAO" abstract="true"></bean>
-DAO实现(Ipml)和注入:
<bean id="iadmindaoimpl" class="zhinangtuan.dao.impl.IAdminDAOImpl" parent="iadmindao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
-Action中的注入:
<bean name="/adminsave" class="zhinangtuan.struts.action.AdminAction">
<property name="iadmindao">
<ref bean="iadmindaoimpl"/>
</property>
</bean>
-------------
各个类
1.DAO:
public interface IAdminDAO {
public void save(Admin admin);
}
2.DAOImpl:
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class IAdminDAOImpl extends HibernateDaoSupport implements IAdminDAO{
public void save(Admin admin)
{
System.out.println("准备保存");
this.getSession().save(admin);
}
}
3.Action:
public class AdminAction extends DispatchAction {
private IAdminDAO iadmindao;
...
iadmindao.save(admin);
...
public IAdminDAO getIadmindao() {
return iadmindao;
}
public void setIadmindao(IAdminDAO iadmindao) {
this.iadmindao = iadmindao;
}