[b]一、Struts零配置: 需要struts2-convention-plugin-2.2.1.jar[/b]
1.1. struts2.properties: 配置convention插件参数
1.2. struts.xml配置拦截器 package包继承convention-default
[b]二、Spring注解\JPA注解\Hibernate注解[/b]
2.1. Spring*.xml中配置注解(组件扫描、hibernate sessionfactory注解、事务注解)
[b]三、注解使用[/b]
3.1. Spring注解使用
[list]
[*]DAO -- @Repository
[*]Service - @Service
[*]Action -- @Controller
[/list]
3.2. JPA(javax.persistence api)使用
[list]
[*]Pojo -- @Entity、@Table...
[/list]
----User.java
----Role.java
1.1. struts2.properties: 配置convention插件参数
/**
* locators.basePackage -- 扫描包根目录
* locators -- 包名关键字
* path -- 结果返回jsp目录 "/jsp/"等于"/webroot/jsp/"
*/
struts.convention.package.locators.basePackage=com.test.web
struts.convention.package.locators=action
struts.convention.result.path=/jsp/
1.2. struts.xml配置拦截器 package包继承convention-default
<package name="crud-default" extends="convention-default,json-default">
[b]二、Spring注解\JPA注解\Hibernate注解[/b]
2.1. Spring*.xml中配置注解(组件扫描、hibernate sessionfactory注解、事务注解)
<!-- Spring注解 扫描 -->
<context:component-scan base-package="com.test"/>
<!-- 配置sessionfactory -
---注意引用类class: AnnotationSessionFactoryBean;
---注意扫描属性property: packagesToScan -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
lazy-init="true">
<property name="dataSource" ref="testmysql"></property>
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy"></bean>
</property>
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
<prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
</props>
</property>
<property name="packagesToScan" value="com.test.pojo" />
<property name="eventListeners">
<map>
<entry key="save-update">
<list>
<bean class="org.hibernate.event.def.DefaultSaveOrUpdateEventListener" />
</list>
</entry>
</map>
</property>
</bean>
<!-- Transaction事务管理配置 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
lazy-init="default">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 声明式 事务
proxy-target-class=true 使用cglib动态代理不需要继承接口(默认jdk动态代理)
-->
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
[b]三、注解使用[/b]
3.1. Spring注解使用
[list]
[*]DAO -- @Repository
[*]Service - @Service
[*]Action -- @Controller
[/list]
3.2. JPA(javax.persistence api)使用
[list]
[*]Pojo -- @Entity、@Table...
[/list]
----User.java
package com.test.pojo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
/**
* User entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "user", catalog = "test")
public class User implements java.io.Serializable {
// Fields
/**
*
*/
private static final long serialVersionUID = 1780768557807417443L;
// Property accessors
@GenericGenerator(name = "generator", strategy = "increment")
@Id
@GeneratedValue(generator = "generator")
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "roleid", nullable = false)
private Role role;
@Column(name = "name", length = 30)
private String name;
@Column(name = "sex", length = 1)
private Character sex;
@Column(name = "enable")
private boolean enable;
// Constructors
/** default constructor */
public User() {
}
/** minimal constructor */
public User(Integer id) {
this.id = id;
}
/** 属性get/set略 */
}
----Role.java
package com.test.pojo;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
/**
* Role entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "role", catalog = "test")
public class Role implements java.io.Serializable {
// Fields
/**
*
*/
private static final long serialVersionUID = 6747382540556815448L;
@GenericGenerator(name = "generator", strategy = "increment")
@Id
@GeneratedValue(generator = "generator")
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@Column(name = "name", length = 30)
private String name;
@Column(name = "enbale")
private boolean enable;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "role")
private Set<User> users = new HashSet<User>(0);
/**
* get/set略
*/
}