Hibernate2 of the ssh (sturts2_spring_hibernate) framework and springhibernate
I. What I want to answer today is to further improve the previous hibernate1. The first step is to use spring to inject a SessionFactory instance to avoid new instances; the second is the application database pool (c3p0 ).
II. Environment Construction:
Compile in the previous BASICS (struts2 jar package, spring jar package, and hibernate jar package, if you do not understand please see: http://www.cnblogs.com/demoMeng ):
Now you need to use the c3p0 jar, find the downloaded hibernate compressed package, decompress and decompress the root directory -- lib --, optional --, and copy the jar in c3p0 to your project lib:
Ghost removes the hibernate. cfg. xml configuration file and replaces it with SessionFactory injected in spring (sessionFactory is configured in hibernate. cfg. xml)
Previous project:
The previous hibernate. cfg. xml configuration file:
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
Current Project:
The current applicationContext. xml configuration file (the database used is the SQL-server2008 ):
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: p = "http://www.springframework.org/schema/p" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: context = "http://www.springframework.org/schema/context" xmlns: jee = "http://www.springframework.org/schema/jee" xmlns: tx = "http://www.springframework.org/schema/tx" xsi: schemaLocati On = "http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/jee http://www.springframework. Org/schema/jee/spring-jee-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "> <! -- Import an external file and set the database information as a variable, which is beneficial to the installation project and subsequent deployment of technicians. (Corresponding to the jdbcSql. properties file outside the project) --> <context: property-placeholder location = "classpath: jdbcSql. properties" ignore-unresolvable = "true"/>
<Bean id = "myActionService" class = "action. ActionService" scope = "prototype"> </bean>
<Bean id = "myActionService" class = "action. actionService "scope =" prototype "> <property name =" ms "ref =" myServiceImp "> </property> </bean> <bean id =" myServiceImp "class =" services. myServiceImp "scope =" prototype "> <property name =" md "ref =" myDaoImp "> </property> </bean> <bean id =" myDaoImp "class =" dao. myDaoImp "scope =" prototype "> <! -- Inject a sessionFactory instance to the sessionFactory member attribute set in the daoImp implementation class --> <property name = "sessionFactorySql" ref = "mySessionFactorySql"> </property> </bean> <! -- Define a sessionFactory bean and use spring to inject instances to sessionFactory --> <bean id = "mySessionFactorySql" class = "org. springframework. orm. hibernate5.LocalSessionFactoryBean"> <! -- The data source includes the database username and password. The following bean is referenced here. For convenience, I used the same font color --> <property name = "dataSource" ref = "myDataSourceSql"> </property> <! -- Define hibernate Attributes --> <property name = "hibernateProperties"> <props> <prop key = "hibernate. dialect "> org. hibernate. dialect. SQLServer2008Dialect </prop> <prop key = "hibernate. show_ SQL "> true </prop> <prop key =" hibernate. format_ SQL "> true </prop> <prop key =" hibernate. connection. autocommit "> false </prop> <! -- Automatically generate a table (based on the object class). If create is set here, the table will be deleted every time the computer starts up and a new table will be created, therefore, the previous data will be cleared at this time. -->
<Prop key = "hibernate. hbm2ddl. auto"> update </prop>
</Props>
</Property>
<! -- Set the ing file. The ing price refers to the hbm. xml file. -->
<Property name = "mappingResources">
<List>
<Value> entity/BookCard. hbm. xml </value>
</List>
</Property>
</Bean>
<! -- Set the data source (use c3p0 to set the database source) and use spring to create an instance --> <bean id = "myDataSourceSql" class = "com. mchange. v2.c3p0. ComboPooledDataSource">
<! -- Variables are used here to allow technicians to use them for subsequent installation projects. To select different databases, You need to modify the basic information of the database. --> <Property name = "driverClass" value = "$ {jdbc. diver} "/> <property name =" jdbcUrl "value =" $ {jdbc. url} "/> <property name =" user "value =" $ {jdbc. user} "/> <property name =" password "value =" $ {jdbc. password} "/> <! -- Check all idle connections in the connection pool every 300 seconds --> <property name = "idleConnectionTestPeriod" value = "300"> </property> <! -- Maximum idle time. connections are discarded if they are not used within 900 seconds. If it is 0, it will never be discarded --> <property name = "maxIdleTime" value = "900"> </property> <! -- Max connections --> <property name = "maxPoolSize" value = "2"> </property> </bean> </beans>
The current hbm. xml file of the object class: (this example is: BookCard. hbm. xml). There is no difference with the previous one:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <property name="entity_prop_name" type="string"> <column name="column_name"></column> </property> </class>
The newly added database information file: (jdbcSql. properties), defines the basic information of the database, but your project needs to be deployed by the technical staff. You may need to modify the database information because of different situations.
Now, the ssh basic framework has been set up. You can add this framework to your project for practical use. Thank you for choosing!