spring + hibernate 配置

http://hi.baidu.com/xogjghsfhjemorr/item/cea598dd22e75e2a39f6f726

 

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="propertyConfigurer" 
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:/deploy.properties</value>
        </property>
    </bean>
    
<!-- 注入数据源 -->
<bean id="dataSource"
   class="org.apache.commons.dbcp.BasicDataSource">
   <property name="url">
    <value>${database.url}</value>
   </property>
   <property name="driverClassName">
    <value>${database.driver}</value>
   </property>
   <property name="username">
    <value>${database.username}</value>
   </property>
   <property name="password">
    <value>${database.password}</value>
   </property>
   <property name="maxWait">
    <value>5000</value>
   </property>
   <property name="maxIdle">
    <value>5</value>
   </property>
   <property name="maxActive">
    <value>50</value>
   </property>
</bean>
<bean id="sessionFactory"
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <!--hibernate数据源配置,如果启用的话,则使用Apache DBCP提供的连接池,而不是下面指定的C3P0,用spring 就要用到-->
   <property name="dataSource">
    <ref local="dataSource" /><!-- 注入数据源 -->
   </property>
   <!-- hibernate 参数设置 -->
   <property name="hibernateProperties">
    <props>
     <!-- 指定Hibernate的连接方言 org.hibernate.dialect.Oracle9Dialect-->
     <prop key="hibernate.dialect">
      org.hibernate.dialect.MySQLDialect
     </prop>
     <prop key="hibernate.connection.driver_class ">
      com.mysql.jdbc.Driver
     </prop>
     <prop key="hibernate.connection.url">
      jdbc:mysql:http://localhost:3306/test
     </prop>
     <prop key="hibernate.connection.username">root</prop>
     <prop key="hibernate.connection.password">fytcm</prop>
     <prop key="hibernate.show_sql">true</prop>
     <prop key="hibernate.c3p0.max_size">5</prop>
     <prop key="hibernate.c3p0.min_size">5</prop>
     <prop key="hibernate.c3p0.timeout">5000</prop>
     <prop key="hibernate.c3p0.max_statements">100</prop>
     <prop key="hibernate.c3p0.idle_test_period">3000</prop>
     <prop key="hibernate.c3p0.acquire_increment">2</prop>
     <prop key="hibernate.c3p0.validate">false</prop>
     <prop key="hibernate.cglib.use_reflection_optimizer">
      false
     </prop>
     <prop key="hibernate.jdbc.fetch_size">50</prop>
     <prop key="hibernate.jdbc.batch_size">30</prop>
     <prop key="hibernate.use_outer_join">false</prop>
    </props>
   </property>
   <!--hibernate O/R mapping文件位置-->
   <property name="mappingResources">
    <list>
     <value>config/hbm/Team.hbm.xml</value>
    </list>
   </property>
   <!-- 集体写入,放在文件夹中
    <property name="mappingDirectoryLocations">
    <list>
    <value>classpath:/config/hbm</value>
    </list>
    </property>        
   -->
</bean>
<!--Hibernate模板定义-->
<bean id="hibernateTemplate"
   class="org.springframework.orm.hibernate3.HibernateTemplate">
   <property name="sessionFactory">
    <ref bean="sessionFactory" />
   </property>
</bean>
<!-- 持久层操作 -->
<bean id="teamDao" class="com.zd.dao.impl.TeamDao">
   <property name="hibernateTemplate">
    <ref local="hibernateTemplate" />
   </property>
</bean>

</beans>
==============================================================
src/deploy.properties
database.url =jdbc:mysql://localhost:3306/test
database.driver =com.mysql.jdbc.Driver
database.username =root
database.password =fytcm
======================================================
web.xml
<?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">
<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<!-- 
<listener>
   <listener-class>
    org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener> -->
<!-- OR USE THE CONTEXTLOADERSERVLET INSTEAD OF THE LISTENER -->
   <servlet>
   <servlet-name>context</servlet-name>
   <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
   </servlet>
  
<servlet>
    <servlet-name>teamAction</servlet-name>
    <servlet-class>com.zd.action.teamAction</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>teamAction</servlet-name>
    <url-pattern>/servlet/teamAction</url-pattern>
</servlet-mapping>
<welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
=================================================
teamAction
package com.zd.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.zd.bean.Team;
import com.zd.dao.ITeamDao;
public class teamAction extends HttpServlet {
/**
* Constructor of the object.
*/
public teamAction() {
   super();
}

private WebApplicationContext ctx;
public WebApplicationContext getCtx() {
   return ctx;
}

public ITeamDao teamDao;
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
   super.destroy(); // Just puts "destroy" string in log
   // Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
* 
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
   ServletContext servletContext = getServletContext();
     this.ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
  
   teamDao = (ITeamDao) this.getCtx().getBean("teamDao");
   Team team = new Team();
   team.setName("Java Team");  
   teamDao.saveTeam(team);
   response.setContentType("text/html");
   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(" </BODY>");
   out.println("</HTML>");
   out.flush();
   out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
* 
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
   doGet(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
   // Put your code here
}
}
===============================
package com.zd.dao;
import com.zd.bean.Team;
public interface ITeamDao {

void saveTeam(Team team);

Team getTeam(Integer id);

void updateTeam(Team team);

void deleteTeam(Integer id);
}
=======================================
package com.zd.dao.impl;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.zd.bean.Team;
import com.zd.dao.ITeamDao;
public class TeamDao extends HibernateDaoSupport implements ITeamDao{
public void deleteTeam(Integer id) {
   // TODO Auto-generated method stub
  
}
public Team getTeam(Integer id) {
   return (Team) this.getHibernateTemplate().get(Team.class, id);

}
public void saveTeam(Team team) {
   getHibernateTemplate().save(team);
  
}
public void updateTeam(Team team) {
   getHibernateTemplate().update(team);  
}

public static void main(String[] args) {
//   ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//   SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");
//   HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);
//   List list = hibernateTemplate.find("from Team where id=1");
//   Team t = (Team) list.get(0);
//   System.out.println(t.getName());
  
}

}

 

转载于:https://www.cnblogs.com/Null2051/archive/2012/10/06/2712724.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值