调整数据库字段,优化部分接口 #1
@@ -1,20 +1,14 @@
|
||||
package cn.celess.blog.entity;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/03/28 22:18
|
||||
*/
|
||||
@Data
|
||||
public class Category {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String articles;
|
||||
|
||||
}
|
||||
package cn.celess.blog.entity;
|
||||
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/03/28 22:18
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
public class Category extends TagCategory {
|
||||
public Category(String name) {
|
||||
super.setName(name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
package cn.celess.blog.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/03/28 22:19
|
||||
*/
|
||||
@Data
|
||||
public class Tag {
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String articles;
|
||||
}
|
||||
package cn.celess.blog.entity;
|
||||
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/03/28 22:19
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
public class Tag extends TagCategory {
|
||||
|
||||
public Tag(String name) {
|
||||
super.setName(name);
|
||||
}
|
||||
}
|
||||
|
||||
19
src/main/java/cn/celess/blog/entity/TagCategory.java
Normal file
19
src/main/java/cn/celess/blog/entity/TagCategory.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package cn.celess.blog.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Author: 小海
|
||||
* @Date: 2020-05-24 14:03
|
||||
* @Desc:
|
||||
*/
|
||||
@Data
|
||||
public class TagCategory {
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
private boolean category = true;
|
||||
|
||||
private boolean deleted = false;
|
||||
}
|
||||
@@ -34,7 +34,7 @@ public interface CategoryMapper {
|
||||
|
||||
String getNameById(long id);
|
||||
|
||||
Long getIDByName(String name);
|
||||
Long getIdByName(String name);
|
||||
|
||||
Category getLastestCategory();
|
||||
|
||||
|
||||
@@ -26,10 +26,6 @@ public interface TagMapper {
|
||||
|
||||
Boolean existsByName(String name);
|
||||
|
||||
Long getIDByName(String name);
|
||||
|
||||
String getNameById(long id);
|
||||
|
||||
Tag getLastestTag();
|
||||
|
||||
List<Tag> findAll();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?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="cn.celess.blog.mapper.CategoryMapper">
|
||||
<resultMap id="categoryResultMap" type="cn.celess.blog.entity.TagCategory">
|
||||
<resultMap id="categoryResultMap" type="cn.celess.blog.entity.Category">
|
||||
<id column="t_id" property="id"/>
|
||||
<result column="t_name" property="name"/>
|
||||
<result column="is_category" property="category"/>
|
||||
@@ -56,7 +56,8 @@
|
||||
<select id="getNameById" resultType="java.lang.String">
|
||||
select t_name
|
||||
from tag_category
|
||||
where is_category = true;
|
||||
where is_category = true
|
||||
and t_id = #{id};
|
||||
</select>
|
||||
|
||||
<select id="getIdByName" resultType="java.lang.Long">
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
<resultMap id="tagResultMap" type="cn.celess.blog.entity.Tag"
|
||||
extends="cn.celess.blog.mapper.CategoryMapper.categoryResultMap">
|
||||
|
||||
</resultMap>
|
||||
|
||||
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
|
||||
|
||||
119
src/test/java/cn/celess/blog/mapper/CategoryMapperTest.java
Normal file
119
src/test/java/cn/celess/blog/mapper/CategoryMapperTest.java
Normal file
@@ -0,0 +1,119 @@
|
||||
package cn.celess.blog.mapper;
|
||||
|
||||
import cn.celess.blog.BaseTest;
|
||||
import cn.celess.blog.entity.Category;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class CategoryMapperTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
CategoryMapper categoryMapper;
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
Category category = generateCategory();
|
||||
assertNotNull(category.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete() {
|
||||
Category category = generateCategory();
|
||||
assertFalse(category.isDeleted());
|
||||
int lines = categoryMapper.delete(category.getId());
|
||||
assertNotEquals(0, lines);
|
||||
Category categoryById = categoryMapper.findCategoryById(category.getId());
|
||||
assertTrue(categoryById.isDeleted());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() {
|
||||
Category category = generateCategory();
|
||||
category.setName(randomStr(4));
|
||||
int update = categoryMapper.update(category);
|
||||
assertEquals(1, update);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existsByName() {
|
||||
Category category = generateCategory();
|
||||
assertTrue(categoryMapper.existsByName(category.getName()));
|
||||
assertFalse(categoryMapper.existsByName(randomStr(8)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existsById() {
|
||||
Category category = generateCategory();
|
||||
assertTrue(categoryMapper.existsById(category.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findCategoryByName() {
|
||||
Category suibi = categoryMapper.findCategoryByName("随笔");
|
||||
assertNotNull(suibi);
|
||||
|
||||
// tag 数据
|
||||
Category shiro = categoryMapper.findCategoryByName("shiro");
|
||||
assertNull(shiro);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findCategoryById() {
|
||||
Category suibi = categoryMapper.findCategoryByName("随笔");
|
||||
|
||||
Category categoryById = categoryMapper.findCategoryById(suibi.getId());
|
||||
|
||||
assertEquals(suibi.getName(), categoryById.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAll() {
|
||||
List<Category> all = categoryMapper.findAll();
|
||||
assertNotEquals(0, all);
|
||||
all.forEach(category -> assertTrue(category.isCategory()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllName() {
|
||||
List<String> allName = categoryMapper.getAllName();
|
||||
assertNotEquals(0, allName.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNameById() {
|
||||
Category category = generateCategory();
|
||||
assertEquals(category.getName(), categoryMapper.getNameById(category.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getIdByName() {
|
||||
Category category = generateCategory();
|
||||
assertEquals(category.getId(), categoryMapper.getIdByName(category.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLastestCategory() {
|
||||
List<Category> all = categoryMapper.findAll();
|
||||
all.sort((o1, o2) -> (int) (o2.getId() - o1.getId()));
|
||||
assertEquals(all.get(0).getId(), categoryMapper.getLastestCategory().getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void count() {
|
||||
List<Category> all = categoryMapper.findAll();
|
||||
assertEquals(all.size(), categoryMapper.count());
|
||||
}
|
||||
|
||||
private Category generateCategory() {
|
||||
Category category = new Category(randomStr(4));
|
||||
|
||||
categoryMapper.insert(category);
|
||||
category.setDeleted(false);
|
||||
return category;
|
||||
}
|
||||
}
|
||||
91
src/test/java/cn/celess/blog/mapper/TagMapperTest.java
Normal file
91
src/test/java/cn/celess/blog/mapper/TagMapperTest.java
Normal file
@@ -0,0 +1,91 @@
|
||||
package cn.celess.blog.mapper;
|
||||
|
||||
import cn.celess.blog.BaseTest;
|
||||
import cn.celess.blog.entity.Tag;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class TagMapperTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
TagMapper tagMapper;
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
Tag tag = generateTag();
|
||||
assertNotNull(tag.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() {
|
||||
Tag tag = generateTag();
|
||||
tag.setName(randomStr(4));
|
||||
int update = tagMapper.update(tag);
|
||||
assertEquals(1, update);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete() {
|
||||
Tag tag = generateTag();
|
||||
assertFalse(tag.isDeleted());
|
||||
assertEquals(1, tagMapper.delete(tag.getId()));
|
||||
Tag tagById = tagMapper.findTagById(tag.getId());
|
||||
assertTrue(tagById.isDeleted());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findTagById() {
|
||||
Tag tag = generateTag();
|
||||
Tag tagById = tagMapper.findTagById(tag.getId());
|
||||
assertEquals(tag.getName(), tagById.getName());
|
||||
assertEquals(tag.getId(), tagById.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findTagByName() {
|
||||
Tag tag = generateTag();
|
||||
Tag tagById = tagMapper.findTagByName(tag.getName());
|
||||
assertEquals(tag.getName(), tagById.getName());
|
||||
assertEquals(tag.getId(), tagById.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existsByName() {
|
||||
String s = randomStr(4);
|
||||
assertFalse(tagMapper.existsByName(s));
|
||||
Tag tag = new Tag(s);
|
||||
tagMapper.insert(tag);
|
||||
assertTrue(tagMapper.existsByName(s));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLastestTag() {
|
||||
List<Tag> all = tagMapper.findAll();
|
||||
all.sort(((o1, o2) -> (int) (o2.getId() - o1.getId())));
|
||||
assertEquals(all.get(0).getId(), tagMapper.getLastestTag().getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAll() {
|
||||
List<Tag> all = tagMapper.findAll();
|
||||
assertNotEquals(0, all.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void count() {
|
||||
assertNotEquals(0, tagMapper.count());
|
||||
List<Tag> all = tagMapper.findAll();
|
||||
assertEquals(all.size(), tagMapper.count());
|
||||
}
|
||||
|
||||
private Tag generateTag() {
|
||||
Tag tag = new Tag(randomStr(4));
|
||||
tagMapper.insert(tag);
|
||||
tag.setDeleted(false);
|
||||
return tag;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user