ssm前后端分离之数据库关联查询及客户端获取数据

一,服务端代码

   1,实体类书写:FlowersInfo.java,DataResult.java

package com.meng.entity;

import java.util.List;

public class DataResult<T> {
	
	public DataResult() {}
public DataResult(int total, List<T> rows) {
 
		this.total = total;
		this.rows = rows;
	}
private int total;
private List<T> rows;
public int getTotal() {
	return total;
}
public void setTotal(int total) {
	this.total = total;
}
public List<T> getRows() {
	return rows;
}
public void setRows(List<T> rows) {
	this.rows = rows;
}
}
package com.meng.entity;

import org.springframework.web.multipart.MultipartFile;

public class FlowersInfo {
	//鲜花编号
    private Integer flowerId;
    //鲜花名
    private String flowerName;
    //存储量
    private Integer flowerNum;
    //鲜花类型
    private Integer flowerType;
    //价格
    private Long price;
    //用户Id
    private Integer userId;
    //状态
    private Boolean status;
    //描述
    private String des;
    
    //关联查询出用户信息
    private UserInfo userInfo;
    //关联查询鲜花类型信息
    private FlowersTypeInfo flowersTypeInfo;
    
    
    public FlowersTypeInfo getFlowersTypeInfo() {
		return flowersTypeInfo;
	}

	public void setFlowersTypeInfo(FlowersTypeInfo flowersTypeInfo) {
		this.flowersTypeInfo = flowersTypeInfo;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	//图片上传实体
    private MultipartFile  flowerImage;
    
    

    public MultipartFile getFlowerImage() {
		return flowerImage;
	}

	public void setFlowerImage(MultipartFile flowerImage) {
		this.flowerImage = flowerImage;
	}

	public Integer getFlowerId() {
        return flowerId;
    }

    public void setFlowerId(Integer flowerId) {
        this.flowerId = flowerId;
    }

    public String getFlowerName() {
        return flowerName;
    }

    public void setFlowerName(String flowerName) {
        this.flowerName = flowerName == null ? null : flowerName.trim();
    }

    public Integer getFlowerNum() {
        return flowerNum;
    }

    public void setFlowerNum(Integer flowerNum) {
        this.flowerNum = flowerNum;
    }

    public Integer getFlowerType() {
        return flowerType;
    }

    public void setFlowerType(Integer flowerType) {
        this.flowerType = flowerType;
    }

    public Long getPrice() {
        return price;
    }

    public void setPrice(Long price) {
        this.price = price;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public Boolean getStatus() {
        return status;
    }

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

    public String getDes() {
        return des;
    }

    public void setDes(String des) {
        this.des = des == null ? null : des.trim();
    }

	@Override
	public String toString() {
		return "FlowersInfo [flowerId=" + flowerId + ", flowerName=" + flowerName + ", flowerNum=" + flowerNum
				+ ", flowerType=" + flowerType + ", price=" + price + ", userId=" + userId + ", status=" + status
				+ ", des=" + des + ", userInfo=" + userInfo + ", flowersTypeInfo=" + flowersTypeInfo + ", flowerImage="
				+ flowerImage + "]";
	}

	
    
    
}

   2,controller

package com.meng.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.meng.entity.DataResult;
import com.meng.entity.FlowersInfo;
import com.meng.service.FlowersInfoService;

@RestController
@RequestMapping("/flowers")
public class FlowerInfoController {

	@Autowired
	private FlowersInfoService flowersInfoService;

	// http://localhost:8080/flower/flowers/getAll
	@RequestMapping(value = "/getAll", method = RequestMethod.GET)
	public DataResult<FlowersInfo> get() {
		List<FlowersInfo> list = flowersInfoService.getAllFlowers();
		return new DataResult<FlowersInfo>(list.size(), list);
	}

	// http://localhost:8080/flower/flowers/getByName--搜索框
	@RequestMapping(value = "/getByName/{flowername}", method = RequestMethod.GET)
	public List<FlowersInfo> getByName(@PathVariable("flowername") String flowerName) {
		return flowersInfoService.getAllFlowersByName(flowerName);
	}

	// http://localhost:8080/flower/flowers/searchByName--查询按钮
	@RequestMapping(value = "/searchByName/{flowername}", method = RequestMethod.GET)
	public DataResult<FlowersInfo> searchByName(@PathVariable("flowername") String flowerName) {
		List<FlowersInfo> list = flowersInfoService.getAllFlowersByName(flowerName);
		return new DataResult<FlowersInfo>(list.size(), list);
	}

	// http://localhost:8080/flower/flowers/add
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public void add(FlowersInfo flower, HttpServletRequest request) {
		String fileName = flower.getFlowerImage().getOriginalFilename();
		String filePath = request.getServletContext().getRealPath("files");
		File file = new File(filePath, fileName);
		try {
			flower.getFlowerImage().transferTo(file);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		flowersInfoService.addFlowers(flower);
	}

	// http://localhost:8080/flower/flower/update
	@RequestMapping(value = "/update", method = RequestMethod.PUT)
	public void update(FlowersInfo flower) {
		flowersInfoService.updateFlowers(flower);
	}

	// http://localhost:8080/flower/flowers/delete
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
	public void delete(@PathVariable(value = "id") Integer flowerId) {
		flowersInfoService.deleteFlowers(flowerId);
	}
}

   3,service

package com.meng.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.meng.dao.FlowersInfoMapper;
import com.meng.entity.FlowersInfo;

@Service
public class FlowersInfoService {
	
	
	@Autowired
	private FlowersInfoMapper flowersInfoMapper;

	public List<FlowersInfo> getAllFlowers() {
		// TODO Auto-generated method stub
		return flowersInfoMapper.selectAllFlowers();
	}

	public void addFlowers(FlowersInfo flower) {
		// TODO Auto-generated method stub
		flowersInfoMapper.insert(flower);
	}

	public void updateFlowers(FlowersInfo flower) {
		// TODO Auto-generated method stub
		flowersInfoMapper.updateByPrimaryKey(flower);
	}

	public void deleteFlowers(Integer flowerId) {
		// TODO Auto-generated method stub
		flowersInfoMapper.deleteByPrimaryKey(flowerId);
	}

	public List<FlowersInfo> getAllFlowersByName(String flowerName) {
		// TODO Auto-generated method stub
		return flowersInfoMapper.selectByKeywords(flowerName);
	}

}

   4,dao

package com.meng.dao;

import java.util.List;

import com.meng.entity.FlowersInfo;

public interface FlowersInfoMapper {
    

    int insert(FlowersInfo record);

    int insertSelective(FlowersInfo record);

    FlowersInfo selectByPrimaryKey(Integer flowerId);

    int updateByPrimaryKeySelective(FlowersInfo record);

    int updateByPrimaryKeyWithBLOBs(FlowersInfo record);

    int updateByPrimaryKey(FlowersInfo record);

	List<FlowersInfo> selectAllFlowers();

	int deleteByPrimaryKey(Integer flowerId);

	List<FlowersInfo> selectByKeywords(String flowerName);
}

   5,mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.meng.dao.FlowersInfoMapper" >
  <resultMap id="BaseResultMap" type="com.meng.entity.FlowersInfo" >
    <id column="flower_id" property="flowerId" jdbcType="INTEGER" />
    <result column="flower_name" property="flowerName" jdbcType="VARCHAR" />
    <result column="flower_num" property="flowerNum" jdbcType="INTEGER" />
    <result column="flower_type" property="flowerType" jdbcType="INTEGER" />
    <result column="price" property="price" jdbcType="DECIMAL" />
    <result column="user_id" property="userId" jdbcType="INTEGER" />
    <result column="status" property="status" jdbcType="BIT" />
    <association property="userInfo" javaType="UserInfo" column="user_id">
    	<id column="user_id" property="userId"/>
    	<result column="real_name" property="realName"/>
    </association>
    <association property="flowersTypeInfo" javaType="FlowersTypeInfo" column="flower_type">
    	<id column="flower_type" property="flowerType"/>
    	<result column="type_name" property="typeName"/>
    </association>
  </resultMap>
  <resultMap id="ResultMapWithBLOBs" type="com.meng.entity.FlowersInfo" extends="BaseResultMap" >
    <result column="des" property="des" jdbcType="LONGVARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    flower_id, flower_name, flower_num, flower_type, price, user_id, status
  </sql>
  <sql id="Blob_Column_List" >
    des
  </sql>
  
  <select id="selectByKeywords" parameterType="String" resultMap="ResultMapWithBLOBs">
  	SELECT 
  		f.flower_num, f.flower_id,f.flower_name,u.real_name,t.type_name,f.price,f.des
	FROM
		flowers f
	INNER JOIN	userinfo u
	ON 
		(f.user_id = u.user_id)
	INNER JOIN	flowertype t
	ON 
		(t.flower_type = f.flower_type)
	where flower_name like CONCAT('%',#{flowerName},'%')
  </select>
  <select id="selectAllFlowers" resultMap="ResultMapWithBLOBs">
  	SELECT 
  		f.flower_num, f.flower_id,f.flower_name,u.real_name,t.type_name,f.price,f.des
	FROM
		flowers f
	INNER JOIN	userinfo u
	ON 
		(f.user_id = u.user_id)
	INNER JOIN	flowertype t
	ON 
		(t.flower_type = f.flower_type)
  </select>
  
  <select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from flowers
    where flower_id = #{flowerId,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from flowers
    where flower_id = #{flowerId,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.meng.entity.FlowersInfo" >
    insert into flowers (flower_id, flower_name, flower_num, 
      flower_type, price, user_id, 
      status, des)
    values (#{flowerId,jdbcType=INTEGER}, #{flowerName,jdbcType=VARCHAR}, #{flowerNum,jdbcType=INTEGER}, 
      #{flowerType,jdbcType=INTEGER}, #{price,jdbcType=DECIMAL}, #{userId,jdbcType=INTEGER}, 
      #{status,jdbcType=BIT}, #{des,jdbcType=LONGVARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.meng.entity.FlowersInfo" >
    insert into flowers
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="flowerId != null" >
        flower_id,
      </if>
      <if test="flowerName != null" >
        flower_name,
      </if>
      <if test="flowerNum != null" >
        flower_num,
      </if>
      <if test="flowerType != null" >
        flower_type,
      </if>
      <if test="price != null" >
        price,
      </if>
      <if test="userId != null" >
        user_id,
      </if>
      <if test="status != null" >
        status,
      </if>
      <if test="des != null" >
        des,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="flowerId != null" >
        #{flowerId,jdbcType=INTEGER},
      </if>
      <if test="flowerName != null" >
        #{flowerName,jdbcType=VARCHAR},
      </if>
      <if test="flowerNum != null" >
        #{flowerNum,jdbcType=INTEGER},
      </if>
      <if test="flowerType != null" >
        #{flowerType,jdbcType=INTEGER},
      </if>
      <if test="price != null" >
        #{price,jdbcType=DECIMAL},
      </if>
      <if test="userId != null" >
        #{userId,jdbcType=INTEGER},
      </if>
      <if test="status != null" >
        #{status,jdbcType=BIT},
      </if>
      <if test="des != null" >
        #{des,jdbcType=LONGVARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.meng.entity.FlowersInfo" >
    update flowers
    <set >
      <if test="flowerName != null" >
        flower_name = #{flowerName,jdbcType=VARCHAR},
      </if>
      <if test="flowerNum != null" >
        flower_num = #{flowerNum,jdbcType=INTEGER},
      </if>
      <if test="flowerType != null" >
        flower_type = #{flowerType,jdbcType=INTEGER},
      </if>
      <if test="price != null" >
        price = #{price,jdbcType=DECIMAL},
      </if>
      <if test="userId != null" >
        user_id = #{userId,jdbcType=INTEGER},
      </if>
      <if test="status != null" >
        status = #{status,jdbcType=BIT},
      </if>
      <if test="des != null" >
        des = #{des,jdbcType=LONGVARCHAR},
      </if>
    </set>
    where flower_id = #{flowerId,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKeyWithBLOBs" parameterType="com.meng.entity.FlowersInfo" >
    update flowers
    set flower_name = #{flowerName,jdbcType=VARCHAR},
      flower_num = #{flowerNum,jdbcType=INTEGER},
      flower_type = #{flowerType,jdbcType=INTEGER},
      price = #{price,jdbcType=DECIMAL},
      user_id = #{userId,jdbcType=INTEGER},
      status = #{status,jdbcType=BIT},
      des = #{des,jdbcType=LONGVARCHAR}
    where flower_id = #{flowerId,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.meng.entity.FlowersInfo" >
    update flowers
    set flower_name = #{flowerName,jdbcType=VARCHAR},
      flower_num = #{flowerNum,jdbcType=INTEGER},
      flower_type = #{flowerType,jdbcType=INTEGER},
      price = #{price,jdbcType=DECIMAL},
      user_id = #{userId,jdbcType=INTEGER},
      status = #{status,jdbcType=BIT}
    where flower_id = #{flowerId,jdbcType=INTEGER}
  </update>
</mapper>

三,easyUI前端获取数据

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>鲜花界面</title>
<link rel="stylesheet" type="text/css"
	href="js/jquery-easyui-1.5.5.4/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="js/jquery-easyui-1.5.5.4/themes/icon.css">
<link rel="stylesheet" type="text/css"
	href="js/jquery-easyui-1.5.5.4/demo/demo.css">

<script type="text/javascript"
	src="js/jquery-easyui-1.5.5.4/jquery.min.js"></script>
<script type="text/javascript"
	src="js/jquery-easyui-1.5.5.4/jquery.easyui.min.js"></script>
<script type="text/javascript">
	//http://localhost:8080/flower_front/flowerList.jsp
	/*
		var url = "http://localhost:8080/flower/flower/getAll";
	$.getJSON(url, function(json) {
		var strHtml = "";

		//多条数据遍历
		$.each(json, function(i, item) {
			
			strHtml += item.flowerId + " " + item.flowerName + " "
					+ item.flowerNum + "</br>"
		});
		$("#divInfo").html(strHtml);
	});
	 */

	$(function() {
		var url = "http://localhost:8080/flower/flowers/getAll";
		$('#table').datagrid({
			url : url,
			method : "get",
			pagePosition : 'bottom',
			pageSize : 10,
			pageList : [ 10, 15, 20 ],
			pagination : true,
			columns : [ [ {
				field : 'flowerId',
				title : '鲜花编号',
				width : 100
			}, {
				field : 'flowerImage',
				title : '鲜花图片',
				width : 100
			}, {
				field : 'flowerName',
				title : '鲜花名称',
				width : 100
			}, {
				field : 'flowerNum',
				title : '储存量',
				width : 100
			}, {
				field : 'flowersTypeInfo',
				title : '鲜花类型',
				width : 100,
				formatter:function(value){return value.typeName}
			}, {
				field : 'price',
				title : '价格',
				width : 100
			}, {
				field : 'userInfo',
				title : '用户名',
				width : 100,
				formatter:function(value){return value.realName}
			}, {
				field : 'des',
				title : '详情',
				width : 100
			}

			] ]
		});

		var p = $("#table").datagrid("getPager");
		$(p).pagination(
				{
					onSelectPage : function(pageNumber, pageSize) {
						alert('onSelect pageNumber:' + pageNumber
								+ ',pageSize:' + pageSize);
						getData(10, pageSize);
					}

				});
	
	});
	 
	 
</script>
<script type="text/javascript">


	function setValue(obj) {
		$("#flowername").val(obj.innerHTML);
	}
	$(function(){
		
		
		//关键字查询
		$("#flowername").keyup(function() {

							if (this.value == "") {
								$("#divInfo").hide();
								return;
							}

							var url = "http://localhost:8080/flower/flowers/getByName/"+ this.value;
							
							$.getJSON(url,function(json) {
								
								var strHtml = "";
								$.each(json,function(i,item) {
									strHtml+="<div><a href='#' onclick=\"setValue(this);\">"+item.flowerName+"</a></div>";
									});
												$("#divInfo").html(strHtml);
												$("#divInfo").show();

											});
						});

	
	});
	
	

</script>
<script type="text/javascript">
	$(function(){
		$('#btnSearch').bind('click', function(){
			
			var url = "http://localhost:8080/flower/flowers/searchByName/"+ $("#flowername").val();
			$('#table').datagrid({
				url : url,
				method : "get",
				pagePosition : 'bottom',
				pageSize : 10,
				pageList : [ 10, 15, 20 ],
				pagination : true,
				columns : [ [ {
					field : 'flowerId',
					title : '鲜花编号',
					width : 100
				}, {
					field : 'flowerImage',
					title : '鲜花图片',
					width : 100
				}, {
					field : 'flowerName',
					title : '鲜花名称',
					width : 100
				}, {
					field : 'flowerNum',
					title : '储存量',
					width : 100
				}, {
					field : 'flowerType',
					title : '鲜花类型',
					width : 100
				}, {
					field : 'price',
					title : '价格',
					width : 100
				}, {
					field : 'userId',
					title : '用户Id',
					width : 100
				}, {
					field : 'des',
					title : '详情',
					width : 100
				}

				] ]
			});	
			});
	    });
	

</script>
<style type="text/css">
	a{
		text-decoration: none;
	
	}
</style>
</head>
<body>

	<div id="p" class="easyui-panel" title="高级查询">
	
	<div style="float:left;">
	鲜花类型:
	</div>
	<div style="float:left;">
		<select>
			<option>类型1</option>
			<option>类型2</option>
		</select>
	</div>
	
		<div style="float:left;">
	 鲜花名称:
	</div>
		<div style="float:left;">
				<input type="text" id="flowername" /> 
		<input type="button"
			id="btnSearch" value="查询" />
		
		<div id="divInfo"   style="display: none; border:solid 1px;"></div>
		</div>
		
	</div>

	<table id="table">
	</table>




</body>
</html>

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页