利用SSM(springmvc+spring+mybatis)实现多表联合查询

最近在做在eclipse + maven搭建SSM框架下做一个简单的后台管理系统,因为是第一次搭建SSM项目,在mybatis进行多表连接查询的时候遇到问题,不知道如何进行处理?在网上搜了一下解决方法是,以实现,记录下自己的实现方案。

  1. 表之间的关系如下图所示:

利用SSM(springmvc+spring+mybatis)实现多表联合查询_第1张图片

环境搭建的是eclipse + maven+SpringMVC + Spring + MyBatis,前端用到的是bootstrap框架。
因为是第一次做SSM项目没有用到 mybatis 的自动生成工具 mybatis generator 生成的实体类、mapper 接口、以及 mapper xml 文件。都是自己写的。

Project 类



import java.util.List;

import org.apache.ibatis.type.Alias;

/**
 * @author  作者:
 * @date 创建时间:2018年10月19日 
 * @version 1.0 
 * @author
 */
@Alias("Project")
public class Project {
	
	private String gid        ;
	private String activity  ;
	private String activity_end;
	private String src ;
	private String src2;
	private String src3;
	private String msgSuc;
	private String msgSuc1;
	private String jdr;
	private String createtime;
	private List lotterys;//这个是一个重点与Lottery实体类进行关联
	public String getGid() {
		return gid;
	}
	public void setGid(String gid) {
		this.gid = gid;
	}
	public String getActivity() {
		return activity;
	}
	public void setActivity(String activity) {
		this.activity = activity;
	}
	public String getSrc() {
		return src;
	}
	public void setSrc(String src) {
		this.src = src;
	}
	public String getActivity_end() {
		return activity_end;
	}
	public void setActivity_end(String activity_end) {
		this.activity_end = activity_end;
	}
	public String getSrc2() {
		return src2;
	}
	public void setSrc2(String src2) {
		this.src2 = src2;
	}
	public String getSrc3() {
		return src3;
	}
	public void setSrc3(String src3) {
		this.src3 = src3;
	}
	public String getMsgSuc() {
		return msgSuc;
	}
	public void setMsgSuc(String msgSuc) {
		this.msgSuc = msgSuc;
	}
	public String getMsgSuc1() {
		return msgSuc1;
	}
	public void setMsgSuc1(String msgSuc1) {
		this.msgSuc1 = msgSuc1;
	}
	public String getJdr() {
		return jdr;
	}
	public void setJdr(String jdr) {
		this.jdr = jdr;
	}
	public String getCreatetime() {
		return createtime;
	}
	public void setCreatetime(String createtime) {
		this.createtime = createtime;
	}
	
	public List getLottery() {
		return lotterys;
	}
	public void setLottery(List lotterys) {
		this.lotterys = lotterys;
	}
	@Override
	public String toString() {
		return "Project [gid=" + gid + ", activity=" + activity + ", activity_end=" + activity_end + ", src=" + src
				+ ", src2=" + src2 + ", src3=" + src3 + ", msgSuc=" + msgSuc + ", msgSuc1=" + msgSuc1 + ", jdr=" + jdr
				+ ", createtime=" + createtime + "]";
	}
	public Project(String gid, String activity, String activity_end, String src, String src2, String src3,
			String msgSuc, String msgSuc1, String jdr) {
		super();
		this.gid = gid;
		this.activity = activity;
		this.activity_end = activity_end;
		this.src = src;
		this.src2 = src2;
		this.src3 = src3;
		this.msgSuc = msgSuc;
		this.msgSuc1 = msgSuc1;
		this.jdr = jdr;
	}
	public Project() {
		super();
	}
	
}

Lottery类



import org.apache.ibatis.type.Alias;

/**
 * @author  作者:
 * @date 创建时间:2018年10月19日 
 * @version 1.0 
 * @author
 */
@Alias("Lottery")
public class Lottery {
	private String id;  
	private String project_id; 
	private String url; 
	private String settingId;
	private String title; 
	private String settingTitle;
	private String days_newCust; 
	private String days_oldCust;
	private String appid; //appid
	private String appSecret; 
	private String awardType; 
	private String status;
	private String mete;
	private String mutex;
	private String createtime;/
	private String jdr;
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getProject_id() {
		return project_id;
	}

	public void setProject_id(String project_id) {
		this.project_id = project_id;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getSettingId() {
		return settingId;
	}

	public void setSettingId(String settingId) {
		this.settingId = settingId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getSettingTitle() {
		return settingTitle;
	}

	public void setSettingTitle(String settingTitle) {
		this.settingTitle = settingTitle;
	}

	public String getDays_newCust() {
		return days_newCust;
	}

	public void setDays_newCust(String days_newCust) {
		this.days_newCust = days_newCust;
	}

	public String getDays_oldCust() {
		return days_oldCust;
	}

	public void setDays_oldCust(String days_oldCust) {
		this.days_oldCust = days_oldCust;
	}

	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getAppSecret() {
		return appSecret;
	}

	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}

	public String getAwardType() {
		return awardType;
	}

	public void setAwardType(String awardType) {
		this.awardType = awardType;
	}

	public String getStatus() {
		return status;
	}

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

	public String getMete() {
		return mete;
	}

	public void setMete(String mete) {
		this.mete = mete;
	}

	public String getMutex() {
		return mutex;
	}

	public void setMutex(String mutex) {
		this.mutex = mutex;
	}

	public String getCreatetime() {
		return createtime;
	}

	public void setCreatetime(String createtime) {
		this.createtime = createtime;
	}

	public String getJdr() {
		return jdr;
	}

	public void setJdr(String jdr) {
		this.jdr = jdr;
	}

	public Lottery() {
		super();
	}

	public Lottery(String id, String project_id, String url, String settingId, String title, String settingTitle,
			String days_newCust, String days_oldCust, String appid, String appSecret, String awardType, String status,
			String mete, String mutex, String jdr) {
		super();
		this.id = id;
		this.project_id = project_id;
		this.url = url;
		this.settingId = settingId;
		this.title = title;
		this.settingTitle = settingTitle;
		this.days_newCust = days_newCust;
		this.days_oldCust = days_oldCust;
		this.appid = appid;
		this.appSecret = appSecret;
		this.awardType = awardType;
		this.status = status;
		this.mete = mete;
		this.mutex = mutex;
		this.jdr = jdr;
	}

	
}

project 与 lottery 是1:1的关系,在 project zhong 加入一个lottery属性,对应一对一的关系。

然后就是mapper接口与xml文件

mapper :ProjectDao

public interface ProjectDao {
	
    
    //查询项目信息列表
    List selectAllProject();

}

xml ProjectDao.xml




      
          
          
          
          
          
          
              
              
              
              
              
              
               
                
          
      
    
    
        gid, activity, activity_end, src,src2,src3,msgSuc,msgSuc1,jdr,createtime
    
     
     
      delete from system_project
      where gid = #{gid}
    

service 层代码

public interface ProjectService {
	
	List selectAllProject();
}

service impl

@Service("projectService")
public class ProjectServiceImpl implements ProjectService {
     
	@Autowired
    public ProjectDao projectDao;

	@Override
	public List selectAllProject() {
		// TODO Auto-generated method stub
		return this.projectDao.selectAllProject();
	}

}

controler层往前端传的是json

	    @RequestMapping("/showProjectList")
	    @ResponseBody
	    public String GetDepartment()
        {
            List list = projectService.selectAllProject();
            Gson gson = new Gson();
            System.err.println("**********list*********"+gson.toJson(list));
	        System.err.println("**********list*********"+gson.toJson(list));
	        System.err.println("**********list*********"+gson.toJson(list));
			return  gson.toJson(list);
              
        }

传的json是复杂的嵌套类型如下图:
利用SSM(springmvc+spring+mybatis)实现多表联合查询_第2张图片

前端用到的了bootstrap框架


对于嵌套的json boostrap有处理方法需要如下操作:

{
                field: 'lotterys',
                title: '类型',
                formatter : function(value,row, index){ //主要配置在这里 
                	return  value[0].awardType; }
  }

最后的实现效果如下:
利用SSM(springmvc+spring+mybatis)实现多表联合查询_第3张图片
bootstrap 参考地址:https://www.cnblogs.com/landeanfen/p/4976838.html
本文实现方法参考文章:https://blog.csdn.net/u012661010/article/details/76408566

你可能感兴趣的:(SSM)