Dao spring + hibernate framework of several ways to access the database

2010-06-25  来源:本站原创  分类:Java  人气:194 

In the spring + hibernate in the structure, there are several ways to access the database by spring dependency injection to distinguish between three kinds, and in this look at again before spring dependency injection, spring the two main core is IOC (Inversion of Control) and AOP (Aspect Oriented Programming), inversion of control is to control the transfer, from the previous call by the Bean to control the interface to be transferred to the container or other resources, from container to find and instantiate the interface to call can also be interpreted as dependent on injection, that is, the spring configuration file to call the interface, set constructor configured to Bean.
Here is based on dependency injection to be divided into sessionFactory, hibernateTemplate, jdbcTemplate, essentially only hibernateTemplate and jdbcTemplate division two.
1, injection sessionFactory

In the spring configuration file for Dao inject sessionFactory, namely:
<bean>
<property name="sessionFactory">
<ref local="sessionFactory" />
</ Property>
</ Bean>
SessionFactory dependency injection is not here to Dao layer class, but to HibernateDaoSupport, see spring Source org/springframework/orm/hibernate3/support / HibernateDaoSupport.java inside, there sessionFactory the set, get operations:
public final void setSessionFactory (SessionFactory sessionFactory) (
this.hibernateTemplate = createHibernateTemplate (sessionFactory); / / by sessionFactory to generate hibernateTemplate
)
public final SessionFactory getSessionFactory () (
return (this.hibernateTemplate! = null? this.hibernateTemplate.getSessionFactory (): null);
)
So Dao layer class inheritance HibernateDaoSupport, you can by this.getHibernateTemplate () to operate the database,
Updated data: this.getHibernateTemplate (). Update (bo);
Query data: this.getHibernateTemplate (). Find (bo);
Add data: this.getHibernateTemplate (). Save (bo);
Delete data: this.getHibernateTemplate (). Delete (bo);

From the above we can see the power of spring + hibernate and access data without jdbc as usual, as to write long list of try, catch statement, but also connect to the database, run out and then close the database connection, but with a statement that can handle that.
Here sessionFactory automatically by the spring automatically connect, close, of course, you can manually connect, close, such as the following approach:

Session session = this.getHibernateTemplate (). GetSessionFactory (). OpenSession ();
Transaction tx = session.beginTransaction ();
/*-------------- Query data ------------------------*/
String str = "hql";
Query query = session.createQuery (str);
List list = query.list ();
/*-------------- Deleted data ------------------------*/
session.load (bo, ID);
session.delete (bo);
/*-------------- Add data ------------------------*/
session.save (bo);
/*-------------- Modify data -----------------------*/
session.load (bo, ID);
session.update (bo);
/*-------------- End --------------------------- * /
tx.commit ();
session.close ();

Beginner Hebernate people should be very familiar with these codes, there is no spring to provide hibernateTemplate, in just the Hibernate in this regard would have to visit with access to data.

2, injection hibernateTemplate

This method essentially the same as with the above sessionFactory injection, but further layer of packaging, this is the Dao of the biggest benefits of the class will not need to inherit HibernateDaoSupport (in java is single inheritance, and this the only time the succession was HibernateDaoSupport deprived of opinions on it a shame?), but in this configuration prior to the first good hibernateTemplate, namely:
<bean>
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</ Property>
</ Bean>

Again use the Dao were injected into hibernateTemplate dependence, namely:
<bean>
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</ Property>
</ Bean>

In the Dao layer would add hibernateTemplate object class to the corresponding configuration file in the dependency injection:
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate () (
return hibernateTemplate;
)
public void setHibernateTemplate (HibernateTemplate hibernateTemplate) (
this.hibernateTemplate = hibernateTemplate;
)

hibernateTemplate additions and deletions of data, like search to Jiugen the above, namely:
Updated data: hibernateTemplate (). Update (bo);
Query data: hibernateTemplate (). Find (bo);
Add data: hibernateTemplate (). Save (bo);
Delete data: hibernateTemplate (). Delete (bo);

3, injection jdbcTemplate
If the SQL jdbc before also never forget, but also on Hibernate's HQL not like, then it can be used to add or delete search jdbcTemplate change the database. In certain cases by jdbcTemplate also more convenient, but also improve the query efficiency. Before injection hibernateTemplate should like it, the first configured jdbcTemplate,
<bean>
<property name="dataSource">
<ref bean="dataSource" />
</ Property>
</ Bean>
If jdbcTemplate and hibernateTemplate configuration point to the same dataSource, it can share the same transaction.
Again use the Dao were injected into jdbcTemplate dependence, namely:
<bean>
<property name="jdbctemplate">
<ref bean="jdbcTemplate" />
</ Property>
</ Bean>
In the Dao layer would add jdbctemplate object class to the corresponding configuration file in the dependency injection:
protected JdbcTemplate jdbctemplate;
public JdbcTemplate getJdbctemplate () (
return jdbctemplate;
)
public void setJdbctemplate (JdbcTemplate jdbctemplate) (
this.jdbctemplate = jdbctemplate;
)
Can now access data through jdbctemplate a:
Query data:
/*------------ Inquiries separate -------------------*/
String SQL = "select name from table";
List list = jdbctemplate.queryForList (SQL);
/*------------ Query multi-column ------------------*/
Hashtable hash = new Hashtable ();
jdbctemplate.query (SQL,
new RowCallbackHandler () (
public void processRow (ResultSet rs) throws SQLException (
hash.put (rs.getString (1), rs.getString (2));
)
));
/*---------- Inquiry which -----------*/ filled to the vo
String SQL = "select * from table where";
String [] obj = new String [1];
obj [0] = N;
VO vo = new VO (); / / here temporarily used to express VO, VO nature is not the case
List list = jdbcTemplate.query (SQL, obj, vo);

VO to achieve RowMapper interfaces mapRow method, the result set to bo filled inside:
class VO implements RowMapper (
public Object mapRow (ResultSet rs, int index) throws SQLException (
Bo bo = new Bo ();
bo.setProperty (rs.getString (1));
bo.setProperty (rs.getString (2));
bo.setProperty (rs.getString (3));
return bo;
)
)
/*---------------- Update data ------------------*/
String SQL = "update table set name =?";
String [] obj = new String [1];
obj [1] = "new name";
jdbcTemplate.update (SQL, obj);
/*---------------- Delete data ------------------*/
String SQL = "delete from table where";
jdbcTemplate.execute (SQL);
/*---------------- Add data ------------------*/
String SQL = "insert into table (property1, property2) values ('" + property1 +"','"+ property1 +"')";
jdbcTemplate.execute (SQL);
This is the framework in spring + hibernate Dao visit to access data on the method to make a conclusion, due to the limited level, errors and deficiencies in the criticism welcome correction.

相关文章