"Java EE Spring" 27, BA Sports Network-integrated HIBERNATE4+SPRING4 (2)

Source: Internet
Author: User

BA-BA Sports Network-integrated HIBERNATE4+SPRING4 (2)

1. Project diagram

2, first we introduce the corresponding jar package


it's used here. Oracle 11g , so we used a database connection Jar Package is Ojdbc6,

actually Ojdbc5 and the 6 The difference is the support of the data version of the problem, as long as you install the corresponding database, the corresponding version of the corresponding database Jar package, not Baidu Cliff has!!!

3. We configure the corresponding entity objects in the database

Producttype.java

/** * Features: This is the product category * File: Producttype.java * Time: May 12, 2015 10:16:21 * cutter_point */package com.cutter_point.bean.product; publicclassproducttype{    private Integer typeid;       Public Integer GetTypeId ()    {        returntypeid;    }     Publicvoid Settypeid (Integertypeid)    {        This.typeid = typeid;    }}


Product.hbm.xml

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping public        "-//hibernate/hibernate mapping DTD 3.0//en"        "Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd "> 


4, we configure the spring configuration file Beans.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" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi: Schemalocation= "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/ Spring-beans-4.1.xsd Http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context /spring-context-4.1.xsd http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/ Spring-aop-4.1.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/ Spring-tx-4.1.xsd "> <!--scan classes with spring special mechanism, which is to add all the classes under these packages to spring for management--<context:component-scan BAS E-package= "Com.cutter_point"/> <!--Property Walker--<!--<context:property-placeholderlocation= "Classpath:jdbc.properties"/>-<!--Connection Database property configuration, destroy-method= "Close" This means that the bean's default Close method can be called when the bean is destroyed--<bean id= "myDataSource" class= " Org.apache.commons.dbcp2.BasicDataSource "destroy-method=" Close "> <property name=" driverclassname "value="        Oracle.jdbc.driver.OracleDriver "/> <property name=" url "value=" Jdbc:oracle:thin: @localhost: 1522:orcl "/> <property name= "username" value= "account"/> <property name= "password" value= "password"/> <!--initial connection pool startup From--<property name= "InitialSize" value= "1"/> <!--connection pool Max Dbcp2 It doesn't seem to have--<!--& Lt;property name= "Maxactive" value= "$"/>-<!--maximum idle value. After a peak time, the connection pool can slowly release a portion of the connection that has not been used, and has been reduced to maxidle --<property name= "Maxidle" value= "2"/> <!--minimum idle value. When the number of idle connections is less than the threshold, the connection pool will pre-apply for some connections to avoid the flood peak when the application is too late-- > <property name= "minidle" value= "1"/> </bean> <!--HibErnate level Two cache configuration--<bean id= "Sessionfactory" class= "        Org.springframework.orm.hibernate4.LocalSessionFactoryBean "> <!--configuration elided for brevity- <property name= "DataSource" ref= "myDataSource"/> <property name= "mappingresources" > <lis            t> <!--map Files--<value>com/cutter_point/bean/product/Product.hbm.xml</value>  </list> </property> <property name= "Hibernateproperties" > <!--configuration of the properties used to configure Hibernate -<value> Org.hibernate.dialect.OracleDialect hibernate.hbm2ddl.auto= Update <!--other values Create, Create-drop, update, validate none--> hibernate.show_sql=true hi                 Bernate.format_sql=true <!--turn on level Two caching function-hibernate.cache.use_second_level_cache= true Hibernate.cache.use_query_cache= false Hibernate.cache.Region.factory_class= Org.hibernate.cache.ehcache.EhCacheRegionFactory <!--hibernate3 Two-level cache configuration- <!--<propertyname= "Hibernate.cache.provider_class" >org.hibernate.cache.ehcacheprovider</property >--> </value> </property> </bean> <!--transaction Manager, the bean configured above is injected into this--&G    T <bean id= "Txmanager" class= "Org.springframework.orm.hibernate4.HibernateTransactionManager" > <property na Me= "Sessionfactory" ref= "Sessionfactory"/> </bean> <!--we use annotations to enable this transaction, first we open the transaction--<tx:a Nnotation-driven transaction-manager= "TransactionManager"/> </beans>



5, Next we test the hibernate+spring whether the configuration is successful

/** * Features: This is the unit test for the product category * File: Producttest.java * Time: May 12, 2015 10:27:24 * cutter_point */package junit.test; Import Javax.sql.DataSource; Import Org.hibernate.hibernateexception;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;import Org.junit.beforeclass;import org.junit.Test; Importorg.springframework.context.ApplicationContext; Importorg.springframework.context.support.ClassPathXmlApplicationContext; Importcom.cutter_point.bean.product.ProductType;    public class producttest{@BeforeClass publicstatic void Setupbeforeclass () throws Exception {} @Test   Publicvoid Test () {producttypept = new ProductType ();//new an Object Pt.settypeid (78);    Set ID Number configurationcfg = new Configuration ();    Get the configuration sessionfactorysf =cfg.configure ("/config/hibernate/hibernate.cfg.xml"). Buildsessionfactory ();        Get Session Factory Sessionsession = Sf.opensession (); Session.begintransaction ();       Session.save (PT);        Session.gettransaction (). commit ();        Session.close ();    Sf.close (); } @Test publicvoid testspring () {//test if spring works APPLICATIONCONTEXTCXT = new Classpathxmlappli        Cationcontext ("Config/spring/beans.xml");    Datasourcedatasource = (DataSource) cxt.getbean ("myDataSource"); Take out an object System.out.println (DataSource); Determine if it is empty,}}


6. Summary

This spring is configured with a data source for Org.apache.commons.dbcp2.BasicDataSource

This jar package is, of course, you can use the other, I'm just an example, if you quote a mysterious version5.1 error, please change a data source jar package, such as Commons-dbcp.jar

This one will change the org.apache.commons.dbcp2.BasicDataSource to Org.apache.commons.dbcp.BasicDataSource.

This is actually the version of the problem, other places should not change!!!!

"Java EE Spring" 27, BA Sports Network-integrated HIBERNATE4+SPRING4 (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.