Spring MVC + Mybatis 一个简单的例子 — 返回Json数据

定义实体类

package com.test.item.dto;

import java.util.Date;

/**
 * @author Duoduo
 * @version 1.0
 * @date 2017/4/16 12:41
 */
public class Item {

    private Long itemId;
    private String itemCode;
    private String itemName;
    private double price;
    private String detail;
    private Date creationDate;

    public Long getItemId() {
        return itemId;
    }

    public void setItemId(Long itemId) {
        this.itemId = itemId;
    }

    public String getItemCode() {
        return itemCode;
    }

    public void setItemCode(String itemCode) {
        this.itemCode = itemCode;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public double getPrice() {
        return price;
    }

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

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }
}

定义Mapper文件 ItemMapper.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.test.mapper.ItemMapper">
    <resultMap id="BaseResultMap" type="com.test.item.dto.Item">
        <result column="item_id" property="itemId" jdbcType="DECIMAL"/>
        <result column="item_code" property="itemCode" jdbcType="VARCHAR"/>
        <result column="item_name" property="itemName" jdbcType="VARCHAR"/>
        <result column="price" property="price" jdbcType="REAL"/>
        <result column="detail" property="detail" jdbcType="VARCHAR"/>
    </resultMap>

    <select id="selectItems" resultMap="BaseResultMap" parameterType="com.test.item.dto.Item">
        select t.item_id, t.item_code, t.item_name, t.price, t.detail from items t
    </select>

</mapper>

定义Mapper类

注意:类的名称必须和Mapper文件名称相同,且函数名称必须和Mapper文件中的函数一致=

package com.test.mapper;

import com.test.item.dto.Item;

import java.util.List;

/**
 * @author Duoduo
 * @version 1.0
 * @date 2017/4/18 17:02
 */
public interface ItemMapper  {

    List<Item> selectItems(Item item);
}

定义Service类 IItemService

package com.test.item.service;

import com.test.item.dto.Item;

import java.util.List;

/**
 * @author Duoduo
 * @version 1.0
 * @date 2017/4/18 17:04
 */
public interface IItemService {

    List<Item> selectItems(Item item);
}

定义Service实现类

注意:此处必须要有注解@Service和@Transactional,否则会报错找不到Service

package com.test.item.service.impl;

import com.test.item.dto.Item;
import com.test.item.service.IItemService;
import com.test.mapper.ItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Duoduo
 * @version 1.0
 * @date 2017/4/18 17:05
 */
@Service
@Transactional
public class ItemServiceImpl implements IItemService {
    @Autowired
    private   ItemMapper itemMapper;

    public List<Item> selectItems(Item item) {
        return itemMapper.selectItems(item);
    }
}

定义Controller类

package com.test.item.controllers;

import com.system.dto.ResponseData;
import com.test.item.dto.Item;
import com.test.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Duoduo
 * @version 1.0
 * @date 2017/4/18 17:09
 */
@Controller
public class ItemController {

    @Autowired
    private IItemService service;

    @RequestMapping("/com/item/query")
    @ResponseBody
    public ResponseData query(Item item){
        return new ResponseData(service.selectItems(item));
    }
}

在浏览器中运行http://localhost:8080/com/item/query
得到如下结果

{“rows”:[{“itemId”:1,”itemCode”:”10001”,”itemName”:”item name 1001”,”price”:3000.0,”detail”:”好雨知时节”,”creationDate”:null},{“itemId”:2,”itemCode”:”10002”,”itemName”:”item name 1002”,”price”:6000.0,”detail”:”当春乃发生”,”creationDate”:null},{“itemId”:3,”itemCode”:”10003”,”itemName”:”item name 1003”,”price”:200.0,”detail”:”随风潜入夜”,”creationDate”:null},{“itemId”:4,”itemCode”:”10004”,”itemName”:”item name 1004”,”price”:100.0,”detail”:”万物润无声”,”creationDate”:null}]}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值