Framework Integration----------Hibernate, spring integration

Source: Internet
Author: User

When it comes to the integration framework, in fact, the environment is set up, first we want to guide the package, where we connect the database we use the spring container, we use the connection pool to connect the database, we need to import c3p0 and JDBC Jar package, The rest is spring and hibernate jar packages.

After we create a series of packages and classes under SRC, the DAO layer we create a new interface, building an interface implementation class, the same service layer is a new interface and implementation class, and then write a test of the main method, of course, the entity class must not be less

Next we create a new spring XML (App.xml), in which we get sessionfactory and load the configuration file and mapping relationship

Next we'll configure the Bean in XML.

1 First is the DAO layer

  2 second is service layer

  3. Transaction Manager and Sessionfactory Association

 4. Transaction notification

 5 Defining Pointcuts

Now the basic XML configuration is complete.

The framework has been set up here, and the next thing to note in the main method is the following.

Here is the Main method code

 Packagecom.hanqi.test;ImportJava.math.BigDecimal;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.hanqi.entity.AccountBank;ImportCom.hanqi.service.AccountBankServiceInterface; Public classTestmain { Public Static voidMain (string[] args) {//TODO Auto-generated method stubsApplicationContext AC=NewClasspathxmlapplicationcontext ("App.xml") ; //Service Layer Interface instantiationAccountbankserviceinterface ABS = (accountbankserviceinterface) ac.getbean ("Kahao1") ; System.out.println (Abs.getbankcard ("123123")); Accountbank AB=NewAccountbank ("123", "Liusi", "9999",NewBigDecimal (777), "333") ;             Abs.addcard (AB); }}

Above, we see the call of the spring container, which is the following line of code

Service Layer Interface Instantiation        accountbankserviceinterface ABS = (accountbankserviceinterface) ac.getbean ("Kahao1");

Here is the instantiation of the interface rather than the instantiation of the implementation class, which you should pay special attention to, otherwise the following exception will occur

Exception in thread "main" java.lang.ClassCastException:com.sun.proxy. $Proxy 9 cannot is cast to Com.hanqi.service.impl.AccountBankService
At Com.hanqi.test.TestMain.main (testmain.java:24)

The next time you encounter such an exception will be resolved,

Then put all the code for reference.

DAO interface

 Package Com.hanqi.dao; Import Com.hanqi.entity.AccountBank;  Public Interface accountbankdaointerface {    accountbank getcard (String Kahao);         void Insert (Accountbank ab);         void Update (Accountbank ab);         void Delete (Accountbank ab);}

DAO Implementation Class

 PackageCom.hanqi.impl;Importorg.hibernate.SessionFactory;ImportCom.hanqi.dao.AccountBankDAOInterface;ImportCom.hanqi.entity.AccountBank; Public classAccountbankdaoImplementsAccountbankdaointerface {//Inject Sessionfactory    Privatesessionfactory sessionfactory;  Public voidsetsessionfactory (sessionfactory sessionfactory) { This. Sessionfactory =sessionfactory; } @Override PublicAccountbank Getcard (String kahao) {Accountbank ab=(Accountbank) sessionfactory. Getcurrentsession ()//get the current session. Get (Accountbank.class, Kahao); returnAB; } @Override Public voidInsert (Accountbank ab) {sessionfactory.getcurrentsession (). Save (AB); } @Override Public voidUpdate (Accountbank ab) {sessionfactory.getcurrentsession (). Update (AB); } @Override Public voidDelete (Accountbank ab) {sessionfactory.getcurrentsession (). Delete (AB); }}

Service Interface

 Package Com.hanqi.service; Import Com.hanqi.entity.AccountBank;  Public Interface accountbankserviceinterface {    accountbank getbankcard (String Kahao);         void Addcard (Accountbank ab);         void Updatecard (Accountbank ab);         void Deletecard (Accountbank ab);}

Service Implementation Class

 PackageCom.hanqi.service.impl;ImportCom.hanqi.entity.AccountBank;ImportCom.hanqi.impl.AccountBankDAO;ImportCom.hanqi.service.AccountBankServiceInterface; Public classAccountbankserviceImplementsAccountbankserviceinterface {//Inject DAO    PrivateAccountbankdao Accountbankdao;  Public voidsetaccountbankserviceinterface (Accountbankdao Accountbankdao) { This. Accountbankdao =Accountbankdao; } @Override PublicAccountbank Getbankcard (String Kahao) {returnAccountbankdao.getcard (Kahao); } @Override Public voidAddcard (Accountbank ab) {Accountbankdao.insert (AB); } @Override Public voidUpdatecard (Accountbank ab) {accountbankdao.update (AB); } @Override Public voidDeletecard (Accountbank ab) {accountbankdao.delete (AB); }}

Db.properties file

jdbc.driver_class=oracle.jdbc.driver.OracleDriverjdbc.url=jdbc:oracle:thin: @localhost: 1521: O Rcljdbc.username=test0816jdbc.password=934617699jdbc.initpoolsize=1jdbc.maxpoolsize =10

XML Configuration Code

<?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:p= "http://www.springframework.org/schema/p"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:jdbc= "Http://www.springframework.org/schema/jdbc"xsi:schemalocation= "Http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd http ://Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-4.2.xsdhttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.2.xsd"><!--!--Load external profiles--><context:property-placeholder location= "classpath based on the C3P0 connection pool's data source--><: Db.properties "/><!--connection pool--><bean id=" DataSource "class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > <property name= "driverclass" value= "${jdbc.driver_class}" > </property> <property name= "Jdbcurl" value= "${jdbc.url}" ></property> <property name= "us Er "value=" ${jdbc.username} "></property> <property name=" password "value=" ${jdbc.password} "></pr operty> <property name= "initialpoolsize" value= "${jdbc.initpoolsize}" ></property> <propert Y name= "maxpoolsize" value= "${jdbc.maxpoolsize}" ></property></bean><!--Hibernate Sessionfactory--><bean id= "Sessionfactory"class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean" > <property name= "dataSource" ref= "DataSource" ></property> <!--load Hibernate configuration file--<property name= "configlocation" value= "Classpat H:hibernate.cfg.xml "></property> <!--mapping file, you can use * as a wildcard--<property name=" Mappinglocations "va Lue= "Classpath:com/hanqi/entity/*.hbm.xml" ></property></bean><!--Bean--><!--DAO-- <bean id= "Accountbankdao"class= "Com.hanqi.impl.AccountBankDAO"p:sessionfactory-ref= "Sessionfactory" ></bean><!--Service--><bean id= "Kahao1"class= "Com.hanqi.service.impl.AccountBankService"P:accountbankserviceinterface-ref= "Accountbankdao" ></bean><!--declarative Transaction--><!--1. Transaction manager and Sessionfactory Association--><bean id= " TransactionManager "class= "Org.springframework.orm.hibernate4.HibernateTransactionManager" > <property name= "sessionfactory" ref= " Sessionfactory "></property></bean><!--business Notice--><tx:advice id=" adv "transaction-manager="                TransactionManager "> <tx:attributes> <tx:method name=" * "/><!--How to use transactions-- <tx:method name= "Get" read-only= "true"/> <!--get start read-only method, do not use transaction--</tx:attributes>&lt ;/tx:advice><!--transaction pointcut--><aop:config> <aop:advisor advice-ref= "adv" pointcut= "Execution (* Com.han Qi.service.*.* (..)) " /> <!--interface--></aop:config></beans>

Framework Integration----------Hibernate, spring integration

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.