调整数据库字段,优化部分接口 #1

Merged
xiaohai2271 merged 33 commits from dev into master 2020-05-27 16:45:03 +08:00
4 changed files with 111 additions and 22 deletions
Showing only changes of commit 0adf936085 - Show all commits

View File

@@ -17,11 +17,12 @@ public class WebUpdate {
private Date updateTime;
private boolean delete;
public WebUpdate() {
}
public WebUpdate(String updateInfo, Date updateTime) {
public WebUpdate(String updateInfo) {
this.updateInfo = updateInfo;
this.updateTime = updateTime;
}
}

View File

@@ -27,5 +27,7 @@ public interface WebUpdateInfoMapper {
List<WebUpdate> findAll();
List<WebUpdate> findAllNotDeleted();
WebUpdate getLastestOne();
}

View File

@@ -2,47 +2,53 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.celess.blog.mapper.WebUpdateInfoMapper">
<resultMap id="webUpdateResultMap" type="cn.celess.blog.entity.WebUpdate">
<id column="update_id" property="id"/>
<result column="update_info" property="updateInfo"/>
<result column="update_time" property="updateTime"/>
<id column="wu_id" property="id"/>
<result column="wu_info" property="updateInfo"/>
<result column="wu_time" property="updateTime"/>
<result column="is_delete" property="delete"/>
</resultMap>
<insert id="insert" parameterType="cn.celess.blog.entity.WebUpdate">
insert into web_update(update_info, update_time)
values (#{updateInfo}, #{updateTime})
<selectKey resultType="java.lang.Long" keyProperty="id">
SELECT LAST_INSERT_ID() AS id
</selectKey>
<insert id="insert" parameterType="cn.celess.blog.entity.WebUpdate" useGeneratedKeys="true" keyProperty="id">
insert into web_update(wu_info, wu_time, is_delete)
values (#{updateInfo}, now(), false)
</insert>
<update id="update">
update web_update
set update_info=#{info}
where update_id = #{id};
set wu_info=#{info}
where wu_id = #{id};
</update>
<delete id="delete">
delete
from web_update
where update_id = #{id}
</delete>
<update id="delete">
update web_update
set is_delete = true
where wu_id = #{id}
</update>
<select id="existsById" resultType="java.lang.Boolean">
select EXISTS(select * from web_update where update_id = #{id})
select EXISTS(select * from web_update where wu_id = #{id})
</select>
<select id="findById" resultMap="webUpdateResultMap">
select *
from web_update
where update_id = #{id}
where wu_id = #{id}
</select>
<select id="findAll" resultMap="webUpdateResultMap">
select *
from web_update
</select>
<select id="findAllNotDeleted" resultMap="webUpdateResultMap">
select *
from web_update
where is_delete = false
</select>
<select id="getLastestOne" resultMap="webUpdateResultMap">
select *
from web_update
order by update_id desc
order by wu_id desc
limit 1
</select>

View File

@@ -0,0 +1,80 @@
package cn.celess.blog.mapper;
import cn.celess.blog.BaseTest;
import cn.celess.blog.entity.WebUpdate;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import static org.junit.Assert.*;
public class WebUpdateInfoMapperTest extends BaseTest {
@Autowired
WebUpdateInfoMapper webUpdateInfoMapper;
@Test
public void insert() {
WebUpdate webUpdate = generateUpdateInfo();
assertNotNull(webUpdate);
}
@Test
public void delete() {
WebUpdate webUpdate = generateUpdateInfo();
assertEquals(1, webUpdateInfoMapper.delete(webUpdate.getId()));
assertTrue(webUpdateInfoMapper.findById(webUpdate.getId()).isDelete());
}
@Test
public void update() {
WebUpdate webUpdate = generateUpdateInfo();
assertEquals(1, webUpdateInfoMapper.update(webUpdate.getId(), randomStr(6)));
}
@Test
public void existsById() {
WebUpdate webUpdate = generateUpdateInfo();
assertTrue(webUpdateInfoMapper.existsById(webUpdate.getId()));
}
@Test
public void findById() {
WebUpdate webUpdate = generateUpdateInfo();
WebUpdate byId = webUpdateInfoMapper.findById(webUpdate.getId());
assertEquals(webUpdate.getUpdateInfo(), byId.getUpdateInfo());
assertEquals(webUpdate.getId(), byId.getId());
}
@Test
public void findAll() {
List<WebUpdate> all = webUpdateInfoMapper.findAll();
assertNotEquals(0, all.size());
}
@Test
public void findAllNotDeleted() {
this.delete();
List<WebUpdate> allNotDeleted = webUpdateInfoMapper.findAllNotDeleted();
allNotDeleted.forEach(webUpdate -> assertFalse(webUpdate.isDelete()));
}
@Test
public void getLastestOne() {
WebUpdate webUpdate = generateUpdateInfo();
List<WebUpdate> all = webUpdateInfoMapper.findAll();
List<WebUpdate> allNotDeleted = webUpdateInfoMapper.findAllNotDeleted();
all.sort(((o1, o2) -> (int) (o2.getId() - o1.getId())));
allNotDeleted.sort(((o1, o2) -> (int) (o2.getId() - o1.getId())));
assertEquals(webUpdate.getId(), all.get(0).getId());
assertEquals(webUpdate.getId(), allNotDeleted.get(0).getId());
assertEquals(webUpdate.getId(), webUpdateInfoMapper.getLastestOne().getId());
}
private WebUpdate generateUpdateInfo() {
WebUpdate webUpdate = new WebUpdate(randomStr(8));
webUpdateInfoMapper.insert(webUpdate);
return webUpdate;
}
}