Integrated example of Spring and Hibernate

xiaoxiao2021-03-06  14

One. Just as before, the most important thing for the framework is the understanding of the understanding of the ideology and the writing of the profile, so it is still a configuration file:

In the integration of Spring and Hibernate, the configuration file is mainly in the Spring configuration file, as in the following example:

Class = "org.apache.commons.dbcp.basicdataasource"

Destroy-method = "close">

com.mysql.jdbc.driver

jdbc: mysql: //192.168.151.72: 3306 / QuickStart

root

Class = "org.springframework.orm.hibernate.localsessionFactoryBean">

Customer.hbm.xml

Net.sf.hibernate.Dialect.MysqldiaLect

True

Class = "org.springframework.orm.hibernate.hibernateTransactionManager">

Class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

PropAgation_required

PropAgation_required, readonly

Class = "org.springframework.web.servlet.handler.simpleurlhandlermapping">

LoginAction

As shown above, the configuration file seems long, very troublesome. But (as comments) we can be divided into three aspects (because all the classes or configuration information to be deployed in Spring generally define a "ID" as "***", so I have encountered below. This is said: Define a "***"):

1. Hibernate-db:

First define a "DataSource" (consistent with hibernate.cfg.xml), then define a "sessionFactory", use it "Datasource" and define "HibernateProperties" in this bean (ie some settings about Hibernate) ), As well as the configuration file "classname" .hbm.xml corresponding to the table.

2. Hibernate-Spring:

First define a "transactionManager", use it to call "SessionFactory" above, then define an interface to implement class "beans" (it also calls "sessionFactory" on the above defined "sessionFactory) and interface" beanfactory "interacting with Hibernate. Call "TransactionManager" and "beans" with "beanfactory" and define the interaction parameters named "TransactionAttributes".

3. Spring-action Spring-bean

Define "LoginAction", corresponding to the Action to process the business in Spring. And define "urlmapping" to define the corresponding page and the action.

two. Regarding the package to be introduced, in addition to introducing Spring, Hibernate, and database driver packages, it is also introduced into a AopAlliance.jar package provided by a third party (it seems to be a package that integrates Spring and Hibernate).

three. Attached to the source code used in this example:

Customer.java:

Package com.ln.hb;

Import java.io.serializable;

Public Class Customer Implements Serializable {

Private int ID;

PRIVATE STRING UserName;

PRIVATE STRING Password;

Public int getId () {

Return ID;

}

Public string getpassword () {

Return Password;

}

Public string getUsername () {

Return UserName;

}

Public void setid (int id) {

THIS.ID = ID;

}

Public void setpassword (string password) {

this.password = password;

}

Public void setusername (String username) {

THIS.USERNAME = UserName;

}

}

Data.java:

/ *

* CREATED ON 2005/01/08

*

* Todo to change the Template for this generated file go to to THERATED FILE Go TO

* WINDOW - Preferences - Java - Code Style - Code Templates * /

Package com.ln.spring;

Import java.util.list;

Import com.ln.hb.customer;

/ **

* @Author n-liang

*

* Todo to change the Template for this generated Type Comment Go To to

* WINDOW - Preferences - Java - Code Style - Code Templates

* /

Public interface data {

Public void insertcustomer (Customer Customer);

Public List Selectcustomer (String SQL);

}

DataImpl.java:

Package com.ln.spring;

Import java.util.list;

Import org.springframework.orm.hibernate.support.hibernatedaosupport;

Import com.ln.hb.customer;

/ **

* @Author n-liang

*

* Todo to change the template for this generated Type Comment Go to window -

* Preferences - Java - Code Style - Code Templates

* /

Public Class DataImpl Extends Hibernatedaosupport IMPLEments DATA {

Public void insertcustomer (Customer Customer) {

GethibernateTemplate (). save (customer);

}

Public List SelectCustomer (String SQL)

{

Return gethibernateTemplate (). Find (SQL);

}

}

LoginAction.java:

Package com.ln.spring;

/ **

* @Author n-liang

*

* Todo to change the template for this generated Type Comment Go to window -

* Preferences - Java - Code Style - Code Templates

* /

Import java.io.ioException;

Import java.util.list;

Import javax.servlet.servletException;

Import javax.servlet.http.httpservletRequest;

Import javax.servlet.http.httpservletResponse;

Import org.springframework.Web.Servlet.ModelandView;

Import Org.SpringFramework.Web.Servlet.mvc.controller;

Import com.ln.hb.customer;

Public Class Loginaction Implements Controller {

Data Data;

/ **

* @Return Returns the Data.

* /

Public data getdata () {

Return Data;

}

/ **

* @Param Data The Data TO SET.

* /

Public void setdata (data data) {this.data = data;

}

Public ModelandView Handlerequest (httpservletRequest Request,

HttpservletResponse response) throws servletexception, ioException {

Data userdao = (data) data;

Customer Customer = New Customer ();

// User.setUsername ("Erica");

// user.setpassword ("mypass");

// UserDao.insertCustomer (user);

List List = UserDao.SelectCustomer ("From Customer As Customer);

For (int i = 0; i

{

System.out.println ((Customer) List.get (i)). GetUserName ());

}

Return New ModelandView ("main.jsp");

}

}

Customer.hbm.xml:

"- // Hibernate / Hibernate mapping DTD 2.0 // en"

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

Web.xml:

hbspring

Org.springframework.web.servlet.dispatcherServlet

1

hbspring

*. do

转载请注明原文地址:https://www.9cbs.com/read-49479.html

New Post(0)