您现在的位置是:网站首页>JavaJava

mybatis使用JSONObject返回数据结果整合

左鹏04-01 10:42:08Java128人已围观

简介整合mybatis与JSONObject,不需要单独建立数据类 一、引入相关包 pom.xml <!--fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.79</version> </dependency>

整合mybatis与JSONObject,不需要单独建立数据类

一、引入相关包 pom.xml

<!--fastjson-->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.2.79</version>
</dependency>

<!--mybatis-->
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>2.2.2</version>
</dependency>

二、创建 UserMapper interface

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {

    Integer insert(JSONObject param);

    List<JSONObject> list(JSONObject param);

    Integer update(JSONObject param);

    JSONObject firstById(int id);
}

三、创建 UserMapper.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.xxx.mapper.UserMapper">
    <insert id="insert" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        INSERT INTO `users` (`password`,`nickname`,`head_pic`,`ip`,`login_at`)
        VALUES (#{password},#{nickname},#{headPic},#{ip},#{loginAt})
    </insert>
    <update id="update">
        UPDATE `users` SET `nickname`=#{nickname},`head_pic`=#{headPic},`ip`=#{ip},`login_at`=#{loginAt} WHERE `id`=#{id}
    </update>

    <select id="list" resultType="com.alibaba.fastjson.JSONObject" parameterType="com.alibaba.fastjson.JSONObject">
        select * from `users`
        <if test="where != null">
            where ${where}
        </if>
        <if test="orderBy != null" >
            order by ${orderBy}
        </if>
        <if test="limit != null" >
            limit ${limit}
        </if>
    </select>

    <select id="firstById" resultType="com.alibaba.fastjson.JSONObject">
        select * from `users` where `id`=#{id}
    </select>
	
</mapper>

四、使用

import com.alibaba.fastjson.JSONObject;
import com.xxx.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("test")
public class Test {

    @Autowired
    UserMapper userMapper;

    public void test(){
        //插入数据
        JSONObject user = new JSONObject();
        user.put("password","123456");
        user.put("nickname","昵称");
        user.put("headPic","头像");
        user.put("ip", "登录IP");
        user.put("loginAt","登录时间");
        userMapper.insert(user);

        //更新数据
        user = new JSONObject();
        user.put("id",1);
        user.put("nickname","昵称2");
        user.put("head_pic","头像2");
        user.put("ip", "登录IP2");
        user.put("login_at","登录时间2");
        userMapper.update(user);

        //查询数据
        user = userMapper.firstById(1);

        System.out.println(user.toJSONString());
    }
}

完毕!

 

文章评论

    请先说点什么
    热门评论
    0人参与,0条评论
    正在载入评论列表...

    站点信息

    • 建站时间:2018-09-18
    • 网站程序:Spring Boot
    • 主题模板:《今夕何夕》
    • 文章统计:99条
    • 微信公众号:扫描二维码,关注我们
    登陆您的账户