Learning Spring from cainiao-60 s learning Spring and Hibernate integration, 60 shibernate

Source: Internet
Author: User

Learning Spring from cainiao-60 s learning Spring and Hibernate integration, 60 shibernate

1. Overview.
The integration of Spring and Hibernate is a common practice in enterprise applications. The combination of Spring and Hibernate improves the flexibility and development efficiency of our code, next I will explain how Spring is integrated with Hibernate step by step.
Ii. Code demonstration.
Import the Hibernate jar package
Hibernate-3.2/lib/*. jar
Hibernate-3.2/hibernate3.jar
Jar packages for importing Spring
The database I use is MySql, so I want to import the MySql driver jar package:
Mysql-connector-java-3.1.13-bin.jar

Directory structure:


Hibernate object ing:

Log. java

package com.tgb.usermgr.domain;import java.util.Date;public class Log {private int id;private String type;private String detail;private Date time;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getType() {return type;}public void setType(String type) {this.type = type;}public String getDetail() {return detail;}public void setDetail(String detail) {this.detail = detail;}public Date getTime() {return time;}public void setTime(Date time) {this.time = time;}}


User. java

package com.tgb.usermgr.domain;public class User {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}


Log. hbm. xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
   

User. hbm. xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
   

Hibernate Tool

HibernateUtils. java

package com.tgb.usermgr.util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtils {private static SessionFactory factory;private HibernateUtils() {}static {try {Configuration cfg = new Configuration().configure();factory = cfg.buildSessionFactory();}catch(Exception e) {e.printStackTrace();throw new java.lang.RuntimeException(e);}}public static SessionFactory getSessionFactory() {return factory;}public static Session getSession() {return factory.openSession();}public static void closeSession(Session session) {if (session != null) {if (session.isOpen()) {session.close();}}}}


Business Logic implementation and interfaces:

LogManager. java

package com.tgb.usermgr.manager;import com.tgb.usermgr.domain.Log;public interface LogManager {public void addLog(Log log);}


LogManagerImpl. java

package com.tgb.usermgr.manager;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.tgb.usermgr.domain.Log;import com.tgb.usermgr.util.HibernateUtils;public class LogManagerImpl extends HibernateDaoSupport implements LogManager {public void addLog(Log log) {//getSession().save(log);getHibernateTemplate().save(log);}}


UserManager. java

package com.tgb.usermgr.manager;import com.tgb.usermgr.domain.User;public interface UserManager {public void addUser(User user) throws Exception;}


UserManagerImpl. java

Package com. tgb. usermgr. manager; import java. util. date; import org. hibernate. session; import org. springframework. orm. hibernate3.support. hibernateDaoSupport; import com. tgb. usermgr. domain. log; import com. tgb. usermgr. domain. user; import com. tgb. usermgr. util. hibernateUtils; public class UserManagerImpl extends HibernateDaoSupport implements UserManager {private LogManager logManager; public void addUser (User user) throws Exception {// this. getSession (). save (user); this. getHibernateTemplate (). save (user); Log log = new Log (); log. setType ("operation log"); log. setTime (new Date (); log. setDetail ("XXX"); logManager. addLog (log); throw new Exception ();} public void setLogManager (LogManager logManager) {this. logManager = logManager ;}}


Client test class:

UserManagerImplTest. java

Package com. tgb. usermgr. manager; import org. springframework. beans. factory. beanFactory; import org. springframework. context. support. classPathXmlApplicationContext; import com. tgb. usermgr. domain. user; import junit. framework. testCase; public class UserManagerImplTest extends TestCase {public void testAddUser () {BeanFactory factory = new ClassPathXmlApplicationContext ("applicationContext -*. xml "); UserManager userManager = (UserManager) factory. getBean ("userManager"); User user User = new user (); User. setName ("Zhang San"); try {userManager. addUser (user);} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}


Configuration file:

Hibernate. cfg. xml

<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
   

ApplicationContext-common.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: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http: // www. Springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd "> <! -- Configure SessionFactory --> <bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean "> <property name =" configLocation "> <value> classpath: hibernate. cfg. xml </value> </property> </bean> <! -- Configure the Transaction Manager --> <bean id = "transactionManager" class = "org. springframework. orm. hibernate3.HibernateTransactionManager "> <property name =" sessionFactory "> <ref bean =" sessionFactory "/> </property> </bean> <! -- The methods of those classes use transactions --> <aop: config> <aop: pointcut id = "allManagerMethod" expression = "execution (* com. tgb. usermgr. manager. *. *(..)) "/> <aop: advisor pointcut-ref =" allManagerMethod "advice-ref =" txAdvice "/> </aop: config> <! -- Transaction propagation feature --> <tx: advice id = "txAdvice" transaction-manager = "transactionManager"> <tx: attributes> <tx: method name = "add *" propagation = "REQUIRED"/> <tx: method name = "del *" propagation = "REQUIRED"/> <tx: method name = "modify *" propagation = "REQUIRED"/> <tx: method name = "*" propagation = "REQUIRED" read-only = "true"/> </tx: attributes> </tx: advice> </beans>


ApplicationContext-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:aop="http://www.springframework.org/schema/aop"     xmlns:tx="http://www.springframework.org/schema/tx"     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">           <bean id="userManager" class="com.tgb.usermgr.manager.UserManagerImpl"><property name="sessionFactory" ref="sessionFactory"/><property name="logManager" ref="logManager"/></bean><bean id="logManager" class="com.tgb.usermgr.manager.LogManagerImpl"><property name="sessionFactory" ref="sessionFactory"/></bean></beans>


:


Iii. Summary.

The combination of Hibernate and Spring makes the system more flexible and can cope with certain demand changes. Hibernate solves the problem of database transformation, and Spring solves the problem of changing between classes. It not only makes the system more flexible, but also greatly improves the development efficiency and maintenance efficiency of our developers.


Start from which to learn java spring struts hibernate

I think it should be hibernate-struts -- spring
Cause: hibernate is not associated with the web, and it feels easy to learn. At least you do not need to start or stop the server.
Then struts works with hibernate to learn how to make small applications,
Finally, use spring to integrate these
Alternatively, you can use hibernate in spring in struts

How to quickly learn spring, hibernate, and structs

I have never heard of any method for quick learning.
Everything must be learned in a down-to-earth Manner. Although you have a C Foundation, C and WEB development do not have much to do with it.
So now it seems that your only advantage is that programming is better than the average person.
I suggest that you first learn Struts and then the first two of Hibernate's last Spring integration.
The premise is that you need to master the JSP + Servlet technology or else it will be very empty.
Let's take a look at my suggestions, but you must be faster than learning without a basic program.
There are no good teaching materials. There are tutorials on the Internet. Let's take a look at what we learn.

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.