发表于: 2025-06-12 20:25:25
0 30
今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin)
UserServiceTest
import org.example.model.User;
import org.example.service.UserService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserServiceTest {
private UserService userService;
@Before
public void init() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
userService = ctx.getBean(UserService.class);
}
@Test
public void testAddNodesWithAllFields() {
// 1. 添加根节点 (level=0)
User root = new User();
root.setPid(0L); // 父节点ID,根节点为0
root.setLevel(0L); // 层级:0-一级节点
root.setName("根节点"); // 节点名称
root.setState(String.valueOf(1)); // 状态:1-启用
root.setCreateAt(System.currentTimeMillis()); // 创建时间
root.setUpdateAt(System.currentTimeMillis()); // 更新时间
root.setAuthor("admin"); // 创建人
root.setSort(1L); // 排序号
Long rootId = userService.create(root);
System.out.println("添加根节点成功,ID=" + rootId);
// 2. 添加二级节点 (level=1)
User second = new User();
second.setPid(rootId); // 父节点为刚创建的根节点
second.setLevel(1L); // 层级:1-二级节点
second.setName("二级节点"); // 节点名称
second.setState(String.valueOf(1)); // 状态:1-启用
second.setCreateAt(System.currentTimeMillis());
second.setUpdateAt(System.currentTimeMillis());
second.setAuthor("manager"); // 创建人
second.setSort(1L); // 排序号
Long secondId = userService.create(second);
System.out.println("添加二级节点成功,ID=" + secondId);
// 3. 添加三级节点 (level=2)
User third = new User();
third.setPid(secondId); // 父节点为刚创建的二级节点
third.setLevel(2L); // 层级:2-三级节点
third.setName("三级节点"); // 节点名称
third.setState(String.valueOf(1)); // 状态:1-启用
third.setCreateAt(System.currentTimeMillis());
third.setUpdateAt(System.currentTimeMillis());
third.setAuthor("user"); // 创建人
third.setSort(1L); // 排序号
// 三级节点特有字段
third.setIntroduce("这是三级节点的详细描述信息"); // 描述
third.setImage("/images/product001.jpg"); // 图片路径
third.setUrl("http://example.com/product001"); // 链接
third.setPicture("/images/detail001.jpg"); // 详情图片
Long thirdId = userService.create(third);
System.out.println("添加三级节点成功,ID=" + thirdId);
// 验证特有字段只在三级节点存在
User rootNode = userService.getById(rootId);
User secondNode = userService.getById(secondId);
User thirdNode = userService.getById(thirdId);
System.out.println("\n验证结果:");
System.out.println("根节点特有字段 - introduce: " + rootNode.getIntroduce()); // 应为null
System.out.println("二级节点特有字段 - image: " + secondNode.getImage()); // 应为null
System.out.println("三级节点特有字段 - url: " + thirdNode.getUrl()); // 应有值
}
@Test
public void testQueryNode() {
Long nodeId = 1L; // 测试查询ID=1的节点
User node = userService.getById(nodeId);
if (node == null) {
System.out.println("没有找到该节点.");
return;
}
System.out.println("此ID的节点信息为: " + node);
// 如果是三级节点,额外显示特有字段
if (node.getLevel() == 2) {
System.out.println("特有字段 -> 描述: " + node.getIntroduce()
+ ", 图片: " + node.getImage());
}
}
@Test
public void testDeleteNode() {
Long nodeId = 3L; // 测试删除ID=3的节点
if (userService.deleteById(nodeId)) {
System.out.println("删除节点成功");
} else {
System.out.println("删除失败(节点不存在或有子节点)");
}
}
@Test
public void testUpdateNode() {
User node = new User();
node.setId(1L); // 要修改的节点ID
node.setName("新名称");
node.setUpdateAt(System.currentTimeMillis());
if (userService.update(node)) {
System.out.println("更新成功");
} else {
System.out.println("更新失败");
}
}
}
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="org.example.mapper.UserMapper">
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
INSERT INTO works
(pid, level, name, state, create_at, update_at, author, sort
<if test="level == 2">, introduce, image, url, picture</if>)
VALUES
(#{pid}, #{level}, #{name}, #{state}, #{createAt}, #{updateAt}, #{author}, #{sort}
<if test="level == 2">, #{introduce}, #{image}, #{url}, #{picture}</if>)
</insert>
<update id="update" parameterType="User">
UPDATE works SET
pid = #{pid},
level = #{level},
name = #{name},
state = #{state},
update_at = #{updateAt},
author = #{author},
sort = #{sort}
<if test="level == 2">,
introduce = #{introduce},
image = #{image},
url = #{url},
picture = #{picture}
</if>
WHERE id = #{id}
</update>
<select id="selectAll" resultType="User">
SELECT * FROM works ORDER BY pid, sort
</select>
<select id="selectById" resultType="User">
SELECT * FROM works WHERE id = #{id}
</select>
<select id="selectByPid" resultType="User">
SELECT * FROM works WHERE pid = #{pid} ORDER BY sort
</select>
<select id="selectByLevel" resultType="User">
SELECT
id, pid, level, name, state,
create_at, update_at, author, sort,
<if test="level == 2">
introduce, image, url, picture
</if>
<if test="level != 2">
null as introduce, null as image, null as url, null as picture
</if>
FROM works
WHERE level = #{level}
ORDER BY sort
</select>
<delete id="deleteById">
DELETE FROM works WHERE id = #{id}
</delete>
</mapper>
明天计划的事情:(一定要写非常细致的内容)
遇到的问题:(遇到什么困难,怎么解决的)
收获:(通过今天的学习,学到了什么知识)
评论