wangl66 2016-08-28 01:23 采纳率: 75%
浏览 1866
已采纳

ssh框架问题,拿不到数据库关联表里的数据

package com.wangl.sshsimple.entity;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

@Entity
public class TbUser {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long userId;
private String userName;
private String userPassword;
private String pName;
private String phoneNo;
@ManyToMany(fetch=FetchType.EAGER)
private List roles;

public String getpName() {
    return pName;
}
public void setpName(String pName) {
    this.pName = pName;
}
public String getPhoneNo() {
    return phoneNo;
}
public void setPhoneNo(String phoneNo) {
    this.phoneNo = phoneNo;
}
public List<TbRole> getRoles() {
    return roles;
}
public void setRoles(List<TbRole> roles) {
    this.roles = roles;
}

public String getUserPassword() {
    return userPassword;
}
public void setUserPassword(String userPassword) {
    this.userPassword = userPassword;
}
public Long getUserId() {
    return userId;
}
public void setUserId(Long userId) {
    this.userId = userId;
}
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
@Override
public String toString() {
    return "TbUser [userId=" + userId + ", userName=" + userName + "]";
}

}
package com.wangl.sshsimple.entity;

import java.util.List;

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.ManyToMany;

@Entity
public class TbRole {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long roleId;
private String roleName;
private String description;
@ManyToMany(fetch=FetchType.EAGER)
private List functions;
@ManyToMany(mappedBy="roles")
private List users;

public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public List<TbFunction> getFunctions() {
    return functions;
}
public void setFunctions(List<TbFunction> functions) {
    this.functions = functions;
}
public List<TbUser> getUsers() {
    return users;
}
public void setUsers(List<TbUser> users) {
    this.users = users;
}
public Long getRoleId() {
    return roleId;
}
public void setRoleId(Long roleId) {
    this.roleId = roleId;
}
public String getRoleName() {
    return roleName;
}
public void setRoleName(String roleName) {
    this.roleName = roleName;
}

}
package com.wangl.sshsimple.entity;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

@Entity
public class TbFunction {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long functionId;
private String functionName;
private String url;
private String memo;
@ManyToMany(mappedBy="functions")
private List roles;

public List<TbRole> getRoles() {
    return roles;
}
public void setRoles(List<TbRole> roles) {
    this.roles = roles;
}
public String getMemo() {
    return memo;
}
public void setMemo(String memo) {
    this.memo = memo;
}
public Long getFunctionId() {
    return functionId;
}
public void setFunctionId(Long functionId) {
    this.functionId = functionId;
}
public String getFunctionName() {
    return functionName;
}
public void setFunctionName(String functionName) {
    this.functionName = functionName;
}
public String getUrl() {
    return url;
}
public void setUrl(String url) {
    this.url = url;
}
@Override
public String toString() {
    return "TbFunction [functionId=" + functionId + ", functionName=" + functionName + ", url=" + url + "]";
}

}
package com.wangl.sshsimple.dao;

import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.wangl.sshsimple.entity.TbRole;
import com.wangl.sshsimple.entity.TbUser;

@Repository
public class TbUserDaoImp extends GenericDaoImp implements TbUserDao{

public TbUserDaoImp() {
    super(TbUser.class);
}

public List<TbUser> findByCondition(Map condition) {
    String hql="from TbUser where 1=1";
    if(condition.size()>0){
        if(condition.containsKey("userName")){
            hql+=" and userName like '%"+condition.get("userName")+"%'";
        }
        if(condition.containsKey("pName")){
            hql+=" and pName like '%"+condition.get("pName")+"%'";
        }
        if(condition.containsKey("phoneNo")){
            hql+=" and phoneNo like '%"+condition.get("phoneNo")+"%'";
        }
        if(condition.containsKey("loginUserName")){
            hql+=" and userName='"+condition.get("loginUserName")+"'";
        }
        if(condition.containsKey("loginPassword")){
            hql+=" and userPassword='"+condition.get("loginPassword")+"'";
        }
    }
    return (List<TbUser>) this.getHibernateTemplate().find(hql);
}

}
package com.wangl.sshsimple.dao;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

public class GenericDaoImp extends HibernateDaoSupport implements GenericDao {
//用添加属性这种方法拿到泛型的具体类型
private Class entityClass;

public GenericDaoImp(Class<T> entityClass){
    this.entityClass = entityClass;
}

public void add(T entity) {
    this.getHibernateTemplate().save(entity);
}

public void update(T entity) {
    this.getHibernateTemplate().update(entity);
}

public void delete(T entity) {
    this.getHibernateTemplate().delete(entity);
}

public List<T> search(Map condition) {
    return (List<T>)this.getHibernateTemplate().find("");
}

public List<T> findAll() {
    String hql="from "+entityClass.getName();   
    return (List<T>) this.getHibernateTemplate().find(hql);
}

public T get(K id) {
    return this.getHibernateTemplate().get(entityClass, id);
}

}
package com.wangl.sshsimple.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.wangl.sshsimple.entity.TbFunction;
import com.wangl.sshsimple.entity.TbRole;
import com.wangl.sshsimple.entity.TbUser;
import com.wangl.sshsimple.service.TbRoleService;
import com.wangl.sshsimple.service.TbUserService;

public class LoginAction {
private String userName;
private String userPassword;
private TbUserService tbUserService;
private TbRoleService tbRoleService;

public void setTbRoleService(TbRoleService tbRoleService) {
    this.tbRoleService = tbRoleService;
}
public void setTbUserService(TbUserService tbUserService) {
    this.tbUserService = tbUserService;
}
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
public String getUserPassword() {
    return userPassword;
}
public void setUserPassword(String userPassword) {
    this.userPassword = userPassword;
}

public String login(){
    //登录成功后将user放入session
    //将用户权限放入session
    if(userName!=null&& !userName.equals("") &&userPassword!=null&& !userPassword.equals("")){
        HttpSession sess=ServletActionContext.getRequest().getSession();
        Map condition = new HashMap();
        condition.put("loginUserName", userName);
        condition.put("loginPassword", userPassword);
        List<TbUser> tbUsers=tbUserService.findByCondition(condition);
        if(tbUsers!=null&&tbUsers.size()>0){
            for(TbUser u:tbUsers){                  
                List<TbRole> roles=u.getRoles();
                if(roles.size()>0){
                    for(TbRole r:roles){
                        List<TbFunction> functions=r.getFunctions();
                        if(functions.size()>0){
                            sess.setAttribute("functions", functions);
                        }
                    }
                }
                sess.setAttribute("tbUser", u);
            }
            return "success";
        }
    }
    return "notLoged";
}

}

  • 写回答

3条回答 默认 最新

  • wangl66 2016-08-28 04:24
    关注

    [INFO] Scanning for projects...
    [INFO]

    [INFO] ------------------------------------------------------------------------
    [INFO] Building sshsimple Maven Webapp 0.0.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) > process-classes @ sshsimple >>>
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ sshsimple ---
    [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] Copying 4 resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ sshsimple ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) < process-classes @ sshsimple <<<
    [INFO]
    [INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ sshsimple ---
    [INFO] Running war on http://localhost:8080/sshsimple
    [INFO] Using existing Tomcat server configuration at C:\Program Files\daima\sshsimple\target\tomcat
    [INFO] create webapp with contextPath: /sshsimple
    八月 28, 2016 12:25:09 下午 org.apache.coyote.AbstractProtocol init
    信息: Initializing ProtocolHandler ["http-bio-8080"]
    八月 28, 2016 12:25:10 下午 org.apache.catalina.core.StandardService startInternal
    信息: Starting service Tomcat
    八月 28, 2016 12:25:10 下午 org.apache.catalina.core.StandardEngine startInternal
    信息: Starting Servlet Engine: Apache Tomcat/7.0.47
    八月 28, 2016 12:25:28 下午 org.apache.catalina.core.ApplicationContext log
    信息: No Spring WebApplicationInitializer types detected on classpath
    八月 28, 2016 12:25:28 下午 org.apache.catalina.core.ApplicationContext log
    信息: Initializing Spring root WebApplicationContext
    八月 28, 2016 12:25:28 下午 org.springframework.web.context.ContextLoader initWebApplicationContext
    信息: Root WebApplicationContext: initialization started
    八月 28, 2016 12:25:29 下午 org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
    信息: Refreshing Root WebApplicationContext: startup date [Sun Aug 28 12:25:29 GMT+08:00 2016]; root of context hierarchy
    八月 28, 2016 12:25:29 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from URL [file:/C:/Program%20Files/daima/sshsimple/target/classes/application-context.xml]
    八月 28, 2016 12:25:30 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from URL [file:/C:/Program%20Files/daima/sshsimple/target/classes/application-context-hibernate.xml]
    八月 28, 2016 12:25:30 下午 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
    信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
    ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
    八月 28, 2016 12:25:39 下午 org.springframework.orm.hibernate5.HibernateTransactionManager afterPropertiesSet
    信息: Using DataSource [org.apache.commons.dbcp.BasicDataSource@53f1a2c] of Hibernate SessionFactory for HibernateTransactionManager
    八月 28, 2016 12:25:39 下午 org.springframework.web.context.ContextLoader initWebApplicationContext
    信息: Root WebApplicationContext: initialization completed in 10792 ms
    八月 28, 2016 12:25:42 下午 org.apache.coyote.AbstractProtocol start
    信息: Starting ProtocolHandler ["http-bio-8080"]
    Hibernate:
    select
    tbuser0_.userId as userId1_3_,
    tbuser0_.pName as pName2_3_,
    tbuser0_.phoneNo as phoneNo3_3_,
    tbuser0_.userName as userName4_3_,
    tbuser0_.userPassword as userPass5_3_
    from
    TbUser tbuser0_
    where
    1=1
    and tbuser0_.userName='wangl'
    and tbuser0_.userPassword='123'
    Hibernate:
    select
    roles0_.users_userId as users_us1_4_0_,
    roles0_.roles_roleId as roles_ro2_4_0_,
    tbrole1_.roleId as roleId1_1_1_,
    tbrole1_.description as descript2_1_1_,
    tbrole1_.roleName as roleName3_1_1_
    from
    TbUser_TbRole roles0_
    inner join
    TbRole tbrole1_
    on roles0_.roles_roleId=tbrole1_.roleId
    where
    roles0_.users_userId=?
    Hibernate:
    select
    tbuser0_.userId as userId1_3_,
    tbuser0_.pName as pName2_3_,
    tbuser0_.phoneNo as phoneNo3_3_,
    tbuser0_.userName as userName4_3_,
    tbuser0_.userPassword as userPass5_3_
    from
    TbUser tbuser0_
    Hibernate:
    select
    roles0_.users_userId as users_us1_4_0_,
    roles0_.roles_roleId as roles_ro2_4_0_,
    tbrole1_.roleId as roleId1_1_1_,
    tbrole1_.description as descript2_1_1_,
    tbrole1_.roleName as roleName3_1_1_
    from
    TbUser_TbRole roles0_
    inner join
    TbRole tbrole1_
    on roles0_.roles_roleId=tbrole1_.roleId
    where
    roles0_.users_userId=?
    Hibernate:
    select
    roles0_.users_userId as users_us1_4_0_,
    roles0_.roles_roleId as roles_ro2_4_0_,
    tbrole1_.roleId as roleId1_1_1_,
    tbrole1_.description as descript2_1_1_,
    tbrole1_.roleName as roleName3_1_1_
    from
    TbUser_TbRole roles0_
    inner join
    TbRole tbrole1_
    on roles0_.roles_roleId=tbrole1_.roleId
    where
    roles0_.users_userId=?
    Hibernate:
    select
    roles0_.users_userId as users_us1_4_0_,
    roles0_.roles_roleId as roles_ro2_4_0_,
    tbrole1_.roleId as roleId1_1_1_,
    tbrole1_.description as descript2_1_1_,
    tbrole1_.roleName as roleName3_1_1_
    from
    TbUser_TbRole roles0_
    inner join
    TbRole tbrole1_
    on roles0_.roles_roleId=tbrole1_.roleId
    where
    roles0_.users_userId=?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)