several methods of DAO accessing database in Spring+hibernate architectureIn the spring+hibernate architecture, there are several ways to access a database, by spring Dependency injection to differentiate between 3 kinds, before this to understand spring's dependency injection, the two main core of spring is the IOC (control reversal) and AOP (aspect-oriented programming) , control reversal is control transfer, from the past, the bean to control the interface or other resources to be invoked to the container, the container to find and instantiate the interface to invoke, can also be interpreted as dependency injection, that is, in the spring configuration file to invoke the interface, settings, construction child configuration to the bean. This side is based on dependency injection to differentiate into sessionfactory, hibernatetemplate, JdbcTemplate, essentially divided into only hibernatetemplate and jdbctemplate these two kinds.1. Inject sessionfactory In the spring configuration file, the DAO is injected with sessionfactory, namely: <bean id= "Classdao" class= "Cn.jmu.data.dao.impl.ClassImpl" > <property name= "Sessionfactory" > <ref local= "Sessionfactory"/> </property> </bean> Instead of giving the class in the DAO layer, Sessionfactory relies on the Hibernatedaosupport, see the Spring source file org/springframework/orm/hibernate3/support/ Hibernatedaosupport.java inside, there is a sessionfactory set, get operation: public final void Setsessionfactory (sessionfactory Sessionfactory) { This.hibernatetemplate = Createhibernatetemplate (sessionfactory);//Generate Hibernatetemplate by sessionfactory Public final Sessionfactory Getsessionfactory () { return (this.hibernatetemplate!= null this.hibernateTemplate.getSessionFactory (): null); So the class inherits hibernatedaosupport in the DAO layer, and you can manipulate the database by This.gethibernatetemplate (). Update 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 can see the powerful power of spring+hibernate, access to data not like the previous JDBC, to write a large number of try,catch statements, but also to connect the database, and then close the database connection, and use a statement can be done. This sessionfactory is automatically connected and closed by spring, but you can also manually connect and close, as follows: Session session=this.gethibernatetemplate (). Getsessionfactory (). Opensession (); Transaction tx=session.begintransaction (); /*--------------Query Data------------------------* * String str= "HQL"; Query query=session.createquery (str); List list=query.list (); /*--------------Delete data------------------------/* Session.load (BO,ID); Session.delete (BO); /*--------------Add Data------------------------/* Session.save (BO); /*--------------Modify the Data-----------------------* * Session.load (BO,ID); Session.update (BO); /*--------------End---------------------------*/Tx.commit (); Session.close (); Beginner hebernate should be familiar with this code, and without spring providing hibernatetemplate, this is the way to access data in the hibernate alone. 2. Inject Hibernatetemplate The essence of this approach is the same as the above injection of sessionfactory, just another layer of packaging, the biggest advantage is that the DAO classes do not have to inherit Hibernatedaosupport (in Java is a single inheritance, It would be a pity that the only time the inheritance was hibernatedaosupport deprived. But before that, you have to configure the hibernatetemplate, namely: <bean id= "Hibernatetemplate" class= "Org.springframework.orm.hibernate3.HibernateTemplate" > <property name= "Sessionfactory" > <ref bean= "Sessionfactory"/> </property> </bean> Then inject dependency on the DAO to use the hibernatetemplate, namely: <bean id= "Classdao" class= "Cn.jmu.data.dao.impl.ClassImpl" > <property name= "Hibernatetemplate" > <ref bean= "Hibernatetemplate"/> </property> </bean> Classes at the DAO layer are added to the Hibernatetemplate object to correspond to the dependencies injected in the configuration file: Private Hibernatetemplate hibernatetemplate; Public Hibernatetemplate gethibernatetemplate () { return hibernatetemplate; } public void Sethibernatetemplate (Hibernatetemplate hibernatetemplate) { This.hibernatetemplate = hibernatetemplate; } Hibernatetemplate to the data added and deleted to the same as above, namely: Update data: Hibernatetemplate (). Update (BO); Query data: hibernatetemplate (). Find (Bo); Add Data: Hibernatetemplate (). Save (Bo); Delete data: Hibernatetemplate (). Delete (Bo); 3. Inject JdbcTemplateIf the previous JDBC SQL is still obsessed with the hibernate of the HQL, you can use JdbcTemplate to modify the database. In some cases, the use of JdbcTemplate is also more convenient, and even improve query efficiency. Before this also should like inject hibernatetemplate, the first configuration good jdbctemplate, <bean id= "JdbcTemplate" class= " Org.springframework.jdbc.core.JdbcTemplate "> <property name= "DataSource" > <ref bean= "DataSource"/> </property> </bean> if both JdbcTemplate and hibernatetemplate are pointing to the same datasource, you can share the same transaction. Then inject dependency on DAO to use JdbcTemplate, namely: <bean id= "Classdao" class= "Cn.jmu.data.dao.impl.ClassImpl" > <property name= "JdbcTemplate" > <ref bean= "JdbcTemplate"/> </property> </bean> classes in the DAO layer will add JdbcTemplate objects to correspond to the dependencies injected in the configuration file: protected JdbcTemplate jdbctemplate; Public JdbcTemplate getjdbctemplate () { return jdbctemplate; } public void Setjdbctemplate (JdbcTemplate jdbctemplate) { This.jdbctemplate = JdbcTemplate; The data can now be accessed via JdbcTemplate: Query data:/*------------Query Single-------------------/String sql= "Select name from table"; List list= jdbctemplate.queryforlist (SQL); /*------------Query multiple columns------------------* * Hashtable hash = new Hashtable (); Jdbctemplate.query (SQL, New RowCallbackHandler () { public void Processrow (ResultSet rs) throws SQLException { Hash.put (rs.getstring (1), rs.getstring (2)); } }); * *----------query is populated inside VO-----------*/String sql= "select * from table where id=?"; string[] obj = new String[1]; Obj[0] = N; Vo vo= new Vo (); This way take up Vo to show that the nature of VO is not like this List List = Jdbctemplate.query (SQL,OBJ,VO); VO to implement the Maprow method in the RowMapper interface, populate the result set into Bo: 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 id= '" +id+ ""; Jdbctemplate.execute (SQL); /*----------------Add Data------------------*/String sql= "insert into table (Property1,property2) VALUES (' +property1+" ', ' "+property1+"; Jdbctemplate.execute (SQL); This is a summary of the DAO Access data method in the Spring+hibernate framework, because of the limited level, errors and deficiencies welcome criticism. |