Spring集成Hibernate(基于XML和注解配置)

配置Hibernate

<?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"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- 开启Spring IOC的注解扫描 -->
    <context:component-scan base-package="com.smart"/>

    <!-- 指定property文件位置 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 配置DBCP数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close"
          p:driverClassName="${jdbc.driverClassName}"
          p:url="${jdbc.url}"
          p:username="${jdbc.username}"
          p:password="${jdbc.password}"
    />

    <!-- 配置Hibernate的SeesionFactory -->
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"

        <!-- 指定引用的数据源 -->
        p:dataSource-ref="dataSource">

        <!-- 自动扫描指定包下的实体类, 减少映射配置文件的书写 -->
        <property name="packagesToScan" value="com.smart.domain"/>

        <!-- 配置Hibernate的控制属性 -->
        <property name="hibernateProperties">
            <props>

                <!-- 指明实际数据库类型 -->
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>

                <!-- 显示当前执行的sql语句 -->
                <prop key="hibernate.show_sql">
                    true
                </prop>

            </props>
        </property>
    </bean>

    <!-- 配置HiberanteTemplate对象 -->
    <bean id="hibernateTemplate"
          class="org.springframework.orm.hibernate4.HibernateTemplate"
          p:sessionFactory-ref="sessionFactory"/>

    <!-- 配置Hibernate的事务管理器 -->
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager"
          p:sessionFactory-ref="sessionFactory"/>

     <!-- 配置开启注解事务处理 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

使用注解配置实体类(域对象和数据库表的映射关系)

/***
 * hibernate对实体类的要求
 * (1)属性必须是private 且有public的get和set方法
 * (2)必须有唯一标识属性来表示实体类
 * (3)属性的类型建议用包装类型
 * hibernate实体类有三种状态:
 * (1)瞬时态:没有id,与session没有关系
 * (2)持久态:有id,与session有关系
 * (3)托管太:有id,和session无关
 */
package com.smart.domain;

@Entity
@Table (name = "user")
public class User {
    /**
     * id作为主键,主键有很多生成策略
     * (1)native自增长:根据选择的数据库自动选择主键的生成方式
     * (2)uuid:字段用String类型
     * (3)identity 需要数据库支持自增长,mysql支持,oracle不支持
     */
    @Id
    @GeneratedValue
    @Column (name = "id")
    private int id;

    @Column (name = "name", nullable = false, length = 100)
    private String name;

    @Column (name = "age", nullable = false)
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

参考

spring中集成hibernate
Spring整合Hibernate

原文地址:https://www.cnblogs.com/weixia-blog/p/12305482.html

时间: 2024-08-12 16:37:22

Spring集成Hibernate(基于XML和注解配置)的相关文章

尚硅谷Spring整合Hibernate基于xml配置

描述:这是一个最简单网上书城demo. 下载地址:http://download.csdn.net/detail/u013488580/8370899 1. Spring 整合 Hibernate 整合什么 ? 1). 有 IOC 容器来管理 Hibernate 的 SessionFactory 2). 让 Hibernate 使用上 Spring 的声明式事务 2. 整合步骤: 1). 加入 hibernate ①. jar 包 ②. 添加 hibernate 的配置文件: hibernate

基于javaConfig和注解配置Spring Security

Spring Security 借助一系列Servlet Filter 来提供安全性功能,但是借助Spring的小技巧,我们只需要配置一个Filer就可以了,DelegatingFilterProxy是一个特殊的Servlet Filter,它本身所做的工作并不多,只是将工作委托给一个javax.servlet.Filter 的实现类,这个实现类作为一个bean注册再Spring应用的上下文中. 如果了解过用xml配置spring security的朋友就知道,用基于xml配置Spring Se

Spring : 基于XML Schema的配置(一)

[本教程翻译自Spring官方文档,并有适当增删] (是针对Spring 4.0.6 Release版本的) 基于XML Schema的配置在Spring 2.0开始被引入,并在2.5和3.0版本得到增强和扩展. 转向基于XML Schema的动机是使得Spring XML配置更简单.传统的基于 <bean/>的方法是很好,但它的通用特性带来了很大的配置开销. 从Spring 依赖注入容器的观点来看,一切都是bean.这对Spring 容器是个好消息,因为如果一切都是bean,那么一对象都能以

Spring 框架的概述以及Spring中基于XML的IOC配置

Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器.框架.一站式 优势: 方便解耦:做到编译期不依赖,运行期才依赖 AOP的支持 声明式事务的支持 方便程序的测试 方便整合各种框架 降低JavaEE API的使用难度 Spring源码很厉害 解耦: 耦合包括:类之间的和方法之间的 解决的思路: 在创建对象的时候用反射来创建,而不是new 读取配置文件

基于XML的AOP配置

创建spring的配置文件并导入约束 此处要导入aop的约束 <?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:aop="http://

spring 系列6 基于xml的aop

spring中基于xml的AOP配置步骤 把通知Bean交给spring来管理 使用aop:config标签表明AOP配置 使用aop:aspect标签表明配置切面 id属性:给切面提供一个唯一标识 ref属性:指定通知类bean的id 4.在aop:aspect标签的内部使用对应标签来配置通知的类型 切入点表达式关键字execution(表达式): 访问修饰符 返回类型 包名.包名...类名.方法名(参数列表) 其中: 访问修饰符可以省略 标准表达式写法:public void com.man

NHibernate支持的数据库 NHibernate连接配置 hibernate.cfg.xml中的配置

使用下列数据库时hibernate.cfg.xml中的配置 Microsoft SQL Server 2005/2000 配置如下: <?xml version="1.0" ?>  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >      <session-factory>          <property name="co

xml配置spring集成hibernate

<?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/sch

spring--声明式事务(包含基于注解和基于xml文件的配置方式)

一.基于注解 步骤如下: 引入jar(mysql驱动,c3p0数据源,spring的必要jar) applicationContext.xml的配置 Service和Dao的类上都加上对应的注解使其在ioc容器中,service的方法上面加上注解@Transactional applicationContext.xml的配置: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt