spring+hibernate Two kinds of methods to realize and delete change and check
First, define a customer bean class and set the Customer.hbm.xml file. Again define a DAO interface. Prepare a jdbc.properties file.
The first method:
Define an object of the Hibernatetemplate class in the DAO class, using the object to invoke the Hibernatetemplate template encapsulation method, as follows: Java code/*** * Hibernatetemplate's transfer mode/publ IC class Customerdaoimpl implements Customerdao {//Set hibernatetemplate properties private Hibernatetemplate Hibernate Template; The Set method must be set to public void Sethibernatetemplate (Hibernatetemplate hibernatetemplate) {This.hibernatetemplat e = hibernatetemplate; } // Insert Method @Override public void insert (customer entity) { hibernatetemplate.save (entity); } // Delete method (delete according to question) @Override public void delete (customer entity) { hibernatetemplate.delete (entity); } // Delete method, deleted by ID, this method is unsuccessful @Override public void deletebyid (int id) { // hibernateTemplate. &NBSP;&NBSP;&NBsp; } // query all records @ override public list<customer> selectall () { // todo auto-generated method stub List<Customer> entities = Hibernatetemplate.find ("From customer"); return entities; } // Find records by ID @Override public Customer selectbyid (int id) { // TODO Auto-generated method stub list<customer> entitise = hibernatetemplate .find ("from customer where id=" + id) ; if (Entitise.size () > 0) { Customer entity = entitise.get (0); return entity; } return null; } // Update Methods @Override public void update (customer entity) { // todo auto-generated method stub hibernatetemplate.update (Entity); } }
Some of the code in the
XML is as follows:
Java code <!-- decentralized configuration --> <context:property-placeholder location= "Jdbc.properties" /> <!-- C3P0 data source --> <bean id= "DataSource" class= " Com.mchange.v2.c3p0.ComboPooledDataSource "> < !-- driver --> <property name= " Driverclass "> <value >${jdbc.driverClass}</value> </property > <!-- Connection URL address --> <property name= "Jdbcurl" > <value>${jdbc.url}</value> </property> <!-- Connected user name --> <property name= "User" > <value>${jdbc.user}</value> </property> <!-- Connection Password --> <property name= " Password "> <value> ${jdbc.password}</value> </property> &nbsP <!-- Max pool number --> <property name= "Maxpoolsize" > <value>${c3p0.pool.max}</value> </property> <!-- Minimum pool number --> <property name= "Minpoolsize" > <value>${c3p0.pool.min}</value> </property> <!-- Default initialized pool count --> <property name= " Initialpoolsize "> <value>${c3p0.pool.init} </value> </property> </bean> <bean id= " Sessionfactory " class=" Org.springframework.orm.hibernate3.LocalSessionFactoryBean "> <!-- Setting data sources --> <property name= "DataSource" ref= "DataSource" /> <!-- Property settings --> <property name= "Hibernateproperties" > <props> < prop key= "Hibernate.show_sql" >${hibernate.show_sql}</prop> <prop key= " Hibernate.hbm2ddl.auto ">${hibernate.hbm2ddl.auto}</prop> </props> </property> <!-- mapping file configuration -- > <property name= "Mappingresources" > <list> <value> Cn/csdn/domain/customer.hbm.xml</value> </list > </property> </bean> <!-- Hibernate templates --> <bean id= "Hibernatetemplate" class= " Org.springframework.orm.hibernate3.HibernateTemplate "> <property name= "Sessionfactory" ref= "Sessionfactory" /> </bean> <!-- dao Operations bean --> <bean id= "Customerdaoimpl" class= "Cn.csdn.dao.CustomerDaoImpl" > <!--injection dependency template--><property name= "Hibernatetemplate" ref= "Hibernatetemplate"/></bean>
The second method:
The DAO implementation class inherits the Hibernatedaosupport class, which is an abstract class that has a hibernatetemplate property that passes through This.gethibernatetemplate () You can get an object for a Hibernatetemplate class.
The code in the DAO implementation class is as follows:
Java code /** * Inheritance Hibernatedaosupport class, Hibernatedaosupport class encapsulates a hibernatetemplate variable */ public class customerdaoimpl1 extends hibernatedaosupport implements CustomerDao { // Add records @Override public void insert (customer entity) { this.gethibernatetemplate (). Save (entity); } // Delete records (delete by entity) @ override public void delete (customer entity) { // todo auto-generated method stub &NBSP;&NBSP;&NBsp; this.gethibernatetemplate (). Delete (entity); } // Delete record (deleted by ID), unsuccessful // @Override public void deletebyid (int id) { // TODO Auto-generated method stub // this.gethibernatetemplate (). Delete (entity); } // query all records @Override public list<customer> selectall () { // TODO Auto-generated method stub &nbsP; list<customer> entities = this.gethibernatetemplate (). Find ( "from customer "); return entities; } // find records by ID @ override public customer selectbyid (int id) { // todo auto-generated method stub list<customer> entities= This.gethibernatetemplate (). Find ("from customer where id=" +id); if (Entities.size () >0) { cuStomer entity=entities.get (0); return entity; } return null; } // Update method @Override public void update (customer entity) { // TODO Auto-generated method stub this.gethibernatetemplate (). Update (entity); } }
Part of the code in xml:
Java code <!-- decentralized configuration --> <context:property-placeholder location= "Jdbc.properties" /> <!-- C3P0 data source --> <bean id= "DataSource" class= " Com.mchange.v2.c3p0.ComboPooledDataSource "> < !-- driver --> <property name= " Driverclass "> <value >${jdbc.driverClass}</value> </property > <!-- Connection URL address --> <property name= "Jdbcurl" > <value>${jdbc.url}</value> </property> <!-- Connected user name --> <property name= "User" > <value>${jdbc.user}</value> </property> <!-- Connection Password --> <property name= " Password "> <value> ${jdbc.password}</value> </property> &nbsP <!-- Max pool number --> <property&n