我的spring+hibernate整合方案
来源:互联网 发布:沙恭尼 知乎 编辑:程序博客网 时间:2024/05/22 01:49
1、spring的jar包手动添加,web.xml
加入
<!-- 指定spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<!--加载多个spring配置文件 -->
/WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml
</param-value>
</context-param>
<!-- 定义SPRING监听器,加载spring -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
2、hibernate用myeclipse添加,生成独立的src/hibernate.cfg.xml
这里面配置hibernate,以及*.hbm.xml
在其中设置数据库方言(数据库类型)
3、applicationContext.xml中c3p0配置数据源,然把数据源注入sessionFactory
<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/j2ee" />
<!-- 指定连接数据库的用户名 -->
<property name="user" value="root" /> <!-- 指定连接数据库的密码 -->
<property name="password" value="32147" /> <!-- 指定连接数据库连接池的最大连接数 -->
<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>
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
配置编码方式,用spring编码过滤器 web.xml中加入最上面
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
加入
<!-- 指定spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<!--加载多个spring配置文件 -->
/WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml
</param-value>
</context-param>
<!-- 定义SPRING监听器,加载spring -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
2、hibernate用myeclipse添加,生成独立的src/hibernate.cfg.xml
这里面配置hibernate,以及*.hbm.xml
在其中设置数据库方言(数据库类型)
3、applicationContext.xml中c3p0配置数据源,然把数据源注入sessionFactory
<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/j2ee" />
<!-- 指定连接数据库的用户名 -->
<property name="user" value="root" /> <!-- 指定连接数据库的密码 -->
<property name="password" value="32147" /> <!-- 指定连接数据库连接池的最大连接数 -->
<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>
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
配置编码方式,用spring编码过滤器 web.xml中加入最上面
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
spring注解,
@Repository("userDao") //等价于在applicationContext.xml声明了一个id为user到的bean
public class UserDao extends HibernateDaoSupport{
@Resource(name="sessionFactory")//把spring容器中id为sessionFactory的bean注入到本方法中
public void setMySessionFactory(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
/*只需要在对应的类上加上一个@Component注解,
就将该类定义为一个Bean了。
Spring还提供了更加细化的注解形式:@Repository(DAO层)
@Service(服务层)
@Controller(控制层)
*/
}
- 我的spring+hibernate整合方案
- spring整合hibernate方案
- 我的笔记 Spring+SpringMVC+hibernate整合
- Struts,Spring,Hibernate整合方案之一
- spring+hibernate+struts2整合方案(1)
- spring+hibernate+struts2整合方案(1)
- hibernate+spring的整合
- spring、hibernate的整合
- 我在spring 中整合hibernate 遇到的错误
- spring+hibernate+struts2整合方案(2,整合细则)
- spring+hibernate+struts2整合方案(2,整合细则)
- spring+hibernate+struts2整合方案(2,整合细则)
- Spring与Hibernate的整合
- hibernate和spring的整合
- webwork+spring+hibernate的整合
- struts+hibernate+spring的整合
- hibernate与spring的整合
- spring 和hibernate的整合
- ZOJ 3223 Journey to the Center of the Earth 【二维最短路】
- OS X Mountain Lion 系统配置 Apache+mysql + php
- 关闭页面时不出现询问直接关闭_js代码
- 字符串编辑距离(C递归实现)
- Weblogic 基础
- 我的spring+hibernate整合方案
- Balanced Binary Tree
- 使用kgdb调试linux内核及内核模块
- delphi中的文件名
- C# where用法
- 四川-雅安-芦山-加油-挺住
- SQL中SELECT INTO和INSERT INTO SELECT语句介绍 笔记
- struts 2标签 <s:textfield> 样式问题
- jQuery.validate使用手册