这里有一个名为
的连接查询SELECT um.USER_ID, um.USERNAME, um.PASSWORD, um.AGENCIA,
um.EMAIL, um.GRABADO_POR, um.MOBILENUMBER,
um.USER_STATUS, um.ZONE, um.NAME,
um.USER_TYPE, urmm.USERROLEMAPPING_ID,
r.ROLE_ID, r.ROLE_NAME, r.PRIORITY,
rcmm.COMPONENT_ID, am.ACTION_ID, am.ACTION_NMAE,
cm.COMPONENTID, cm.COMPONENTNAME, cm.COMPONENTIDENTIFICATION,
cm.COMPONENTSTATE
FROM USER_MASTER um, role r, USER_ROLE_MAPPING_MASTER urmm,
ACTION_MASTER am, ROLE_COMPONENT_MAPPING_MASTER rcmm,
ACTION_COMPONENT_MAPPINGMASTER acm,COMPONENT_MASTER cm
WHERE upper(um.USERNAME) = upper(?) AND um.USER_ID = urmm.USER_ID AND
urmm.ROLE_ID = r.ROLE_ID AND r.ROLE_ID = rcmm.ROLE_ID AND
urmm.ROLE_ID = rcmm.ROLE_ID AND
acm.ACTION_ID = am.ACTION_ID AND
rcmm.COMPONENT_NAME = acm.COMPONENT_NAME AND
acm.COMPONENT_NAME=cm.COMPONENTNAME(+)
我必须将该查询设置到我的bean中,是否可以使用行映射器或者是否有任何实现方法。在这里我发布我的练习代码是推荐的吗?如果不正确则给我建议。
在DAOImpl
@Override
public List<User> getUserTest(String username) {
final List<User> userList=new ArrayList<User>();
String query="SELECT um.USER_ID, um.USERNAME, um.PASSWORD, um.AGENCIA, um.EMAIL, um.GRABADO_POR, um.MOBILENUMBER, um.USER_STATUS, um.ZONE, um.NAME, um.USER_TYPE, urmm.USERROLEMAPPING_ID, r.ROLE_ID, r.ROLE_NAME, r.PRIORITY, rcmm.COMPONENT_ID, am.ACTION_ID, am.ACTION_NMAE, cm.COMPONENTID, cm.COMPONENTNAME, cm.COMPONENTIDENTIFICATION, cm.COMPONENTSTATE FROM USER_MASTER um, role r, USER_ROLE_MAPPING_MASTER urmm, ACTION_MASTER am, ROLE_COMPONENT_MAPPING_MASTER rcmm, ACTION_COMPONENT_MAPPINGMASTER acm,COMPONENT_MASTER cm WHERE upper(um.USERNAME)=upper(?) AND um.USER_ID =urmm.USER_ID AND urmm.ROLE_ID =r.ROLE_ID AND r.ROLE_ID =rcmm.ROLE_ID AND urmm.ROLE_ID =rcmm.ROLE_ID AND acm.ACTION_ID =am.ACTION_ID AND rcmm.COMPONENT_NAME =acm.COMPONENT_NAME AND acm.COMPONENT_NAME=cm.COMPONENTNAME(+)";
System.out.println("in dao");
try{
List<Object> users = getJdbcTemplate().query(query, new Object[] { username },
new RowMapper<Object>(){
@Override
public Object mapRow(ResultSet rs, int arg1)
throws SQLException {
User user=new User();
user.setId(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.setAgencia(rs.getString(4));
user.setEmail(rs.getString(5));
user.setGrabadoPor(rs.getString(6));
user.setMobileNumber(rs.getString(7));
user.setUserStatus(rs.getString(8));
user.setZone(rs.getString(9));
user.setName(rs.getString(10));
user.setUserType(rs.getString(11));
Set<UserRole> urole=user.getUserRoles();
if(urole == null){
urole = new HashSet<UserRole>();
}
UserRole userRole=new UserRole();
userRole.setId(rs.getInt(12));
// System.out.println("id is user role"+user.getId());
userRole.setUser(user);
Role role=userRole.getRole();
if(role == null){
role = new Role();
}
role.setRoleId(rs.getInt(13));
role.setRoleName(rs.getString(14));
role.setPriority(rs.getInt(15));
role.setUserRoles(urole);
Set<RoleAction> roleAction=role.getRoleActions();
if(roleAction == null){
roleAction = new HashSet<RoleAction>();
}
RoleAction roleaction=new RoleAction();
roleaction.setId(rs.getInt(16));
roleaction.setRole(role);
Action action= roleaction.getAction();
if(action == null){
action = new Action();
}
action.setActionId(rs.getInt(17));
action.setActionName(rs.getString(18));
Set<CustomizableMenus> custmenusSet=action.getCustomizablemenu();
if(custmenusSet == null){
custmenusSet = new HashSet<CustomizableMenus>();
}
CustomizableMenus custmenus=new CustomizableMenus();
custmenus.setComponentId(rs.getInt(19));
custmenus.setComponentName(rs.getString(20));
custmenus.setComponentIdentification(rs.getString(21));
custmenus.setComponentState(rs.getString(22));
custmenusSet.add(custmenus);
action.setCustomizablemenu(custmenusSet);
System.out.println("action id and name is "+rs.getInt(17)+" "+rs.getString(18));
action.setRoleActions(roleAction);
roleaction.setAction(action);
roleAction.add(roleaction);
role.setRoleActions(roleAction);
userRole.setRole(role);
urole.add(userRole);
user.setUserRoles(urole);
userList.add(user);
System.out.println("cust menus in db custid "+ custmenus.getComponentId()+"component name is "+custmenus.getComponentName()+"identification "+custmenus.getComponentIdentification());
return user;
}
});
if (userList.size()>0) {
System.out.println("the list is "+ userList);
return userList;
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
user.java
private Integer id;
private String username;
private String password;
private String name;
private String email;
private String mobileNumber;
private String reTypeMobileNumber;
private String status;
private String userType;
private String userDescription;
private String grabadoPor;
private String userStatus;
private String zone;
private String agencia;
private Set<UserRole> userRoles = new HashSet<UserRole>(0);
UserRole.java
public class UserRole implements Serializable{
private Integer id;
private User user;
private Role role;
Role.java
public class Role implements Serializable{
private Integer roleId;
private String roleName;
private Integer priority;
private String roleType;
private String roleDescription;
private String roledentification;
private String grabadoPor;
private Date fechaDeCreacion;
private Date fechaDeModificada;
private Set<UserRole> userRoles = new HashSet<UserRole>(0);
private Set<RoleAction> roleActions = new HashSet<RoleAction>(0);
RoleAction.java
public class RoleAction implements Serializable{
private Integer id;
private Role role;
private Action action;
Action.java
public class Action implements Serializable{
private Integer actionId;
private String actionName;
private String actionType;
private String url;
private Date fechaDeCreacion;
private Date fechaDeModificada;
private Set<RoleAction> roleActions = new HashSet<RoleAction>(0);
private Set<CustomizableMenus> customizablemenu=new HashSet<CustomizableMenus>(0);
CustomizableMenus.java
public class CustomizableMenus implements Serializable{
private Integer componentId;
private String componentName;
private String componentIdentification;
//private String componentType;
private String componentState;
是否可以减少代码量。并且通过hibernate,它只返回与一个用户对象相关的所有与用户相关的角色和角色相关的自定义菜单,因此我返回list.get(0)。这里我的连接查询返回了他正在返回的许多记录中有多少个操作。所以我得到了更多的用户对象,所以如果我将用户列表发送到前端,它将返回查询返回的最后一条记录。
答案 0 :(得分:1)
很容易。你可以这样做。 创建一个名为 UserActionVO.java 的 View Object类,并为其创建一个名为 UserActionVOMapper.java 的映射器类,如下所示。
<强> UserActionVO.java 强>
public class UserActionVO {
private int userId;
private String userName;
private String password;
private String agencia;
private String email;
private String grabadoPor;
private String mobileNumber;
private String userStatus;
private String zone;
private String name;
private String userType;
private int userRoleMappingId;
private int roleId;
private String roleName;
private int priority;
private int roleActionId;
private String roleActionName;
private int componentId;
private String componentName;
private String componentIdentification;
private String componentState;
/**
* @return the userId
*/
public int getUserId() {
return userId;
}
/**
* @param userId the userId to set
*/
public void setUserId(int userId) {
this.userId = userId;
}
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the agencia
*/
public String getAgencia() {
return agencia;
}
/**
* @param agencia the agencia to set
*/
public void setAgencia(String agencia) {
this.agencia = agencia;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the grabadoPor
*/
public String getGrabadoPor() {
return grabadoPor;
}
/**
* @param grabadoPor the grabadoPor to set
*/
public void setGrabadoPor(String grabadoPor) {
this.grabadoPor = grabadoPor;
}
/**
* @return the mobileNumber
*/
public String getMobileNumber() {
return mobileNumber;
}
/**
* @param mobileNumber the mobileNumber to set
*/
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
/**
* @return the userStatus
*/
public String getUserStatus() {
return userStatus;
}
/**
* @param userStatus the userStatus to set
*/
public void setUserStatus(String userStatus) {
this.userStatus = userStatus;
}
/**
* @return the zone
*/
public String getZone() {
return zone;
}
/**
* @param zone the zone to set
*/
public void setZone(String zone) {
this.zone = zone;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the userType
*/
public String getUserType() {
return userType;
}
/**
* @param userType the userType to set
*/
public void setUserType(String userType) {
this.userType = userType;
}
/**
* @return the userRoleMappingId
*/
public int getUserRoleMappingId() {
return userRoleMappingId;
}
/**
* @param userRoleMappingId the userRoleMappingId to set
*/
public void setUserRoleMappingId(int userRoleMappingId) {
this.userRoleMappingId = userRoleMappingId;
}
/**
* @return the roleId
*/
public int getRoleId() {
return roleId;
}
/**
* @param roleId the roleId to set
*/
public void setRoleId(int roleId) {
this.roleId = roleId;
}
/**
* @return the roleName
*/
public String getRoleName() {
return roleName;
}
/**
* @param roleName the roleName to set
*/
public void setRoleName(String roleName) {
this.roleName = roleName;
}
/**
* @return the priority
*/
public int getPriority() {
return priority;
}
/**
* @param priority the priority to set
*/
public void setPriority(int priority) {
this.priority = priority;
}
/**
* @return the roleActionId
*/
public int getRoleActionId() {
return roleActionId;
}
/**
* @param roleActionId the roleActionId to set
*/
public void setRoleActionId(int roleActionId) {
this.roleActionId = roleActionId;
}
/**
* @return the roleActionName
*/
public String getRoleActionName() {
return roleActionName;
}
/**
* @param roleActionName the roleActionName to set
*/
public void setRoleActionName(String roleActionName) {
this.roleActionName = roleActionName;
}
/**
* @return the componentId
*/
public int getComponentId() {
return componentId;
}
/**
* @param componentId the componentId to set
*/
public void setComponentId(int componentId) {
this.componentId = componentId;
}
/**
* @return the componentName
*/
public String getComponentName() {
return componentName;
}
/**
* @param componentName the componentName to set
*/
public void setComponentName(String componentName) {
this.componentName = componentName;
}
/**
* @return the componentIdentification
*/
public String getComponentIdentification() {
return componentIdentification;
}
/**
* @param componentIdentification the componentIdentification to set
*/
public void setComponentIdentification(String componentIdentification) {
this.componentIdentification = componentIdentification;
}
/**
* @return the componentState
*/
public String getComponentState() {
return componentState;
}
/**
* @param componentState the componentState to set
*/
public void setComponentState(String componentState) {
this.componentState = componentState;
}
}
<强> UserActionVOMapper.java 强>
public class UserActionVOMapper implements RowMapper<UserActionVO>(){
@Override
public UserActionVO mapRow(ResultSet rs, int rownumber) throws SQLException {
UserActionVO userActionVO = new UserActionVO();
userActionVO.setUserId(rs.getString("USER_ID"));
userActionVO.setUserName(rs.getString("USERNAME"));
userActionVO.setPassword(rs.getString("PASSWORD"));
userActionVO.setAgencia(rs.getString("AGENCIA"));
userActionVO.setEmail(rs.getString("EMAIL"));
userActionVO.setGrabadoPor(rs.getString("GRABADO_POR"));
userActionVO.setMobileNumber(rs.getString("MOBILENUMBER"));
userActionVO.setUserStatus(rs.getString("USER_STATUS"));
userActionVO.setZone(rs.getString("ZONE"));
userActionVO.setName(rs.getString("NAME"));
userActionVO.setUserType(rs.getString("USER_TYPE"));
userActionVO.setUserRoleMappingId(rs.getInt("USERROLEMAPPING_ID"));
userActionVO.setRoleId(rs.getInt("ROLE_ID"));
userActionVO.setRoleName(rs.getString("ROLE_NAME"));
userActionVO.setPriority(rs.getInt("PRIORITY"));
userActionVO.setRoleActionId(rs.getInt("COMPONENT_ID"));
userActionVO.setActionId(rs.getInt("ACTION_ID"));
userActionVO.setActionName(rs.getString("ACTION_NMAE"));
userActionVO.setComponentId(rs.getInt("COMPONENTID"));
userActionVO.setComponentName(rs.getString("COMPONENTNAME"));
userActionVO.setComponentIdentification(rs.getString("COMPONENTIDENTIFICATION"));
userActionVO.setComponentState(rs.getString("COMPONENTSTATE"));
return userActionVO;
}
}
现在在您的代码中使用它,如下所示,返回 UserActionVO POJO列表而不是用户POJO列表:
@Override
public List<UserActionVO> getUserTest(String username) {
final List<UserActionVO> userActionVOs = null;
String query="SELECT um.USER_ID, um.USERNAME, um.PASSWORD, um.AGENCIA, um.EMAIL, um.GRABADO_POR, um.MOBILENUMBER, um.USER_STATUS, um.ZONE, um.NAME, um.USER_TYPE, urmm.USERROLEMAPPING_ID, r.ROLE_ID, r.ROLE_NAME, r.PRIORITY, rcmm.COMPONENT_ID, am.ACTION_ID, am.ACTION_NMAE, cm.COMPONENTID, cm.COMPONENTNAME, cm.COMPONENTIDENTIFICATION, cm.COMPONENTSTATE FROM USER_MASTER um, role r, USER_ROLE_MAPPING_MASTER urmm, ACTION_MASTER am, ROLE_COMPONENT_MAPPING_MASTER rcmm, ACTION_COMPONENT_MAPPINGMASTER acm,COMPONENT_MASTER cm WHERE upper(um.USERNAME)=upper(?) AND um.USER_ID =urmm.USER_ID AND urmm.ROLE_ID =r.ROLE_ID AND r.ROLE_ID =rcmm.ROLE_ID AND urmm.ROLE_ID =rcmm.ROLE_ID AND acm.ACTION_ID =am.ACTION_ID AND rcmm.COMPONENT_NAME =acm.COMPONENT_NAME AND acm.COMPONENT_NAME=cm.COMPONENTNAME(+)";
System.out.println("in dao");
try{
userActionVOs = getJdbcTemplate().query(query, new Object[] { username }, new UserActionVOMapper());
}catch(Exception e){
e.printStackTrace();
}
return userList;
}
容易!!!对!!
如果有任何问题,请告诉我。