Commit 2c0f34f5 authored by wenmo's avatar wenmo

database crud

parent c13a0618
package com.dlink.controller;
import com.dlink.common.result.ProTableResult;
import com.dlink.common.result.Result;
import com.dlink.model.DataBase;
import com.dlink.service.DataBaseService;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
/**
* DataBaseController
*
* @author wenmo
* @since 2021/7/20 23:48
*/
@Slf4j
@RestController
@RequestMapping("/api/database")
public class DataBaseController {
@Autowired
private DataBaseService databaseService;
/**
* 新增或者更新
*/
@PutMapping
public Result saveOrUpdate(@RequestBody DataBase database) {
if(databaseService.saveOrUpdate(database)){
return Result.succeed("新增成功");
}else {
return Result.failed("新增失败");
}
}
/**
* 动态查询列表
*/
@PostMapping
public ProTableResult<DataBase> listDataBases(@RequestBody JsonNode para) {
return databaseService.selectForProTable(para);
}
/**
* 批量删除
*/
@DeleteMapping
public Result deleteMul(@RequestBody JsonNode para) {
if (para.size()>0){
List<Integer> error = new ArrayList<>();
for (final JsonNode item : para){
Integer id = item.asInt();
if(!databaseService.removeById(id)){
error.add(id);
}
}
if(error.size()==0) {
return Result.succeed("删除成功");
}else {
return Result.succeed("删除部分成功,但"+error.toString()+"删除失败,共"+error.size()+"次失败。");
}
}else{
return Result.failed("请选择要删除的记录");
}
}
/**
* 获取指定ID的信息
*/
@PostMapping("/getOneById")
public Result getOneById(@RequestBody DataBase database) {
database = databaseService.getById(database.getId());
return Result.succeed(database,"获取成功");
}
}
\ No newline at end of file
package com.dlink.mapper;
import com.dlink.db.mapper.SuperMapper;
import com.dlink.model.DataBase;
import org.apache.ibatis.annotations.Mapper;
/**
* DataBaseMapper
*
* @author wenmo
* @since 2021/7/20 23:43
*/
@Mapper
public interface DataBaseMapper extends SuperMapper<DataBase> {
}
package com.dlink.model;
import com.baomidou.mybatisplus.annotation.TableName;
import com.dlink.db.model.SuperEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* DataBase
*
* @author wenmo
* @since 2021/7/20 20:53
*/
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("dlink_database")
public class DataBase extends SuperEntity {
private static final long serialVersionUID = -5002272138861566408L;
private String alias;
private String groupName;
private String type;
private String ip;
private Integer port;
private String url;
private String username;
private String password;
private String note;
private String dbVersion;
}
package com.dlink.service;
import com.dlink.db.service.ISuperService;
import com.dlink.model.DataBase;
/**
* DataBaseService
*
* @author wenmo
* @since 2021/7/20 23:47
*/
public interface DataBaseService extends ISuperService<DataBase> {
}
package com.dlink.service.impl;
import com.dlink.db.service.impl.SuperServiceImpl;
import com.dlink.mapper.DataBaseMapper;
import com.dlink.model.DataBase;
import com.dlink.service.DataBaseService;
import org.springframework.stereotype.Service;
/**
* DataBaseServiceImpl
*
* @author wenmo
* @since 2021/7/20 23:47
*/
@Service
public class DataBaseServiceImpl extends SuperServiceImpl<DataBaseMapper, DataBase> implements DataBaseService {
}
<?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.dlink.mapper.DataBaseMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.dlink.model.DataBase">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="alias" property="alias" />
<result column="group_name" property="groupName" />
<result column="type" property="type" />
<result column="ip" property="ip" />
<result column="port" property="port" />
<result column="url" property="url" />
<result column="username" property="username" />
<result column="password" property="password" />
<result column="db_version" property="dbVersion" />
<result column="note" property="note" />
<result column="enabled" property="enabled" />
<result column="create_time" property="createTime" />
<result column="update_time" property="updateTime" />
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id, name, alias,group_name, type,ip,port, url,username,password,db_version,note, enabled, create_time, update_time
</sql>
<select id="selectForProTable" resultType="com.dlink.model.DataBase">
select
a.*
from
dlink_database a
<where>
1=1
<if test='param.name!=null and param.name!=""'>
and a.name like "%${param.name}%"
</if>
<if test='param.alias!=null and param.alias!=""'>
and a.alias like "%${param.alias}%"
</if>
<if test='param.createTime!=null and param.createTime!=""'>
and a.create_time <![CDATA[>=]]> str_to_date( #{param.createTime},'%Y-%m-%d %H:%i:%s')
</if>
<if test='param.updateTime!=null and param.updateTime!=""'>
and a.update_time <![CDATA[>=]]> str_to_date( #{param.updateTime},'%Y-%m-%d %H:%i:%s')
</if>
<if test='ew.sqlSegment!=null and ew.sqlSegment!="" and !ew.sqlSegment.startsWith(" ORDER BY")'>
and
</if>
<if test='ew.sqlSegment!=null and ew.sqlSegment!=""'>
${ew.sqlSegment}
</if>
</where>
</select>
</mapper>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment