一、spring配置文件
<?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:jee="http://www.springframework.org/schema/jee" 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-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-lazy-init="true"> <description>Spring公共配置文件</description> <!-- 属性文件读入 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:config/jdbc.properties</value> </list> </property> </bean> <!-- 使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 --> <context:component-scan base-package="com.xinghuo.zfsjgx" /> <!-- 使用annotation定义事务 --> <!-- 支持 @Transactional 标记 Enable @Transactional support --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- Hibernate配置 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:hibernate.cfg.xml"/> <property name="namingStrategy"> <bean class="org.hibernate.cfg.ImprovedNamingStrategy" /> </property> <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.use_sql_comments">${hibernate.use_sql_comments}</prop> <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop> <!-- --> <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop> <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop> <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop> <prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache-hibernate.xml</prop> </props> </property> <!-- --> <property name="eventListeners"> <map> <entry key="save-update" value-ref="saveUpdateEventListener" /> </map> </property> <!-- <entry key="pre-insert" value-ref="vipEntityPrePersistListener" /> <entry key="pre-update" value-ref="vipEntityPreUpdateListener" /> <property name="annotatedClasses"> <list> <value>com.xinghuo.examples.miniweb.entity.user.User</value> <value>com.xinghuo.examples.miniweb.entity.user.Role</value> <value>com.xinghuo.examples.miniweb.entity.user.Authority</value> </list> </property> --> </bean> <bean id="saveUpdateEventListener" class="com.xinghuo.zfsjgx.dao.VipEntityListener" ></bean> <!-- 事务配置 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> </beans>
二、manager
package com.xinghuo.zfsjgx.service.sys;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xinghuo.zfsjgx.dao.sys.SzPcsDAO;
import com.xinghuo.zfsjgx.entity.sys.SzPcs;
/**
* SzPcsManager.java
* 用@Transactional注明类里的每个方法都用事务的,对只读方法注明readOnly=true加强性能.
* AutoGenerated by XMP 1.2
* 2013-8-13 14:14:48
* @author
* @see SzPcsDAO
*
*/
@Service
@Transactional
public class SzPcsManager {
/** TODO 如果是另外一种方式,请用DAO ,用此方式去在spring的配置文件中设置
private SimpleHibernateTemplate<SzPcs, String> szPcsDAO;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
szPcsDAO = new SimpleHibernateTemplate<SzPcs, String>(sessionFactory, SzPcs.class);
}
*/
private SzPcsDAO szPcsDAO;
@Autowired
public void setSzPcsDAO(SzPcsDAO szPcsDAO) {
this.szPcsDAO = szPcsDAO;
}
//SzPcsDAO
@Transactional(readOnly = true)
public SzPcs getSzPcs(String id) {
return szPcsDAO.get(id);
}
@Transactional(readOnly = true)
public List<SzPcs> getAllSzPcss() {
return szPcsDAO.findAll();
}
public void deleteSzPcs(String id) {
SzPcs szPcs = szPcsDAO.get(id);
szPcsDAO.delete(szPcs);
}
public void deleteSzPcs(SzPcs szPcs) {
szPcsDAO.delete(szPcs);
}
public void saveSzPcs(SzPcs szPcs) {
szPcsDAO.save(szPcs);
}
public List<SzPcs> getSzPcsList(){
return szPcsDAO.getSzPcsList();
}
}
三、bean
package com.xinghuo.zfsjgx.entity.sys;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import com.xinghuo.zfsjgx.entity.VipEntity;
/**
* SysUser entity.
*/
@SuppressWarnings("serial")
@Entity
@Table(name = "SYS_USER", uniqueConstraints = {})
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class SysUser extends VipEntity implements java.io.Serializable {
// Fields
private String userId;
private String userPassword;
private SysPerson sysPerson;
// Constructors
/** default constructor */
public SysUser() {
}
/** minimal constructor */
public SysUser(String userId) {
this.userId = userId;
}
// Property accessors
@Id
// @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqSysUser")
// @SequenceGenerator(name = "seqSysUser", sequenceName = "SEQ_SYS_USER", allocationSize = 1)
@Column(name = "USER_ID", unique = true, nullable = false, insertable = true, updatable = true, length = 20)
public String getUserId() {
return this.userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
@Column(name = "USER_PASSWORD", unique = false, nullable = true, insertable = true, updatable = true, length = 20)
public String getUserPassword() {
return this.userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
@JoinColumn(name = "PERSON_ID", unique = false, nullable = true, insertable = true, updatable = true)
public SysPerson getSysPerson() {
return sysPerson;
}
public void setSysPerson(SysPerson sysPerson) {
this.sysPerson = sysPerson;
}
}