Spring3.0+Struts2.2+Hibernate3.6+ExtJS3.2.0+DWR框架 整合一

一、系统构建
1、数据持久层用Hibernate注释,数据库表根据配置自动生成。
@Entity
@Table(name = "t_user")
public class User {

private int id;
private String user_code;
private String pwd;// 用户密码
private String name;// 用户姓名
private String sex;// 用户性别
private String email;// 用户email
private int creator_id;// 用户创建者id
private Date created_dt = new Date();// 创建日期
private int del_flag;// 删除标志

private Popedom popedom;// 身份对象
private Department department;// 部门对象

/*
* @Id
*
* @GenericGenerator(name = "idGenerator", strategy = "uuid") //
* 这个是hibernate的注解
*
* @GeneratedValue(generator = "idGenerator") // 使用uuid的生成策略
*/
@Id
@GeneratedValue
public int getId() {
return id;
}

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

@Column(unique = true)
public String getUser_code() {
return user_code;
}

public void setUser_code(String user_code) {
this.user_code = user_code;
}

public String getName() {
return name;
}

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

public String getPwd() {
return pwd;
}

public void setPwd(String pwd) {
this.pwd = pwd;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public int getCreator_id() {
return creator_id;
}

public void setCreator_id(int creator_id) {
this.creator_id = creator_id;
}

public int getDel_flag() {
return del_flag;
}

public void setDel_flag(int del_flag) {
this.del_flag = del_flag;
}

@ManyToOne(cascade = { CascadeType.REFRESH })
public Popedom getPopedom() {
return popedom;
}

public void setPopedom(Popedom popedom) {
this.popedom = popedom;
}

@ManyToOne(cascade = { CascadeType.REFRESH })
public Department getDepartment() {
return department;
}

public void setDepartment(Department department) {
this.department = department;
}

public Date getCreated_dt() {
return created_dt;
}

public void setCreated_dt(Date created_dt) {
this.created_dt = created_dt;
}
}
/**
* 权限类
*
* @author
*/
@Entity
@Table(name = "t_popedom")
public class Popedom {
private int id;// id
private String status;// 身份
private Set<User> users = new HashSet<User>();// 属于该身份的成员
@Id
@GeneratedValue
public int getId() {
return id;
}

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

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}
@OneToMany(mappedBy="popedom")
public Set<User> getUsers() {
return users;
}

public void setUsers(Set<User> users) {
this.users = users;
}

}
/**
* 部门类
*
* @author
*/
@Entity
@Table(name = "t_department")
public class Department {
private Integer id;// 部门id
private String name;// 部门名称
private String description;// 部门描述

private User director;// 部门主管




private Set<User> users = new HashSet<User>();// 部门成员

@Id
@GeneratedValue
public Integer getId() {
return id;
}

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

public String getName() {
return name;
}

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

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
@ManyToOne
public User getDirector() {
return director;
}

public void setDirector(User director) {
this.director = director;
}
@OneToMany(mappedBy = "department")
public Set<User> getUsers() {
return users;
}

public void setUsers(Set<User> users) {
this.users = users;
}

public String toString() {
return name;
}
}
2、WEB-INF目录下applicationContext.xml相关配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Spring配置文件的Schema信息 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.hanz" />

<!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"
/> <property name="url" value="jdbc:mysql://localhost:3306/spring" /> <property
name="username" value="root" /> <property name="password" value="bjsxt" />
</bean> -->

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>

<!-- 定义数据源Bean,使用C3P0数据源实现 -->
<!-- 设置连接数据库的驱动、URL、用户名、密码 连接池最大连接数、最小连接数、初始连接数等参数 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" p:driverClass="${jdbc.driverClassName}"
p:jdbcUrl="${jdbc.url}" p:user="${jdbc.username}" p:password="${jdbc.password}"
p:maxPoolSize="40" p:minPoolSize="1" p:initialPoolSize="1"
p:maxIdleTime="20" />


<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- <property name="annotatedClasses"> <list> <value>com.bjsxt.model.User</value>
<value>com.bjsxt.model.Log</value> </list> </property> -->
<property name="packagesToScan" value="com.hanz.domain"/>


<!-- 定义Hibernate的SessionFactory的属性 -->
<property name="hibernateProperties">
<!-- 指定数据库方言、是否自动建表 是否生成SQL语句等 -->
<value>
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true
#开启二级缓存
hibernate.cache.use_second_level_cache=true
#设置二级缓存的提供者
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
</property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>


<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 配置事务拦截器 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>


</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值