First, create the Hibernate entity mapping file.
Create a mapping file Product.hbm.xml in the package where the entity is located, introducing Hibernate's mapping constraints. (the constraint is located inside the Hibernate3.jar hibernate-mapping-3.0.dtd, can be cope used) such as:
Product.hbm.xml:
<?xml version= "1.0" encoding= "UTF-8"?><!--Hibernate constraint header information: hibernate3.jar→org, HIBERNATE→HIBERNATE-MAPPING-3.0.DTD--><! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
Second, using spring to introduce a property file to connect to the database
2.1 Create a new DBA connection properties file Jdbc.produce under Project SRC to hold the basic parameters of the database connection:
jdbc.driverclass=com.mysql.jdbc.driverjdbc.url=jdbc:mysql://localhost:3306/sshjdbc.username=rootjdbc.password= 123456
2.2 Use the Context:property-placeholder tag in Applicationcontext.xml to introduce a property file to prepare the parameters for the connection pool:
<!--introducing external properties Files--- <context:property-placeholder location= "Classpath:jdbc.produce"/>
2.3 Configuring connection pooling in Applicationcontext.xml: Assigning a connection pool using the Ingest profile property
<!--configuring connection pooling : Assigning a connection pool with the ingest profile property-- <bean id= "DataSource" class= " Com.mchange.v2.c3p0.ComboPooledDataSource "> <property name=" Driverclass "value=" ${jdbc.driverclass} " ></property> <property name= "Jdbcurl" value= "${jdbc.url}" ></property> <property Name= "User" value= "${jdbc.username}" ></property> <property name= "password" value= "${jdbc.password } "></property> </bean>
2.4 Configure hibernate properties and load mapping files in Applicationcontext.xml:
(Hibernate dialect location: hibernate-release-5.2.8.final\project\etc\hibernate.properties) such as:
Applicationcontext.xml Configuration Mapping: (Note the introduction of the hibernate version of Sessionfactory, according to its own hibernate version)
<!--Configure Hibernate related properties: Spring initiates the creation of a mapping table based on this configuration--<bean id= "sessionfactory" class= "org.springframework. Orm.hibernate3.LocalSessionFactoryBean "> <!--inject connection pool--<property name=" DataSource "ref=" Datasou Rce "></property> <!--Configure hibernate Properties--<property name=" Hibernateproperties "> &L t;props> <!--hibernate.dialect:hibernate dialect--<prop key= "Hibernate.dialect" >ORG.HIBERNATE.D Ialect. mysqldialect</prop> <prop key= "Hibernate.show_sql" >true</prop> <prop key= "hibernate.for Mat_sql ">true</prop> <prop key=" Hibernate.hbm2ddl.auto ">update</prop> </props> </property> <!--Configuring mapping files in Hibernate--<property name= "Mappingresources" > < list> <!--mapping file full path--<value>com/ssh/entity/product.hbm.xml</value> </LIST&G T </prOperty> </bean>
When you do this, when you start the project, the load Applicationcontext.xml when the system loads initialize spring automatically connects to the database and creates the corresponding tables in the database based on the configuration mappings.
Third, start the Project observation database
Run created successfully!
SSH (v) Spring integration hibernate