java mysql mybatis 使用记录 (一)

mysql 时间格式化:

date_format(i.createTime,"%Y-%m-%d %H:%i:%S") createTime

mybatis 注解版实现多表联查

备注(只适用简单查询,比如做非空判断就比较麻烦)

  @Select("select  i.id,i.title,i.content,i.image,i.createTime,i.author,t.name from info i,info_type t where typeId = #{typeId})")
    List<Map<String,Object>> infoAndType(@Param("typeId") Integer typeId);

mybatis xml 实现多表联查

备注(适用比较复杂的查询)

     <select id="infoAndType" resultType="java.util.Map">
       select  i.id,i.typeId,i.title,i.content,i.image,date_format(i.createTime,"%Y-%m-%d %H:%i:%S") createTime,i.author,t.name typeName from info i,info_type t
       where i.typeId=t.id
       <if test="status !=null">
           and i.status=#{status}
       </if>
       <if test="typeId !=null">
        and i.typeId=#{typeId}
       </if>
         order by i.createTime desc
     </select>

xml 常用的返回两种类型

<select id="infoAndType" resultType="java.util.Map">

返回 Map集合,适用于复杂的、自定义,且与实体类出入比较大的。

前台Map接收

List<Map<String,Object>> infoAndType(@Param("typeId") Integer typeId,
                                         @Param("status") Integer status);
 <select id="infoAndType" resultMap="BaseResultMap">

 返回实体类,适用比较简单、非定义的结果。

 前台实体类接收

 List<info> infoAndType(@Param("typeId") Integer typeId,
                                         @Param("status") Integer status);
接收接口URL参数
前端接口传参
var typeId = $('#mdoelTypeEdtSearch').val();
admin.jsonReq(RD.url + 'kayu/product/copyAll?typeId='+ typeId +'', JSON.stringify(idData), function (data) {
    layer.closeAll('loading');
    if (data.code == 200) {
        layer.msg(data.msg, { icon: 1 });
        table.reload("roleTable");
    } else {
        layer.msg(data.msg, { icon: 2 });
    }
}, 'post');

后端接收typeId传参
@PostMapping("copyAll")
public JsonResult copyAll(@RequestBody List<Integer> ids){
    String typeId = request.getParameter("typeId");//接收接口URL参数
    Integer typeIdInt = null;
    if (StringUtil.isNotNull(typeId)) {
        typeIdInt = Integer.parseInt(typeId);
    }
    return productService.copyAll(ids,typeIdInt);
}

复制数据

ProductService  

public JsonResult copyAll(List<Integer> ids, Integer typeId) {
        List<Product> proList = productMapper.selectBatchIds(ids);//找出所有ids的列
        if (typeId != null) {
            for (Product pro : proList) {
                pro.setTypeId(typeId);//更改typeId为传过来的值typeId
            }
        }
        productMapper.insertBatch(proList);
        return JsonResult.ok("复制成功!");
    }

xml

    <insert id="insertBatch" parameterType="List">
        insert into product(title,content,createTime,updateTime,typeId,model,status,title02,title03,content2,content3) VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.title},#{item.content},#{item.createTime},#{item.updateTime},#{item.typeId},#{item.model},
            #{item.status},#{item.title02},#{item.title03},#{item.content2},#{item.content3}
            )
        </foreach>
    </insert>

返回一个对象 非list

public JsonResult getWebUrl(Setting setting){
EntityWrapper<Setting> entityWrapper = new EntityWrapper<>();//mybatis plus 条件构造器
		entityWrapper.eq("parent_id",setting.getId());
		List<Setting> list = settingMapper.selectList(entityWrapper);// 找到符合这个条件的所有数据
		JSONObject jsonObject = new JSONObject();
		for (int i = 0; i < list.size(); i++) {
			jsonObject.put(list.get(i).getName(), list.get(i).getValue());
		}
		return JsonResult.ok("查询成功").put("data",jsonObject);

	}

前台接收示列

返回JsonResult  list

public JsonResult getWebUrl(Setting setting){
EntityWrapper<Setting> entityWrapper = new EntityWrapper<>();
		entityWrapper.eq("parent_id",setting.getId());
		List<Setting> list = settingMapper.selectList(entityWrapper);
		List<JSONObject> jsonList = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			JSONObject jsonObject = new JSONObject();
			jsonObject.put(list.get(i).getName(), list.get(i).getValue());
			jsonList.add(jsonObject);
		}
		return JsonResult.ok("查询成功").put("data",jsonList);

	}

前台接收示列

发布了87 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_38188762/article/details/99742540