spring3.1+struts2+hibernate4整合

Struts2.3+Spring3.1+hibernate4 整合
  • 博客分类:
  • java

  1. 准备工作

struts2
spring3.1
hibernate4
数据库,连接池

2.配置文件

web.xml

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!-- 
  3.      Licensed to the Apache Software Foundation (ASF) under one or more 
  4.       contributor license agreements.  See the NOTICE file distributed with 
  5.       this work for additional information regarding copyright ownership. 
  6.       The ASF licenses this file to You under the Apache License, Version 2.0 
  7.       (the "License"); you may not use this file except in compliance with 
  8.       the License.  You may obtain a copy of the License at 
  9.  
  10.           http://www.apache.org/licenses/LICENSE-2.0 
  11.  
  12.       Unless required by applicable law or agreed to in writing, software 
  13.       distributed under the License is distributed on an "AS IS" BASIS, 
  14. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.       See the License for the specific language governing permissions and 
  16.       limitations under the License. 
  17. --> 
  18. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  19.     xmlns="http://java.sun.com/xml/ns/javaee"  
  20.     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  21.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  22.     metadata-complete="true" version="3.0"> 
  23.     <!--Spring--> 
  24.     <context-param> 
  25.         <param-name>contextConfigLocation</param-name> 
  26.         <param-value>  
  27.             classpath*:applicationContext*.xml 
  28.             classpath*:*Context.xml 
  29.         </param-value> 
  30.     </context-param> 
  31.     <listener> 
  32.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  33.     </listener> 
  34.  
  35.     <!-- 配置spiring security --><!-- 
  36.     <filter> 
  37.         <filter-name>springSecurityFilterChain</filter-name> 
  38.         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
  39.     </filter> 
  40.     <filter-mapping> 
  41.         <filter-name>springSecurityFilterChain</filter-name> 
  42.         <url-pattern>/*</url-pattern> 
  43.     </filter-mapping>--> 
  44.     <!-- 配置spiring security结束 --> 
  45.  
  46.     <filter> 
  47.         <filter-name>struts2</filter-name> 
  48.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
  49.     </filter> 
  50.     <filter-mapping> 
  51.         <filter-name>struts2</filter-name> 
  52.         <url-pattern>*.action</url-pattern> 
  53.     </filter-mapping> 
  54.     <!--OpenSessionInViewFilter--> 
  55.     <filter> 
  56.          <filter-name>openSessionInViewFilter</filter-name> 
  57.          <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> 
  58.          <init-param> 
  59.              <param-name>sessionFactoryBeanName</param-name> 
  60.              <param-value>sessionFactory</param-value> 
  61.          </init-param> 
  62.      </filter> 
  63.      <filter-mapping> 
  64.          <filter-name>openSessionInViewFilter</filter-name> 
  65.          <url-pattern>/*</url-pattern> 
  66.      </filter-mapping>     
  67.  
  68.     <welcome-file-list> 
  69.         <welcome-file>index.jsp</welcome-file> 
  70.     </welcome-file-list> 
  71. </web-app> 

Struts.xml

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2. <!DOCTYPE struts PUBLIC 
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  5. <struts> 
  6.     <constant name="struts.enable.DynamicMethodInvocation" value="false" /> 
  7.     <constant name="struts.devMode" value="false" /> 
  8.  
  9.      
  10.     <!-- 交给spring管理 --> 
  11.     <constant name="struts.objectFactory" value="spring" /> 
  12.     <!--国际化语言文件编码--> 
  13.     <constant name="struts.i18n.encoding" value="UTF-8" /> 
  14.     <!--国际化--> 
  15.     <constant name="struts.custom.i18n.resources" value="i18n.message" /> 
  16.      
  17.     <!--加载所有Struts2配置文件--> 
  18.     <include file="*/struts.xml"/> 
  19.  
  20.     <package name="default" namespace="/" extends="struts-default"> 
  21.         <action name="userAction" class="userAction" method="doUser"> 
  22.             <result>/user.jsp</result> 
  23.         </action> 
  24.     </package> 
  25.          
  26. </struts> 

Spring 配置文件

  • applicationContext.xml
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.      xmlns:context="http://www.springframework.org/schema/context" 
  5.      xmlns:tx="http://www.springframework.org/schema/tx" 
  6.      xsi:schemaLocation="http://www.springframework.org/schema/beans 
  7.          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  8.          http://www.springframework.org/schema/context 
  9.          http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
  10.     <!-- UT框架Spring Configer --> 
  11.      
  12.     <!-- 配置使用注解 --> 
  13.     <context:annotation-config /> 
  14.      
  15.         <!-- 自动装在  
  16.     <context:component-scan base-package="cn.net.comsys.ut"/> 
  17.     --> 
  18.          
  19.  
  20. </beans> 

  • databaseContext.xml
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xmlns:context="http://www.springframework.org/schema/context" 
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xsi:schemaLocation=" 
  8.         http://www.springframework.org/schema/beans 
  9.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  10.         http://www.springframework.org/schema/context 
  11.         http://www.springframework.org/schema/context/spring-context-3.0.xsd 
  12.         http://www.springframework.org/schema/tx  
  13.         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
  14.         http://www.springframework.org/schema/aop  
  15.         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 
  16.         <!--加载数据库信息--> 
  17.     <bean  
  18.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
  19.         <property name="locations" value="classpath:jdbc.properties"/> 
  20.     </bean>    
  21.     <!--数据源--> 
  22.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
  23.         <property name="driverClassName" value="${jdbc.driverClassName}"/> 
  24.         <property name="url" value="${jdbc.url}"/> 
  25.         <property name="username" value="${jdbc.username}"/> 
  26.         <property name="password" value="${jdbc.password}"/> 
  27.         <property name="maxActive" value="${dbcp.maxActive}"/> 
  28.         <property name="maxIdle" value="${dbcp.maxIdle}"/> 
  29.         <property name="maxWait" value="${dbcp.maxWait}"/> 
  30.     </bean> 
  31.  
  32.     <!-- SessionFactory--> 
  33.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
  34.         <property name="dataSource" ref="dataSource"/> 
  35.         <!--hibernate config--> 
  36.         <property name="hibernateProperties"> 
  37.             <props> 
  38.                 <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
  39.                 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
  40.                 <prop key="connection.characterEncoding">${connection.characterEncoding}</prop> 
  41.                 <prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop> 
  42.             </props> 
  43.         </property> 
  44.         <!--映射--> 
  45.         <property name="annotatedClasses"> 
  46.             <list> 
  47.                 <value>cn.net.comsys.ut.model.Users</value> 
  48.                 <value>cn.net.comsys.ut.model.Roles</value> 
  49.             </list> 
  50.         </property>        
  51.     </bean> 
  52.     <!-- 事务管理器 -->   
  53.     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">   
  54.         <property name="sessionFactory" ref="sessionFactory"/>   
  55.     </bean>  
  56.  
  57.     <tx:advice id="txAdvice" transaction-manager="transactionManager">   
  58.         <tx:attributes>   
  59.             <tx:method name="save*" propagation="REQUIRED" />   
  60.             <tx:method name="add*" propagation="REQUIRED" />   
  61.             <tx:method name="update*" propagation="REQUIRED" />   
  62.             <tx:method name="del*" propagation="REQUIRED" />   
  63.             <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->   
  64.             <tx:method name="find*" propagation="REQUIRED" read-only="true" /> 
  65.         </tx:attributes>   
  66.     </tx:advice>   
  67.     <aop:config expose-proxy="true">   
  68.         <!-- 只对业务逻辑层实施事务 -->   
  69.         <aop:pointcut id="txPointcut" expression="execution(* cn.net.comsys.ut.service.impl.*.*(..))" />   
  70.         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>   
  71.     </aop:config>   
  72.      
  73. </beans> 
  • jdbc.properties
Properties代码 复制代码 收藏代码
  1. ######################### Mysql Config ################################## 
  2. jdbc.driverClassName=org.gjt.mm.mysql.Driver 
  3. jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8 
  4. jdbc.username=ut 
  5. jdbc.password=ut 
  6. ######################### Hibernate Config ################################## 
  7. #hibernate信息 
  8. hibernate.dialect=org.hibernate.dialect.MySQLDialect 
  9. hibernate.show_sql=true 
  10. connection.characterEncoding=utf-8 
  11. hibernate.current_session_context_class = org.springframework.orm.hibernate4.SpringSessionContext 
  12. #hibernate.current_session_context_class = thread 
  13. ######################### DBCP Config ################################## 
  14. #配置连接池信息 
  15. dbcp.maxActive=10 
  16. dbcp.maxIdle=3 
  17. dbcp.maxWait=10 
  18. dbcp.whenExhaustedAction=1 
  19. dbcp.validationQuery=select 1 from dual 
  20. dbcp.testOnBorrow=true 
  21. dbcp.testOnReturn=false 
  • daoContext.xml
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xmlns:context="http://www.springframework.org/schema/context" 
  5.     xsi:schemaLocation=" 
  6.         http://www.springframework.org/schema/beans 
  7.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  8.         http://www.springframework.org/schema/context 
  9.         http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
  10.     <bean id="userDao" class="cn.net.comsys.ut.dao.impl.UserDaoImpl"/> 
  11.  
  12. </beans> 
  • serviceContext.xml
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xmlns:context="http://www.springframework.org/schema/context" 
  5.     xsi:schemaLocation=" 
  6.         http://www.springframework.org/schema/beans 
  7.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  8.         http://www.springframework.org/schema/context 
  9.         http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
  10.     <bean id="userService" class="cn.net.comsys.ut.service.impl.UserServiceImpl"/> 
  11.  
  12. </beans> 
  • actionContext.xml
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xmlns:context="http://www.springframework.org/schema/context" 
  5.     xsi:schemaLocation=" 
  6.         http://www.springframework.org/schema/beans 
  7.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  8.         http://www.springframework.org/schema/context 
  9.         http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
  10.     <bean id="userAction" class="cn.net.comsys.ut.action.UserAction" scope="prototype"/> 
  11.  
  12. </beans> 
  3.编码
  • Users.java
Java代码 复制代码 收藏代码
  1. package cn.net.comsys.ut.model; 
  2.  
  3. import java.io.Serializable; 
  4. import java.util.Set; 
  5.  
  6. import javax.persistence.Basic; 
  7. import javax.persistence.CascadeType; 
  8. import javax.persistence.Entity; 
  9. import javax.persistence.FetchType; 
  10. import javax.persistence.GeneratedValue; 
  11. import javax.persistence.GenerationType; 
  12. import javax.persistence.Id; 
  13. import javax.persistence.JoinColumn; 
  14. import javax.persistence.JoinTable; 
  15. import javax.persistence.ManyToMany; 
  16. import javax.persistence.Table; 
  17. import javax.persistence.Transient; 
  18.  
  19. @Entity 
  20. @Table(name="ut_users"
  21. public class Users implements Serializable{ 
  22.     /**
  23.      * serialVersionUID
  24.      */ 
  25.     private static final long serialVersionUID = 1L; 
  26.     /**
  27.      * 用户编号
  28.      */ 
  29.     @Id 
  30.     @GeneratedValue(strategy=GenerationType.AUTO) 
  31.     private int userId; 
  32.     /**
  33.      * 账号
  34.      */ 
  35.     private String loginId; 
  36.     /**
  37.      * 密码
  38.      */ 
  39.     private String password; 
  40.     /**
  41.      * 姓名
  42.      */ 
  43.     private String name; 
  44.     /**
  45.      * 手机
  46.      */ 
  47.     private String phone; 
  48.     /**
  49.      * 邮箱
  50.      */ 
  51.     private String email; 
  52.     /**
  53.      * 身份证
  54.      */ 
  55.     private String cardId; 
  56.     /**
  57.      * 管理员标识
  58.      */ 
  59.     private int isDepartAdmin; 
  60.     /**
  61.      * @aggregation
  62.      * @clientCardinality 1
  63.      * @supplierCardinality 0..*
  64.      */ 
  65.     @ManyToMany(targetEntity=cn.net.comsys.ut.model.Roles.class
  66.             cascade={CascadeType.PERSIST, CascadeType.MERGE}, 
  67.             fetch=FetchType.EAGER) 
  68.     @JoinTable(name="ut_user_role"
  69.             joinColumns={@JoinColumn(name="userId")}, 
  70.             inverseJoinColumns={@JoinColumn(name="roleId")}) 
  71.     private Set<Roles> role; 
  72.      
  73.     public int getUserId() { 
  74.         return userId; 
  75.     } 
  76.     public void setUserId(int userId) { 
  77.         this.userId = userId; 
  78.     } 
  79.  
  80.     public String getLoginId() { 
  81.         return loginId; 
  82.     } 
  83.     public void setLoginId(String loginId) { 
  84.         this.loginId = loginId; 
  85.     } 
  86.     public String getPassword() { 
  87.         return password; 
  88.     } 
  89.     public void setPassword(String password) { 
  90.         this.password = password; 
  91.     } 
  92.     public String getName() { 
  93.         return name; 
  94.     } 
  95.     public void setName(String name) { 
  96.         this.name = name; 
  97.     } 
  98.     public String getPhone() { 
  99.         return phone; 
  100.     } 
  101.     public void setPhone(String phone) { 
  102.         this.phone = phone; 
  103.     } 
  104.     public String getEmail() { 
  105.         return email; 
  106.     } 
  107.     public void setEmail(String email) { 
  108.         this.email = email; 
  109.     } 
  110.     public String getCardId() { 
  111.         return cardId; 
  112.     } 
  113.     public void setCardId(String cardId) { 
  114.         this.cardId = cardId; 
  115.     } 
  116.     public int getIsDepartAdmin() { 
  117.         return isDepartAdmin; 
  118.     } 
  119.     public void setIsDepartAdmin(int isDepartAdmin) { 
  120.         this.isDepartAdmin = isDepartAdmin; 
  121.     } 
  122.     public Set<Roles> getRole() { 
  123.         return role; 
  124.     } 
  125.     public void setRole(Set<Roles> role) { 
  126.         this.role = role; 
  127.     } 
  128.      
package cn.net.comsys.ut.model;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name="ut_users")
public class Users implements Serializable{
    /**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	/**
     * 用户编号 
     */
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
    private int userId;
    /**
     * 账号 
     */
    private String loginId;
    /**
     * 密码 
     */
    private String password;
    /**
     * 姓名 
     */
    private String name;
    /**
     * 手机 
     */
    private String phone;
    /**
     * 邮箱 
     */
    private String email;
    /**
     * 身份证 
     */
    private String cardId;
    /**
     * 管理员标识 
     */
    private int isDepartAdmin;
    /**
     * @aggregation
     * @clientCardinality 1
     * @supplierCardinality 0..* 
     */
    @ManyToMany(targetEntity=cn.net.comsys.ut.model.Roles.class,
    		cascade={CascadeType.PERSIST, CascadeType.MERGE},
    		fetch=FetchType.EAGER)
    @JoinTable(name="ut_user_role",
    		joinColumns={@JoinColumn(name="userId")},
    		inverseJoinColumns={@JoinColumn(name="roleId")})
    private Set<Roles> role;
    
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}

	public String getLoginId() {
		return loginId;
	}
	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getCardId() {
		return cardId;
	}
	public void setCardId(String cardId) {
		this.cardId = cardId;
	}
	public int getIsDepartAdmin() {
		return isDepartAdmin;
	}
	public void setIsDepartAdmin(int isDepartAdmin) {
		this.isDepartAdmin = isDepartAdmin;
	}
	public Set<Roles> getRole() {
		return role;
	}
	public void setRole(Set<Roles> role) {
		this.role = role;
	}
    
}
  • Roles.java
Java代码 复制代码 收藏代码
  1. package cn.net.comsys.ut.model; 
  2.  
  3. import java.io.Serializable; 
  4. import java.util.Set; 
  5.  
  6. import javax.persistence.Basic; 
  7. import javax.persistence.CascadeType; 
  8. import javax.persistence.Entity; 
  9. import javax.persistence.FetchType; 
  10. import javax.persistence.GeneratedValue; 
  11. import javax.persistence.GenerationType; 
  12. import javax.persistence.Id; 
  13. import javax.persistence.ManyToMany; 
  14. import javax.persistence.Table; 
  15. import javax.persistence.Transient; 
  16. @Entity 
  17. @Table(name="ut_roles"
  18. public class Roles implements Serializable{ 
  19.     /**
  20.      * serialVersionUID
  21.      */ 
  22.     private static final long serialVersionUID = 1L; 
  23.     /**
  24.      * 角色编号
  25.      */ 
  26.     @Id 
  27.     @GeneratedValue(strategy=GenerationType.AUTO) 
  28.     private int roleId; 
  29.     /**
  30.      * 角色名称
  31.      */ 
  32.     private String roleName; 
  33.     /**
  34.      * 状态:禁用(0),激活(1)
  35.      */ 
  36.     private int status; 
  37.     /**
  38.      * 描述
  39.      */ 
  40.     private String description; 
  41.      
  42.     private int parentId; 
  43.      
  44.     @ManyToMany
  45.             cascade={CascadeType.PERSIST,CascadeType.MERGE}, 
  46.             mappedBy="role"
  47.             targetEntity=cn.net.comsys.ut.model.Users.class 
  48.         ) 
  49.  
  50.     private Set<Users> user; 
  51.  
  52.     public int getRoleId() { 
  53.         return roleId; 
  54.     } 
  55.  
  56.     public void setRoleId(int roleId) { 
  57.         this.roleId = roleId; 
  58.     } 
  59.  
  60.     public String getRoleName() { 
  61.         return roleName; 
  62.     } 
  63.  
  64.     public void setRoleName(String roleName) { 
  65.         this.roleName = roleName; 
  66.     } 
  67.  
  68.     public int getStatus() { 
  69.         return status; 
  70.     } 
  71.  
  72.     public void setStatus(int status) { 
  73.         this.status = status; 
  74.     } 
  75.  
  76.     public String getDescription() { 
  77.         return description; 
  78.     } 
  79.  
  80.     public void setDescription(String description) { 
  81.         this.description = description; 
  82.     } 
  83.  
  84.     public int getParentId() { 
  85.         return parentId; 
  86.     } 
  87.  
  88.     public void setParentId(int parentId) { 
  89.         this.parentId = parentId; 
  90.     } 
  91.  
  92.     public Set<Users> getUser() { 
  93.         return user; 
  94.     } 
  95.  
  96.     public void setUser(Set<Users> user) { 
  97.         this.user = user; 
  98.     } 
  99.      
package cn.net.comsys.ut.model;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
@Table(name="ut_roles")
public class Roles implements Serializable{
    /**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	/**
     * 角色编号 
     */
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
    private int roleId;
    /**
     * 角色名称 
     */
    private String roleName;
    /**
     * 状态:禁用(0),激活(1) 
     */
    private int status;
    /**
     * 描述 
     */
    private String description;
	
    private int parentId;
	
    @ManyToMany(
            cascade={CascadeType.PERSIST,CascadeType.MERGE},
            mappedBy="role",
            targetEntity=cn.net.comsys.ut.model.Users.class
        )

	private Set<Users> user;

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getParentId() {
		return parentId;
	}

	public void setParentId(int parentId) {
		this.parentId = parentId;
	}

	public Set<Users> getUser() {
		return user;
	}

	public void setUser(Set<Users> user) {
		this.user = user;
	}
	
}
Java代码 复制代码 收藏代码
  1. DAO层 
DAO层
  • IUserDao.java
Java代码 复制代码 收藏代码
  1. package cn.net.comsys.ut.dao.interfaces; 
  2.  
  3. import java.util.List; 
  4.  
  5. import cn.net.comsys.ut.model.Users; 
  6.  
  7. public interface IUserDao{ 
  8.  
  9.     public List<Users> findUser(); 
  10.      
  11.     public List<Users> findUser(String loginId); 
  12.                                                                                                                                          
package cn.net.comsys.ut.dao.interfaces;

import java.util.List;

import cn.net.comsys.ut.model.Users;

public interface IUserDao{

	public List<Users> findUser();
	
	public List<Users> findUser(String loginId);
}
																																		
  • UserDaoImpl.java
Java代码 复制代码 收藏代码
  1. package cn.net.comsys.ut.dao.impl; 
  2.  
  3. import java.util.List; 
  4.  
  5. import javax.annotation.Resource; 
  6.  
  7. import org.hibernate.Session; 
  8. import org.hibernate.SessionFactory; 
  9.  
  10. import cn.net.comsys.ut.dao.interfaces.IUserDao; 
  11. import cn.net.comsys.ut.model.Users; 
  12.  
  13. public class UserDaoImpl implements IUserDao { 
  14.  
  15.     @Resource 
  16.     private SessionFactory sessionFactory; 
  17.  
  18.     @Override 
  19.     public List<Users> findUser() { 
  20.         Session session = sessionFactory.getCurrentSession(); 
  21.         List<Users> userList = session.createQuery("from Users").list(); 
  22.         return userList; 
  23.     } 
  24.  
  25.     @Override 
  26.     public List<Users> findUser(String loginId) { 
  27.          
  28.         Session session = sessionFactory.getCurrentSession(); 
  29.         List<Users> userList = session.createQuery("from Users user where user.loginId= :loginId").setString("loginId", loginId).list(); 
  30.          
  31.         return userList; 
  32.     } 
  33.      
package cn.net.comsys.ut.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import cn.net.comsys.ut.dao.interfaces.IUserDao;
import cn.net.comsys.ut.model.Users;

public class UserDaoImpl implements IUserDao {

	@Resource
	private SessionFactory sessionFactory;

	@Override
	public List<Users> findUser() {
		Session	session = sessionFactory.getCurrentSession();
		List<Users> userList = session.createQuery("from Users").list();
		return userList;
	}

	@Override
	public List<Users> findUser(String loginId) {
		
		Session session = sessionFactory.getCurrentSession();
		List<Users> userList = session.createQuery("from Users user where user.loginId= :loginId").setString("loginId", loginId).list();
		
		return userList;
	}
	
}
Java代码 复制代码 收藏代码
  1. 业务层 
业务层
  • IUserService.java
Java代码 复制代码 收藏代码
  1. package cn.net.comsys.ut.service.interfaces; 
  2.  
  3. import java.util.List; 
  4.  
  5. import cn.net.comsys.ut.model.Users; 
  6.  
  7. public interface IUserService { 
  8.  
  9.     public List<Users> findUser(); 
  10.      
  11.     public List<Users> findUser(String loginId); 
package cn.net.comsys.ut.service.interfaces;

import java.util.List;

import cn.net.comsys.ut.model.Users;

public interface IUserService {

	public List<Users> findUser();
	
	public List<Users> findUser(String loginId);
}
  • UserServiceImpl.java
Java代码 复制代码 收藏代码
  1. package cn.net.comsys.ut.service.impl; 
  2.  
  3. import java.util.List; 
  4.  
  5. import javax.annotation.Resource; 
  6.  
  7. import cn.net.comsys.ut.dao.interfaces.IUserDao; 
  8. import cn.net.comsys.ut.model.Users; 
  9. import cn.net.comsys.ut.service.interfaces.IUserService; 
  10.  
  11. public class UserServiceImpl implements IUserService{ 
  12.      
  13.     @Resource 
  14.     private IUserDao userDao; 
  15.  
  16.     public List<Users> findUser() { 
  17.          
  18.         List<Users> userList = userDao.findUser(); 
  19.          
  20.         return userList; 
  21.     } 
  22.  
  23.     @Override 
  24.     public List<Users> findUser(String loginId) { 
  25.          
  26.         List<Users> userList = userDao.findUser(loginId); 
  27.          
  28.         return userList; 
  29.     } 
  30.  
package cn.net.comsys.ut.service.impl;

import java.util.List;

import javax.annotation.Resource;

import cn.net.comsys.ut.dao.interfaces.IUserDao;
import cn.net.comsys.ut.model.Users;
import cn.net.comsys.ut.service.interfaces.IUserService;

public class UserServiceImpl implements IUserService{
	
	@Resource
	private IUserDao userDao;

	public List<Users> findUser() {
		
		List<Users> userList = userDao.findUser();
		
		return userList;
	}

	@Override
	public List<Users> findUser(String loginId) {
		
		List<Users> userList = userDao.findUser(loginId);
		
		return userList;
	}

}
Java代码 复制代码 收藏代码
  1. action 
action
  • UserAction.action
Java代码 复制代码 收藏代码
  1. package cn.net.comsys.ut.action; 
  2.  
  3. import java.util.List; 
  4.  
  5. import javax.annotation.Resource; 
  6.  
  7. import cn.net.comsys.ut.model.Users; 
  8. import cn.net.comsys.ut.service.interfaces.IUserService; 
  9.  
  10. import com.opensymphony.xwork2.ActionSupport; 
  11.  
  12. public class UserAction extends ActionSupport { 
  13.  
  14.     /**
  15.      * serialVersionUID
  16.      */ 
  17.     private static final long serialVersionUID = 1L; 
  18.     @Resource 
  19.     private IUserService userService; 
  20.     private List<Users> userList; 
  21.     public String doUser() { 
  22.          
  23.         userList = userService.findUser(); 
  24.         return SUCCESS; 
  25.     } 
  26.  
  27.     public List<Users> getUserList() { 
  28.         return userList; 
  29.     } 
  30.     public void setUserList(List<Users> userList) { 
  31.         this.userList = userList; 
  32.     } 
  33.      
package cn.net.comsys.ut.action;

import java.util.List;

import javax.annotation.Resource;

import cn.net.comsys.ut.model.Users;
import cn.net.comsys.ut.service.interfaces.IUserService;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	@Resource
	private IUserService userService;
	private List<Users> userList;
	public String doUser() {
		
		userList = userService.findUser();
		return SUCCESS;
	}

	public List<Users> getUserList() {
		return userList;
	}
	public void setUserList(List<Users> userList) {
		this.userList = userList;
	}
	
}
   4.页面
  • user.jsp
Jsp代码 复制代码 收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" 
  2.     pageEncoding="UTF-8"%> 
  3. <%@ taglib prefix="s" uri="/struts-tags" %> 
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
  5. <html> 
  6. <head> 
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
  8. <title>用户</title> 
  9. </head> 
  10. <body> 
  11.     <table border="1"
  12.         <s:iterator value="userList"
  13.             <tr> 
  14.                 <td><s:property value="loginId"/> </td> 
  15.                 <td> 
  16.                     <s:iterator value="role"
  17.                         <s:property value="roleName"/> 
  18.                     </s:iterator> 
  19.                 </td> 
  20.             </tr> 
  21.         </s:iterator> 
  22.     </table> 
  23.     <s:debug></s:debug> 
  24. </body> 
  25. </html> 

5.结果



  • 大小: 12.1 KB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值