Previous Words struts2,hibernate,spring integrated notes (1)
Then continue
Start hibernate configuration after configuring struts
Hibernate's environment is not dependent on the Web development environment, the first time I configured hibernate with the Javase,
In window-> preferences->java->user libraries, create a new use libraries, add the appropriate jar package
Then in project build Path->build configurationpath->add libraries Select Use libraries join the libraries you created
Under normal circumstances there is no problem, at the beginning of my configuration is no problem, spring and hibernate after the integration is not a problem
But there was a problem when the SSH framework was fully integrated.
The reason, or struts+spring in the test class do not recognize Hibernate
So, the best way to do this is to add the jar package to Lib.
This is the jar package that hibernate needs alone.
This includes some annotation content, and if you're using XML configuration files, you don't need to join
Then it's the configuration file.
Also, just go straight to the example and look for it.
Create a new hibernate.cfg.xml under the SRC directory
<?XML version= ' 1.0 ' encoding= ' utf-8 '?><!DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://hibern Ate.sourceforge.net/hibernate-configuration-3.0.dtd "><hibernate-configuration> <session-factory> <!--Database Connection Settings - < Propertyname= "Connection.driver_class">Com.mysql.jdbc.Driver</ Property> < Propertyname= "Connection.url">Jdbc:mysql://localhost/oa</ Property> < Propertyname= "Connection.username">Root</ Property> < Propertyname= "Connection.password"></ Property> <!--JDBC Connection pool (use the built-in) - <!--<property name= "connection.pool_size" >1</property> - <!--SQL dialect - < Propertyname= "dialect">Org.hibernate.dialect.MySQLDialect</ Property> <!--Enable Hibernate ' s automatic session context management - <!--<property name= "Current_session_context_class" >thread</property> - <!--Disable the Second-level cache - < Propertyname= "Cache.provider_class">Org.hibernate.cache.NoCacheProvider</ Property> <!--Echo all executed SQL to stdout - < Propertyname= "Show_sql">True</ Property> < Propertyname= "Format_sql">True</ Property> <!--Drop and re-create the database schema on startup - < Propertyname= "Hbm2ddl.auto">Update</ Property> <Mappingclass= "Com.hibernate.user"/> </session-factory></hibernate-configuration>
This is some of the most basic configuration, according to their own circumstances slightly change can
and write a simple javabean.
Packagecom.hibernate;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;Importjavax.persistence.Id; Public classUser {Private intID; PrivateString name; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } }
Provide the configuration file for this JavaBean, add the User.hbm.xml in the same package
<?xml version= "1.0"? ><! DOCTYPE hibernate-Mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" Package = " Com.hibernate "> <class name=" user "table=" itcast_user "> <id name=" id "> class= "native"/> </id> <property name= "name"/> </class >
Import JUnit, create Sourcefolder, create an identical package
Enter the following code
Packagecom.hibernate;Import Staticorg.junit.assert.*;Importjava.util.Date;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.cfg.AnnotationConfiguration;Importorg.hibernate.cfg.Configuration;Importorg.junit.Test; Public classTest {@Test Public voidTest () {User U=Newuser (); U.setname ("Ni"); Configuration CFG=Newannotationconfiguration (). Configure (); Sessionfactory SF=cfg.configure (). Buildsessionfactory (); Session Session=sf.opensession (); Session.begintransaction (); Session.save (U); Session.gettransaction (). commit (); Session.close (); Sf.close (); }}
Will
<property name= "Hbm2ddl.auto" >update</property>
The update in is temporarily changed to create
Remember to add an OA library to your database
Run the user table appears
There's a row of data in the table.
Struts2,hibernate,spring Integrated Notes (2)