Spring Hibernate集成配置文件

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.5.xsd">


<bean id="userDao" class="dao.DaoIMPL">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</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>
<prop key="hibernate.show_sql">true</prop>
<!-- prop key="hibernate.hbm2ddl.auto">create</prop -->
</props>
</property>
<property name="mappingResources">
<list>
<value>po/BookPO.hbm.xml</value>
</list>
</property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>111111</value>
</property>
</bean>
</beans>


[b]Spring C3p0配置[/b]

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/jdbc.properties" />
</bean>

<bean id="oracleDataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass">
<value>${mysqljdbc.driverClassName}</value>
</property>
<property name="jdbcUrl">
<value>${mysqljdbc.url}</value>
</property>
<property name="user">
<value>${mysqljdbc.username}</value>
</property>
<property name="password">
<value>${mysqljdbc.password}</value>
</property>
<property name="minPoolSize">
<value>5</value>
</property>

<!-- 达到最大连接数后可以增加的连接数 个 -->
<property name="acquireIncrement">
<value>2</value>
</property>
<property name="maxPoolSize">
<value>3</value>
</property>
<!-- 最大闲置时间 秒 -->
<property name="maxIdleTime">
<value>600</value>
</property>

<property name="maxStatements">
<value>100</value>
</property>

<!-- 闲置的连接测试周期 秒 -->
<property name="idleConnectionTestPeriod">
<value>1200</value>
</property>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>po/BookPO.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>

<prop key="hibernate.generate_statistics">
${hibernate.generate_statistics}
</prop>
<prop key="hibernate.dialect">
${mysqlhibernate.dialect}
</prop>
<prop key="hibernate.show_sql">
${hibernate.show_sql}
</prop>

</props>
</property>

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

</bean>

<!--事务管理-->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--配置事务传播性-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="query*" read-only="true" />
<tx:method name="*Query" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="save*" rollback-for="Exception" />
<tx:method name="*Save" rollback-for="Exception" />
<tx:method name="update*" rollback-for="Exception" />
<tx:method name="*Update" rollback-for="Exception" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 那些类使用事务 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceMethod" expression="execution(* net..*Service.*(..))" />
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
</aop:config>

<bean id="userDao" class="dao.DaoIMPL">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
</beans>


[b]属性文件配置jdbc.properties[/b]

mysqljdbc.driverClassName=com.mysql.jdbc.Driver
mysqljdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
mysqljdbc.username=root
mysqljdbc.password=111111
hibernate.generate_statistics=true
mysqlhibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true

[b]web.xml配置[/b]

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>tesths</servlet-name>
<servlet-class>action.tesths</servlet-class>
</servlet>

<!-- 普通spring上下文环境 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>

<!-- log4j配置-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>

<!-- 字符集过滤 -->
<filter>
<filter-name>SetCharacterEncodingFilter</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>

<!-- 加载applicationContext.xml的listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet-mapping>
<servlet-name>tesths</servlet-name>
<url-pattern>/tesths</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>



[b]servlet 使用spring[/b]

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println("hibernate,spring测试操作");

String bname=request.getParameter("bname");
String bno=request.getParameter("bno");

if(bname!=null&&bno!=null)
{

ServletContext application;
WebApplicationContext wac;
application = getServletContext();
wac = WebApplicationContextUtils.getWebApplicationContext(application);//获取spring的context

dao daoimpl = (dao)wac.getBean("userDao");

BookPO book= new BookPO();
book.setBookname(bname);
book.setBookno(bno);

daoimpl.addBook(book);

out.println("数据添加成功");
}

out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值