1. web.xml 配置
1. <context-param>
2. <param-name>contextConfigLocation</param-name>
3. <param-value>
4. /WEB-INF/applicationContext.xml
5. <!--classpath*:/spring-config/applicationContext.xml-->
6. </param-value>
7. </context-param>
8. <!-- hibernate open session in view -->
9. <filter>
10. <filter-name>hibernateFilter</filter-name>
11. <filter-class>
12. org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
13. </filter-class>
14. </filter>
15.
16. <!-- 编码 -->
17. <filter>
18. <filter-name>encodingFilter</filter-name>
19. <filter-class>
20. org.springframework.web.filter.CharacterEncodingFilter
21. </filter-class>
22. <init-param>
23. <param-name>encoding</param-name>
24. <param-value>UTF-8</param-value>
25. </init-param>
26. <init-param>
27. <param-name>forceEncoding</param-name>
28. <param-value>true</param-value>
29. </init-param>
30. </filter>
31. <!-- hibernate open session in view -->
32. <filter-mapping>
33. <filter-name>hibernateFilter</filter-name>
34. <url-pattern>/*<url-pattern>
35. </filter-mapping>
36. <!-- 编码 -->
37. <filter-mapping>
38. <filter-name>encodingFilter</filter-name>
39. <url-pattern>*.jsp<url-pattern>
40. </filter-mapping>
41.
42. <listener>
43. <listener-class>
44. org.springframework.web.context.ContextLoaderListener
45. </listener-class>
46. </listener>
47.
48. <listener>
49. <listener-class>
50. org.springframework.web.context.request.RequestContextListener
51. </listener-class>
52. </listener>
53.
54. <!-- 要负责处理由 JavaBeans Introspector的使用而引起的缓冲泄露 -->
55. <listener>
56. <listener-class>
57. org.springframework.web.util.IntrospectorCleanupListener
58. </listener-class>
59. </listener>
2.applicationContext.xml 配置
对于dataSource就省略了 可以使用c3p0连接池
1. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
2. <property name="dataSource" ref="dataSource"/>
3. <property name="annotatedClasses" ref="annotatedClasses"/>
4. <property name="hibernateProperties" ref="hibernateProperties"/>
5. </bean>
6.
7. <bean name="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
8. <property name="properties">
9. <props>
10. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
11. <prop key="hibernate.show_sql">true</prop>
12. <prop key="hibernate.hbm2ddl.auto">none</prop>
13. <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
14. <prop key="hibernate.cache.use_query_cache">true</prop>
15. <prop key="hibernate.default_batch_fetch_size">30</prop>
16. </props>
17. </property>
18. </bean>
19.
20. <!-- annotation table 类 -->
21. <bean id="annotatedClasses" class="org.springframework.beans.factory.config.ListFactoryBean">
22. <property name="sourceList">
23. <list>
24. <value>com.bask.model.Promotion</value>
25. <value>com.bask.model.PromotionGroup</value>
26. <value>com.bask.model.Largess</value>
27. </list>
28. </property>
29. </bean>
30.
31. <!-- 事务 begin -->
32. <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
33. <property name="sessionFactory" ref="sessionFactory" />
34. </bean>
35.
36. <!-- 支持 @Transactional -->
37. <tx:annotation-driven/>
38.
39. <!-- 支持 @AspectJ -->
40. <aop:aspectj-autoproxy/>
41.
42. <!-- 以AspectJ方式定义AOP -->
43. <aop:config proxy-target-class="true">
44. <aop:advisor pointcut="execution(* com.bask.service.*Manager.*(..))" advice-ref="txAdvice"/>
45. </aop:config>
46.
47. <tx:advice id="txAdvice" transaction-manager="transactionManager">
48. <tx:attributes>
49. <!-- 以get 、 find 开头的方法是只读事务 -->
50. <tx:method name="get*" read-only="true" />
51. <tx:method name="find*" read-only="true" />
52. <!-- 其他方法是默认 -->
53. <tx:method name="save*" />
54. </tx:attributes>
55. </tx:advice>
56. <!-- 事务 end -->
57.
58. <context:annotation-config/>
59. <!-- service下是spring使用注解 -->
60. <context:component-scan base-package="com.bask.service"/>
3.说明
在这里我们将会根据class生产数据库表
自动创建|更新|验证数据库表结构
1. <prop key="hibernate.hbm2ddl.auto">none</prop>
2. <!-- auto 可以有create update and none 等-->
接下来是Hibernate Annotation使用笔记
1. @Entity --声明为一个实体bean
2. @Table(name="promotion_info") --为实体bean映射指定表(表名="promotion_info)
3. @Id --声明了该实体bean的标识属性
4. @GeneratedValue --可以定义标识字段的生成策略.
5. @Transient --将忽略这些字段和属性,不用持久化到数据库
6. @Column(name="promotion_remark")--声明列(字段名="promotion_total") 属性还包括(length=200等)
7. @Temporal(TemporalType.TIMESTAMP)--声明时间格式
8. @Enumerated --声明枚举
9. @Version --声明添加对乐观锁定的支持
10. @OneToOne --可以建立实体bean之间的一对一的关联
11. @OneToMany --可以建立实体bean之间的一对多的关联
12. @ManyToOne --可以建立实体bean之间的多对一的关联
13. @ManyToMany --可以建立实体bean之间的多对多的关联
14. @Formula --一个SQL表达式,这种属性是只读的,不在数据库生成属性(可以使用sum、average、max等)
以上是基本常用注解,下面是例子说明
1. @Entity
2. @Table(name="promotion_info")
3. public class Promotion implements Serializable {
4.
5. //AUTO--可以是identity类型的字段,或者sequence类型或者table类型,取决于不同的底层数据库
6. @Id
7. @GeneratedValue(strategy = GenerationType.AUTO)
8. private Long id;
9.
10. @Column(name="group_start_amount")
11. private Integer groupStartAmount=0;
12.
13. @Column(name="promotion_remark",length=200)
14. //@Lob 如果是文章内容可以使用 只需要把length=200去掉就可以了
15. private String remark;
16. //DATE - java.sql.Date
17. //TIME - java.sql.Time
18. //TIMESTAMP - java.sql.Timestamp
19. @Temporal(TemporalType.TIMESTAMP)
20. @Column(name="start_time")
21. private Date startTime;
22.
23. //显示0 隐藏1
24. public static enum DisplayType {
25. 显示,隐藏
26. }
27. @Enumerated(value = EnumType.ORDINAL)//ORDINAL序数
28. private DisplayType displayType = DisplayType.显示;
29.
30. @Version
31. private Integer version;
32.
33. //CascadeType.PERSIST -- 触发级联创建(create)
34. //CascadeType.MERGE -- 触发级联合并(update)
35. //FetchType.LAZY -- 延迟加载
36. @ManyToOne(cascade = {CascadeType.PERSIST,CascadeType.MERGE},fetch = FetchType.LAZY)
37. private PromotionGroup promotionGroup;
38.
39. //单向ManyToMany
40. //@JoinTable(关联的表名)
41. //joinColumns -- promotion关联的列的外键
42. //inverseJoinColumns -- largess 关联列的外键
43. @ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE})
44. @JoinTable(name="promotion_largess",joinColumns={@JoinColumn(name="promotion_id")},inverseJoinColumns={@JoinColumn(name="largess_id")})
45. private Set<Largess> largess;
46.
47. //get set 省略.
48.
49. }
50. @Entity
51. @Table(name="promotion_group")
52. public class PromotionGroup implements Serializable {
53. @Id
54. @GeneratedValue(strategy = GenerationType.AUTO)
55. private Long id;
56.
57. //mappedBy的值"promotionGroup"指向owner(Promotion)端的关联属性,并且是双向关系
58. @OneToMany(mappedBy="promotionGroup",cascade=CascadeType.ALL)
59. private List<Promotion> promotion;
60.
61. //get set 省略.
62. }
63. @Entity
64. @Table(name="largess")
65. public class Largess implements Serializable {
66. @Id
67. @GeneratedValue(strategy = GenerationType.AUTO)
68. private Long id;
69.
70. //1.sql语句中的字段和表名都应该和数据库相应,而不是类中的字段,
71. //若带有参数如la.id= id,这个=id才是类中属性
72. //2.操作字段一定要用别名
73. @Formula(select max(la.id) from largess as la)
74. private int maxId;
75.
76. @Formula(select COUNT(la.id) from largess la)
77. private int count;
78.
79. @Transient
80. private String img
81.
82. //get set 省略.
83. }
posted on 2008-08-31 00:36
lvq810 阅读(2072)
评论(0) 编辑 收藏 所属分类:
Open Framekwork