<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 开启Spring注解配置,但是还是需要将JavaBean手动交给Spring IOC容器进行管理 --> <!-- <context:annotation-config/> --> <!-- 会扫描以下@Component,@Repository,@Service,@Controller注解修饰的类,将其交给Spring容器进行管理 , 还包含context:annotation-config配置的所有功能 @Component:无法区分该类为那一层操作时用该注解修饰 @Repository:用于修饰数据库访问层代码(DAO) @Service:用于修饰业务逻辑层代码 @Controller:用于修饰业务流程控制的代码 以上四个注解会给JavaBean默认一个名字,规则是将类名的首字母小写 但是这四个注解其实在根本没有啥区别 --> <context:component-scan base-package="com.zhidi"/> <!-- 将JavaBean交给Spring容器进行管理 --> <!-- autowire-candidate="false" 会将该bean排除到自动注入列表之外 --> <bean class="com.zhidi.dao.UserDao" autowire-candidate="false" /> <!-- priamry=true,会将该bean做为优先注入的对象 --> <bean class="com.zhidi.dao.UserDao" primary="true"/> <bean class="com.zhidi.dao.UserDao" /> <bean id="userDao" class="com.zhidi.dao.UserDao" /> <!-- autowire="byType" 按照类型进行自动装配 --> <!-- autowire="byName" 按照名称进行装配 --> <bean class="com.zhidi.service.UserService" autowire="byName"> <!-- 手动构建依赖 --> <!-- <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> <!-- 将创建SessionFactory的类交给Spring IOC容器进行管理 --> <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 对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> <!-- 配置扫描实体类所在包,将其配置到SessionFactory中 --> <property name="packagesToScan" value="com.zhidi.entity"/> </bean> </beans>
applicationContext.xml配置文件的内容
步骤总结:
1.构建依赖jar
Spring必须jar(6个)
Hibernate必须jar
整合最少还需要导入3个jar,spring-jdbc.x.x ,spring-tx.x.x,spring-orm.x.x
2.目的是要创建SessionFactory
将LocalSessionFactoryBean交给Spring IOC容器进行管理
<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"></bean>
3.对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.扫描实体类所在包
packagesToScan
5.对实体类进行注解配置
6.将SessionFactory注入到Dao层
7.通过SessionFactory创建Session对象,进行数据库操作