<?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:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd "><!--start the spring annotation configuration, but you still need to hand javabean to the spring IOC container for management--><!--<context: The annotation-config/>--><!--scans the following @component, @Repository, @Service, @Controller annotation-decorated classes and handing them over to the spring container for management. It also contains all the features of the Context:annotation-config configuration @component: It is not possible to differentiate this class from being decorated with this annotation @repository: used to decorate the database Access layer code (DAO) @ Service: Used to decorate the business Logic layer code @controller: code to decorate business process control above four annotations will give JavaBean a default name, the rule is the first letter of the class name lowercase but these four annotations in fact there is no difference at all-->< Context:component-scan base-package= "Com.zhidi"/><!--JavaBean to the spring container for management--><!-- Autowire-candidate= "False" will exclude the bean from the auto-injection list--><bean class= "Com.zhidi.dao.UserDao" autowire-candidate= "false"/><!--priamry=true, the bean will be the first injected object-- ><bean class= "Com.zhidi.dao.UserDao" primary= "true"/><bean class= "Com.zhidi.dao.UserDao"/><bean Id= "Userdao" class= "Com.zhidi.dao.UserDao"/><!--autowire= "Bytype" automatically assembled by type--><!--autowire= "ByName" Assemble by name--><bean class= "Com.zhidi.service.UserService" autowire= "byname" ><!--manually build dependent--><!--< Property Name= "Userdao" ref= "Userdao"/>--></bean><bean class= "Com.zhidi.dao.EmpDao" primary= "true"/ ><bean id= "Empdao" class= "Com.zhidi.dao.EmpDao"/><bean class= "Com.zhidi.service.EmpService"/>< Bean class= "Com.zhidi.dao.DeptDao"/><bean class= "Com.zhidi.service.DeptService"/><bean class= " Com.zhidi.TestLifeDemo "></bean><!--will create the Sessionfactory class to the spring IOC container for management--><bean class=" Org.springframework.orm.hibernate4.LocalSessionFactoryBean "><!--Configure the underlying properties of Hibernate-->≪property name= "hibernateproperties" ><props><prop key= "Hibernate.connection.driver_class" > Com.mysql.jdbc.driver</prop><prop key= "Hibernate.connection.url" >jdbc:mysql://localhost:3306/db_ Hibernate</prop><prop key= "Hibernate.connection.username" >root</prop><prop key= " Hibernate.connection.password ">root</prop><prop key=" Hibernate.dialect "> Org.hibernate.dialect.mysql5dialect</prop><prop key= "Hibernate.current_session_context_class" > Thread</prop><prop key= "Hibernate.show_sql" >true</prop><prop key= "Hibernate.format_sql" > true</prop></props></property><!--Configure the package to scan the entity class, configure it to sessionfactory--><property name= " Packagestoscan "value=" com.zhidi.entity "/></bean></beans>
Contents of the Applicationcontext.xml configuration file
Summary of steps:
1. Build a dependent jar
Spring must jar (6)
Hibernate must jar
Integration requires at least 3 jar,spring-jdbc.x.x to be imported, spring-tx.x.x,spring-orm.x.x
2. The goal is to create a sessionfactory
Manage Localsessionfactorybean to the spring IOC container
<bean class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean" ></bean>
3. Configuration of basic properties for hibernate
. hibernate.connection.driver_class
. Hibernate.connection.url. Hibernate.connection.username
. Hibernate.connection.password
. hibernate.dialect
. Hibernate.current_session_context_class. Hibernate.show_sql
. hibernate.format_sql
4. Scan the package for the entity class
Packagestoscan
5. Annotation Configuration for entity classes
6. Inject sessionfactory into the DAO layer
7. Create a Session object through Sessionfactory for database operation
Java framework: The spring Framework consolidates the XML configuration of the Hibernate framework (using annotations)