本demo本人测试,完全可以。希望给大家一点参考。
文章链接太多,于是我删掉许多。
完整demo下载地址:
http://download.csdn.net/detail/zwz568017880/7982849
直接贴代码。
package cn.ztz.model;
@Entity
@Table(name="USERS")
public class Users {
private int id;
private String name;
private String password;
private String status;
private Date createTime;
private Set<UsersType> usersType=new HashSet<UsersType>();
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment",strategy="increment")
@Column(name="ID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="PASSWORD")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name="STATUS")
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name="CREATETIME")
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@OneToMany(cascade=CascadeType.REMOVE,mappedBy="usersId",fetch=FetchType.LAZY)
public Set<UsersType> getUsersType() {
return usersType;
}
public void setUsersType(Set<UsersType> usersType) {
this.usersType = usersType;
}
}
package cn.ztz.model;
@Entity
@Table(name="USERS_TYPE")
public class UsersType {
private int id;
private String nameType;
private Users usersId;
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment",strategy="increment")
@Column(name="ID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="NAMETYPE")
public String getNameType() {
return nameType;
}
public void setNameType(String nameType) {
this.nameType = nameType;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="USERS_ID")
public Users getUsersId() {
return usersId;
}
public void setUsersId(Users usersId) {
this.usersId = usersId;
}
}
package cn.ztz.dao;
import java.util.List;
import cn.ztz.model.Users;
public interface IndexDao {
public List<Users> findAll();
}
package cn.ztz.dao.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.stat.Statistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import cn.ztz.dao.IndexDao;
import cn.ztz.model.Users;
@Repository
public class IndexDaoImpl implements IndexDao {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
private Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
@Override
public List<Users> findAll() {
Query query=this.getCurrentSession().createQuery("from Users");
return query.list();
}
/**
* 继承HibernateDaoSupport用下面这个方法
*/
/*@Autowired
public void setSessionFactoryOverride(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
@Override
public List<Users> findAll() {
return (List<Users>) this.getHibernateTemplate().find("from Users");
}*/
}
package cn.ztz.service;
import java.util.List;
import cn.ztz.model.Users;
public interface IndexService {
public List<Users> findAll();
}
package cn.ztz.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.ztz.dao.IndexDao;
import cn.ztz.model.Users;
import cn.ztz.service.IndexService;
@Service
public class IndexServiceImpl implements IndexService {
@Autowired
private IndexDao indexDao;
public void setIndexDao(IndexDao indexDao) {
this.indexDao = indexDao;
}
@Override
public List<Users> findAll() {
return indexDao.findAll();
}
}
package cn.ztz.action;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.struts2.interceptor.RequestAware;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ztz.model.Users;
import cn.ztz.model.UsersType;
import cn.ztz.service.IndexService;
import com.opensymphony.xwork2.ActionSupport;
public class IndexAction extends ActionSupport implements RequestAware {
@Autowired
private IndexService indexService;
private Map<String,Object> request;
public void setIndexService(IndexService indexService) {
this.indexService = indexService;
}
public String index()throws Exception{
System.out.println("进来了");
List<Users> u=indexService.findAll();
request.put("users", u);
System.out.println(u);
for (Users users : u) {
System.out.println(users.getName());
Set<UsersType> uType=users.getUsersType();
System.out.println(uType);
}
return SUCCESS;
}
@Override
public void setRequest(Map<String, Object> arg0) {
this.request=arg0;
}
}
struts2配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 修改struts2拦截路径后缀名 -->
<constant name="struts.action.extension" value="html"/>
<package name="demo_ssh" extends="struts-default">
<!-- 全局默认配置 -->
<global-results>
<result name="error">/error.jsp</result>
<result name="login" type="redirect">/login.jsp</result>
<result name="500">/500.jsp</result>
<result name="404">/404.jsp</result>
</global-results>
<action name="index" class="cn.ztz.action.IndexAction" method="index">
<result>/index.jsp</result>
</action>
</package>
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<!-- 加载数据库属性配置文件 -->
<context:property-placeholder location="WEB-INF/config/application.properties" />
<!-- 数据库连接池c3p0配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="jdbcUrl" value="${driverUrl}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="maxPoolSize" value="40"></property>
<property name="minPoolSize" value="1"></property>
<property name="initialPoolSize" value="1"></property>
<property name="maxIdleTime" value="20"></property>
<property name="acquireIncrement" value="5"></property>
</bean>
<!-- session工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描注解方式配置的hibernate类文件 -->
<property name="packagesToScan">
<list>
<value>cn.ztz.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- 二级缓存
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
开启查询缓存
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.net.sf.ehcache.configurationResourceName">/ehcache.xml</prop>
强制Hibernate以更人性化的格式将数据存入二级缓存
<prop key="hibernate.cache.use_structured_entries">true</prop>
Hibernate将收集有助于性能调节的统计数据
<prop key="hibernate.generate_statistics">true</prop>-->
</props>
</property>
</bean>
<!-- 事务配置 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 需要事务控制的业务方法配置 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="select*" read-only="true" />
<tx:method name="count*" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<!-- 事务控制拦截器 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="txMethod" expression="execution(* cn.ztz.service.impl.*Impl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txMethod"/>
</aop:config>
<!-- 自动扫描包 -->
<context:annotation-config />
<context:component-scan base-package="cn.ztz.*" annotation-config="true"/>
</beans>