Integration of Spring Learning notes Hibernate

Source: Internet
Author: User

Spring and hibernate are at different levels, and spring is concerned with the combination of business logic, and Spring provides a strong ability to manage them, and hibernate completes or maps so that developers don't have to care about SQL statements. Deal directly with objects. Spring provides integrated functionality for Hibernate's sessionfactory.

    1. Create a spring Bean.xml file that configures Sessionfactory and transactions within it and does not need to be configured in Hibernate.cfg.xml.

      

<!--Configure Sessionfactory instance of Hibernate: configured with Spring-provided Localsessionfactorybean-
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<!--Configure Data source Properties-
<property name= "DataSource" ref= "DataSource" ></property>
<!--Configure the location and name of hibernate configuration Files--
<!--
<property name= "configlocation" value= "Classpath:hibernate.cfg.xml" ></property>
-
<!--use hibernateproperties zodiac to configure Hibernate native Properties-
<property name= "Hibernateproperties" >
<props>
<prop key= "Hibernate.dialect" >org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key= "Hibernate.show_sql" >true</prop>
<prop key= "Hibernate.format_sql" >true</prop>
<prop key= "Hibernate.hbm2ddl.auto" >update</prop>
</props>
</property>
<!--Configure the location and name of the hibernate mapping file, you can use wildcards--
<property name= "Mappinglocations"
Value= "Classpath:com/atguigu/spring/hibernate/entities/*.hbm.xml" ></property>
</bean>

<!--Configure Spring's declarative transactions--
<!--1. Configure transaction Manager-
<bean id= "TransactionManager" class= "Org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name= "Sessionfactory" ref= "Sessionfactory" ></property>
</bean>

<!--2. Configuring transaction properties requires transaction manager--
<tx:advice id= "Txadvice" transaction-manager= "TransactionManager" >
<tx:attributes>
<tx:method name= "get*" read-only= "true"/>
<tx:method name= "Purchase" propagation= "Requires_new"/>
<tx:method name= "*"/>
</tx:attributes>
</tx:advice>

<!--3. Configure a transaction pointcut and associate the tangency point with the transaction properties-
<aop:config>
<aop:pointcut expression= "Execution (* com.localhost.spring.hibernate.service.*.* (..))"
Id= "Txpointcut"/>
<aop:advisor advice-ref= "Txadvice" pointcut-ref= "Txpointcut"/>
</aop:config>

    2. Create entity classes, and *.hbm.xml files.  

        

public class Account {

Private Integer ID;
Private String username;
private int balance;
Public Integer getId () {
return ID;
}
public void SetId (Integer id) {
This.id = ID;
}
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username = Username;
}
Public Integer GetBalance () {
return balance;
}
public void setbalance (int balance) {
This.balance = balance;
}
}

3. Building DAO interfaces and implementing classes

Public interface Bookshopdao {

public int FINDBOOKPRICEBYISBN (String ISBN);

public void Updatebookstock (String ISBN);

public void Updateuseraccount (String username, int. price);

}

@Repository
public class Bookshopdaoiml implements Bookshopdao {

@Autowired
Private Sessionfactory sessionfactory;

Private Session getsession () {
return Sessionfactory.getcurrentsession ();
}

@Override
public int FINDBOOKPRICEBYISBN (String ISBN) {
String sql= "Select B.price from book B where b.isbn =?";
Query query = getsession (). CreateQuery (SQL). SetString (0, ISBN);
Return (Integer) Query.uniqueresult ();

}

@Override
public void Updatebookstock (String ISBN) {

}

@Override
public void Updateuseraccount (String username, int. price) {
TODO auto-generated Method Stub

}

}

4. Testing Test

Private ApplicationContext Ctx=null;
Private Bookshopdao Bookshopdao;

{
CTX = new Classpathxmlapplicationcontext ("Application.xml");
Bookshopdao=ctx.getbean (Bookshopdao.class);
}



Method of the interface class
@Test
public void Test () {
System.out.println (BOOKSHOPDAO.FINDBOOKPRICEBYISBN ("1001"));
}

Integration of Spring Learning notes Hibernate

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.