zoukankan      html  css  js  c++  java
  • 注解的力量 Spring 2.5 JPA hibernate 使用方法的点滴整理(一):消除hibernate中<mapping resouce 的xxxx. hbm.xml文件

    以下几篇文章简单的介绍一下jpa 和 spring2.5 hibernate3.2 整合配置的一个过程。纯粹个人经验只谈。如果有错误,请各位留言指出。
     
    本系列重点是涉及 配置过程 ,对注释的用法不多介绍。
     
    注释语法越来越多的被业界所使用,并且注释配置相对于 XML 配置具有很多的优势:它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作。注释和 Java 代码位于一个文件中,而 XML 配置采用独立的配置文件,大多数配置信息在程序开发完成后都不会调整,如果配置信息和 Java 代码放在一起,有助于增强程序的内聚性。而采用独立的 XML 配置文件,程序员在编写一个功能时,往往需要在程序文件和配置文件中不停切换,这种思维上的不连贯会降低开发效率。因此在很多情况下,注释配置比 XML 配置更受欢迎,注释配置有进一步流行的趋势。Spring 2.5 的一大增强就是引入了很多注释类,现在您已经可以使用注释配置完成大部分 XML 配置的功能。
     

    首先,我们已经通过 传统的spring +hibernate方式构架成功了一个应用的后台体系。

    这个体系里面 有这样几个重要的配置文件。
    1. hibernate.cfg.xml 。
      里面通过 配置 mapping来指向每张数据表单生成配置文件.xxxx.hbm.xml文件
    2. applicaitonContex.xml。
      里面通过定义一个一个bean 来配置 各个需要用到的 DAO 和 Service。
      1. <?xml version="1.0" encoding="UTF-8"?>
      2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
      3. <beans >
      4.     <bean id="sessionFactory"
      5.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      6.         <property name="configLocation"
      7.             value="classpath:hibernate.cfg.xml">
      8.         </property>
      9.     </bean>
      10.     
      11.     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      12.         <property name="sessionFactory"
      13.             <ref local="sessionFactory"/>
      14.         </property> 
      15.     </bean>
      16.     <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
      17.         <!--  事务拦截器bean需要依赖注入一个事务管理器 -->
      18.         <property name="transactionManager">
      19.             <ref local="transactionManager"/>
      20.         </property>
      21.         <property name="transactionAttributes">
      22.             <!--  下面定义事务传播属性-->
      23.             <props>
      24.                 <prop key="insert*">PROPAGATION_REQUIRED</prop>
      25.                 <prop key="update*">PROPAGATION_REQUIRED</prop>
      26.                 <prop key="save*">PROPAGATION_REQUIRED</prop>
      27.                 <prop key="add*">PROPAGATION_REQUIRED</prop>
      28.                 <prop key="update*">PROPAGATION_REQUIRED</prop>
      29.                 <prop key="remove*">PROPAGATION_REQUIRED</prop>
      30.                 <prop key="delete*">PROPAGATION_REQUIRED</prop>
      31.                 <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
      32.                 <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
      33.                 <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
      34.                 <prop key="change*">PROPAGATION_REQUIRED</prop>
      35.                 <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
      36.             </props>
      37.         </property>
      38.     </bean>
      39.     
      40.     
      41. <!-- 定义自动代理BeanNameAutoProxyCreator -->
      42. <bean id="beanNameAutoProxyCreator"
      43. class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
      44. <!--  指定对满足哪些bean name的bean自动生成业务代理 -->
      45. <property name="beanNames">
      46.       <list>
      47.        <value>*Service</value>
      48.       </list>
      49.       </property>
      50. <!--  下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
      51. <property name="interceptorNames">
      52. <list>
      53. <!-- 此处可增加其他新的Interceptor -->
      54. <value>transactionInterceptor</value>
      55. </list>
      56. </property>
      57.         </bean>            
      58.     <bean id="McCityInfoDAO"
      59.         class="com.firemax.manatee.hibernate.McCityInfoDAO">
      60.         <property name="sessionFactory">
      61.             <ref bean="sessionFactory" />
      62.         </property>
      63.     </bean>
      64.     <bean id="McMaterialInfoDAO"
      65.         class="com.firemax.manatee.hibernate.McMaterialInfoDAO">
      66.         <property name="sessionFactory">
      67.             <ref bean="sessionFactory" />
      68.         </property>
      69.     </bean>
      70. </beans>
      hibernate.cfg.xml要配置这么多 xxxxx.hbm.xml文件。每次数据结构发生变化的时候。要重新去改写pojo和dao以及这些xxxxx.hbm.xml
      那么好。我们现在就用 注解的力量 去把这部分工作简化。
    • 首先我们需要 
    1. hibernate3.2 以上版本的jar
    2. jdk 5 以上的环境
    3. spring2 
    • 然后我们修改pojo的java类。加上注解。使起通过注解来取代原先要xxxx.hbm.xml里面配置的指向的数据库表单结构的信息。
    1. package com.alcor.web.hibernate;
    2. import java.util.HashSet;
    3. import java.util.Set;
    4. import javax.persistence.CascadeType;
    5. import javax.persistence.Column;
    6. import javax.persistence.Entity;
    7. import javax.persistence.FetchType;
    8. import javax.persistence.Id;
    9. import javax.persistence.JoinColumn;
    10. import javax.persistence.ManyToOne;
    11. import javax.persistence.OneToMany;
    12. import javax.persistence.Table;
    13. /**
    14.  * AlcorTCitys entity. @author MyEclipse Persistence Tools
    15.  */
    16. @Entity
    17. @Table(name = "alcor_t_citys", catalog = "alcorweb")
    18. public class AlcorTCitys implements java.io.Serializable {
    19.     // Fields
    20.     private String cityCode;
    21.     private AlcorTProvinces alcorTProvinces;
    22.     private String cityName;
    23.     private Set<AlcotTDistrict> alcotTDistricts = new HashSet<AlcotTDistrict>(0);
    24.     // Constructors
    25.     /** default constructor */
    26.     public AlcorTCitys() {
    27.     }
    28.     /** minimal constructor */
    29.     public AlcorTCitys(String cityCode, AlcorTProvinces alcorTProvinces,
    30.             String cityName) {
    31.         this.cityCode = cityCode;
    32.         this.alcorTProvinces = alcorTProvinces;
    33.         this.cityName = cityName;
    34.     }
    35.     /** full constructor */
    36.     public AlcorTCitys(String cityCode, AlcorTProvinces alcorTProvinces,
    37.             String cityName, Set<AlcotTDistrict> alcotTDistricts) {
    38.         this.cityCode = cityCode;
    39.         this.alcorTProvinces = alcorTProvinces;
    40.         this.cityName = cityName;
    41.         this.alcotTDistricts = alcotTDistricts;
    42.     }
    43.     // Property accessors
    44.     @Id
    45.     @Column(name = "city_code", unique = true, nullable = false, length = 32)
    46.     public String getCityCode() {
    47.         return this.cityCode;
    48.     }
    49.     public void setCityCode(String cityCode) {
    50.         this.cityCode = cityCode;
    51.     }
    52.     @ManyToOne(fetch = FetchType.EAGER)
    53.     @JoinColumn(name = "province_code", nullable = false)
    54.     public AlcorTProvinces getAlcorTProvinces() {
    55.         return this.alcorTProvinces;
    56.     }
    57.     public void setAlcorTProvinces(AlcorTProvinces alcorTProvinces) {
    58.         this.alcorTProvinces = alcorTProvinces;
    59.     }
    60.     @Column(name = "city_name", nullable = false, length = 64)
    61.     public String getCityName() {
    62.         return this.cityName;
    63.     }
    64.     public void setCityName(String cityName) {
    65.         this.cityName = cityName;
    66.     }
    67.     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "alcorTCitys")
    68.     public Set<AlcotTDistrict> getAlcotTDistricts() {
    69.         return this.alcotTDistricts;
    70.     }
    71.     public void setAlcotTDistricts(Set<AlcotTDistrict> alcotTDistricts) {
    72.         this.alcotTDistricts = alcotTDistricts;
    73.     }
    74. }

    • 修改hibernate.cfg.xml中的定义方式,把原来的maping source=“xxxxx.hbm.xml”修改成
    <mapping class="com.alcor.web.hibernate.AlcorTCitys" />

    这样我们就可以把原来的 xxxxx.hbm.xml全部删除了。
    经过这个步骤。如果你原有的servcice层的功能能够正常使用。恭喜你。迈出了成功的第一步。
     
  • 相关阅读:
    阿里云 k8s 部署 Spring Cloud Alibaba 微服务实践 (四) 自动化部署
    阿里云 k8s 部署 Spring Cloud Alibaba 微服务实践 (三) 服务观测
    阿里云 k8s 部署 Spring Cloud Alibaba 微服务实践 (二) 部署微服务程序
    阿里云 k8s 部署 Spring Cloud Alibaba 微服务实践 (一) 部署 Nacos
    C++知识点
    libmkl 学习笔记
    基于tesseract-OCR进行中文识别
    poco编译与运行
    Linux下的I/O复用与epoll详解(转载)
    高并发网络编程之epoll详解(转载)
  • 原文地址:https://www.cnblogs.com/winkey4986/p/2354513.html
Copyright © 2011-2022 走看看