Test item: SPRING4_STRUTS2_HIBERNATE4
First, create hibernatesessionfactory
Packagecom.chen.utils;Importorg.hibernate.HibernateException;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;ImportOrg.hibernate.boot.registry.StandardServiceRegistryBuilder;Importorg.hibernate.cfg.Configuration;ImportOrg.hibernate.service.ServiceRegistry; Public classHibernatesessionfactory {Private Static FinalThreadlocal<session> ThreadLocal =NewThreadlocal<session>(); //private static String configfile = "/hibernate.cfg.xml"; Private StaticConfiguration Configuration =NewConfiguration (). Configure (); Private Staticsessionfactory sessionfactory; Static { Try{standardserviceregistrybuilder Serviceregistrybuilder=NewStandardserviceregistrybuilder (); Serviceregistry Serviceregistry=serviceregistrybuilder.applysettings (Configuration.getproperties ()). build (); Sessionfactory=configuration.buildsessionfactory (serviceregistry); } Catch(hibernateexception e) {e.printstacktrace (); } } Privatehibernatesessionfactory () {} Public Staticsession getsession () {Session session=(Session) threadlocal.get (); if(Session = =NULL|| !Session.isopen ()) {Session= (Sessionfactory! =NULL) ?sessionfactory.opensession ():NULL; Threadlocal.set (session); } returnsession; } Public Static voidClosesesssion () {Session session=Threadlocal.get (); Threadlocal.set (NULL); if(Session! =NULL) {session.close (); } } Public Staticsessionfactory getsessionfactory () {returnsessionfactory; } Public Static voidsetsessionfactory (sessionfactory sessionfactory) {hibernatesessionfactory.sessionfactory=sessionfactory; } Public StaticConfiguration getconfiguration () {returnconfiguration; } Public Static voidsetconfiguration (Configuration configuration) {Hibernatesessionfactory.configuration=configuration; }}
Second, testing in the test class
Packagecom.chen.test;Importjava.util.List;ImportOrg.hibernate.Query;Importorg.hibernate.Session;Importorg.hibernate.Transaction;Importorg.junit.Test;Importcom.chen.utils.HibernateSessionFactory;Importcom.chen.vo.News; Public classHibernatetest {//Get Session@Test Public voidSessiontest () {Session session=hibernatesessionfactory.getsession (); SYSTEM.OUT.PRINTLN (session); } //querying list information for news@Test Public voidListnews () {Session session=hibernatesessionfactory.getsession (); Query Query= Session.createquery ("From News"); List<?> list =query.list (); SYSTEM.OUT.PRINTLN (list); } //querying the specified news by ID@Test Public voidFindByID () {Session session=hibernatesessionfactory.getsession (); Query Query= Session.createquery ("From News where id=?")); //Setting ParametersQuery.setparameter (0, 1); //single-Value retrievalNews n =(News) Query.uniqueresult (); SYSTEM.OUT.PRINTLN (n); } //Add News@Test Public voidSavenews () {Session session=hibernatesessionfactory.getsession (); Transaction TX=session.begintransaction (); News N=NewNews (); N.settitle ("Title3"); N.setcontent ("Content3"); Session.saveorupdate (n);; Tx.commit (); Session.close (); } //Modify News@Test Public voidUpdatenews () {Session session=hibernatesessionfactory.getsession (); //get news for the specified ID firstQuery query = Session.createquery ("From News where id=?")); Query.setparameter (0, 5); News N=(News) Query.uniqueresult (); //Make changes againTransaction tx =session.begintransaction (); N.setcontent ("Update_content5"); Session.saveorupdate (n); Tx.commit (); Session.close (); SYSTEM.OUT.PRINTLN (n); } //Delete News@Test Public voidDetelenews () {Session session=hibernatesessionfactory.getsession (); Query Query= Session.createquery ("From News where id=?")); Query.setparameter (0, 4); News N=(News) Query.uniqueresult (); Transaction TX=session.begintransaction (); Session.delete (n); Tx.commit (); Session.close (); }}
Summary: The implementation of "check" is implemented by querying the HQL statement.
Attached: hibernate.cfg.xml
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd "><!--c3p0 Connection Pool -<hibernate-configuration><session-factory> <!--SQL when displaying the actual operational database - < Propertyname= "Show_sql">True</ Property> <!--SQL dialect, the Oracle is set here - < Propertyname= "dialect">Org.hibernate.dialect.Oracle10gDialect</ Property> <!--drivers: Configuration of Oracle Database - < Propertyname= "Connection.driver_class">Oracle.jdbc.driver.OracleDriver</ Property> <!--JDBC URL - < Propertyname= "Connection.url">Jdbc:oracle:thin: @localhost: 1521:MYORCL</ Property> <!--Database user name - < Propertyname= "Connection.username">Scott</ Property> <!--Database Password - < Propertyname= "Connection.password">Root</ Property> <!--Specify the minimum number of connections in the connection pool - < Propertyname= "Hibernate.cp30.minsize">5</ Property> <!--Specify the maximum number of connections in the connection pool - < Propertyname= "Hibernate.c3p0.maxsize">20</ Property> <!--specifies that the timeout in the connection pool is often - < Propertyname= "Hibernate.cp30.timeout">1800</ Property> <!--Specify the maximum number of statement objects to cache in the connection pool - < Propertyname= "Hibernate.cp30.max_statements">50</ Property> <!--automatically create database tables as needed - < Propertyname= "Hbm2ddl.auto">Update</ Property> <!--object vs. database table image File - <MappingResource= "Com/chen/vo/news.hbm.xml"/></session-factory></hibernate-configuration>
Through the session to achieve the increase, delete, check, change