springboot项目空值

springboot项目,前端页面部分数据为空,在输出台输出显示值为空。问应该如何完全查询数据,代码哪里写错了?

img

img

IDEA 2021.2.3 JDK17
代码部分:
控制器:

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("/to_login")
    public String toLogin(){
        return "admin/login";
    }

    @RequestMapping("/login")
    public String login(Admin admin, HttpSession session, Model model){
        if (!adminService.isValidatedUser(admin)){
            model.addAttribute("login_error","登录失败!");
            return "admin/login";
        }
        model.addAttribute("admin", admin);
        System.out.println(admin);
        session.setAttribute("login_name",admin.getLogin_name());
        return "forward:/to_home_page?page=1&homePage=1";
    }

mapper.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="com.javaee.dzy.mapper.AdminMapper">
    <select id="findAdmins" resultType="Admin">
        select * from admin
    </select>
    <select id="findAdminByUserId" parameterType="String" resultType="Admin">
        select * from admin where user_id=#{user_id};
    </select>
    <select id="findAdminByLoginName" parameterType="String" resultType="Admin">
        select * from admin where login_name=#{login_name};
    </select>
    <insert id="addAdmin" parameterType="Admin">
        insert into admin(user_id,password,login_name,mobile) values(#{user_id},#{password},#{login_name},#{mobile});
    </insert>
    <delete id="deleteAdminByUserId" parameterType="String">
        delete from admin where user_id=#{value};
    </delete>
    <update id="updateAdmin" parameterType="Admin">
        update admin set login_name=#{login_name},mobile=#{mobile} where user_id=#{user_id};
    </update>
    <update id="resetPassword" parameterType="String">
        update admin set password='123456' where user_id=#{value};
    </update>
    <update id="updatePassword" parameterType="Admin">
        update admin set password=#{password} where login_name=#{loginName};
    </update>
</mapper>


service


package com.javaee.dzy.Service.impl;

import com.javaee.dzy.Service.AdminService;
import com.javaee.dzy.mapper.AdminMapper;
import com.javaee.dzy.po.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public boolean isValidatedUser(Admin admin) {
        Admin dbAdmin = adminMapper.findAdminByUserId(admin.getUser_id());
        if (dbAdmin == null)
            return false;
        if (dbAdmin.getPassword().equals(admin.getPassword()))
            return true;
        return false;
    }

    @Override
    public List<Admin> findAdmins(){
        return adminMapper.findAdmins();
    }

    @Override
    public Admin findAdminByUserId(String user_id){
        return adminMapper.findAdminByUserId(user_id);
    }

    @Override
    public Admin findAdminByLoginName(String login_name){
        return adminMapper.findAdminByLoginName(login_name);
    }

    @Override
    public int addAdmin(Admin admin){
        return adminMapper.addAdmin(admin);
    }

    @Override
    public int deleteAdminByUserId(String user_id){
        return adminMapper.deleteAdminByUserId(user_id);
    }

    @Override
    public int updateAdmin(Admin admin){
        return adminMapper.updateAdmin(admin);
    }

    @Override
    public int updatePassword(String originPwd, String newPwd, String login_name) {
        Admin admin = adminMapper.findAdminByLoginName(login_name);
        if (originPwd.compareTo(admin.getPassword()) != 0)
            return 0;
        admin.setPassword(newPwd);
        return adminMapper.updatePassword(admin);
    }

    @Override
    public int resetPassword(String user_id) {
        return adminMapper.resetPassword(user_id);
    }

    @Override
    public boolean registerAdmin(Admin admin){
        Admin dbAdmin = adminMapper.findAdminByLoginName(admin.getLogin_name());
        if (dbAdmin != null)
            return false;
        int res = adminMapper.addAdmin(admin);
        return res == 1;
    }
}

html
```html
 <div class='span12 box bordered-box green-border' style='margin-left:0px;'>
            <div class='box-header sea-blue-background'>
                <div class='title'>管理员信息列表</div>
            </div>
            <div class='box-content box-no-padding'>
                <div class='responsive-table'>
                    <div class='scrollable-area'>
                        <table class='table table-bordered table-hover table-striped' style='margin-bottom:0;'>
                            <thead>
                            <tr><th>登录id</th><th>管理员登录名</th><th>电话</th><th>操作</th></tr>
                            </thead>
                            <tbody>
                            <tr id="${admin.user_id}" th:each="admin:${pageInfo.list}">
                                <td th:text="${admin.user_id}"></td>
                                <td th:text="${admin.login_name}"></td>
                                <td th:text="${admin.mobile}"></td>
                                <td width="150">
                                    <div class='text-center'>
                                        <a class='btn btn-success btn-mini' href='#' th:onclick="editAdmin([[${admin.user_id}]]);">编辑</a>
                                        <a class='btn btn-danger btn-mini' href='#' th:onclick="deleteAdmin([[${admin.user_id}]]);">删除</a>
                                        <a class='btn btn-warning btn-mini' href='#' th:onclick="resetPassword([[${admin.user_id}]]);">重置密码</a>
                                    </div>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <a th:href="@{/to_home_page(homePage=1,page=1)}">首页</a>
            <a th:href="@{/to_home_page(homePage=1,page=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页</a>
            <a th:href="@{/to_home_page(homePage=1,page=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a>
            <a th:href="@{/to_home_page(homePage=1,page=${pageInfo.pages})}">尾页</a>
            当前 <span th:text="${pageInfo.pageNum}"></span> 页,总<span th:text="${pageInfo.pages}"></span> 页,共<span th:text="${pageInfo.total}"></span> 条记录<br/>
            <a class='btn btn-success btn-small' data-toggle='modal' href='#modal-add-admin' role='button' onclick='addAdmin();'>添加</a>
        </div>



TechWhizKid参考GPT回答:

可以考虑在验证用户之后更新你的admin对象,如下所示:

    @RequestMapping("/login")
    public String login(Admin admin, HttpSession session, Model model){
        Admin dbAdmin = adminService.findAdminByUserId(admin.getUser_id());
        if (dbAdmin == null || !dbAdmin.getPassword().equals(admin.getPassword())) {
            model.addAttribute("login_error","登录失败!");
            return "admin/login";
        }
        model.addAttribute("admin", dbAdmin);
        System.out.println(dbAdmin);
        session.setAttribute("login_name", dbAdmin.getLogin_name());
        return "forward:/to_home_page?page=1&homePage=1";
    }


修改后的方法中,首先从数据库获取Admin对象,然后检查密码是否匹配。如果验证成功,那么就将从数据库中检索到的dbAdmin对象返回给前端,而不是原始的、可能未完全填充的admin对象。这样,你应该可以在前端看到所有的管理员数据。

Admin中实体类的属性名定义错了,mybatis 默认开启了驼峰命名规则。

user_id 改为 userId
login_name 改为 loginName

看这个样子,你应该是开启了自动下划线转驼峰式命名了,看下你配置里是不是有这个配置mybatis.configuration.map-underscore-to-camel-case=true
你实体类字段都改成驼峰式的试试,user_id改成 userId,login_name改成loginName,注意页面里的也要一起修改。

能互相远程看一下吗兄弟,我这里有个问题也解决不了。。。

查看实体类Admin的各个属性和你的数据库admin表的各个字段保持一下一致

增删改查,别再自己写了

  • 这篇博客也许可以解决你的问题👉 :SpringBoot在普通类中调用service,mapper等
  • 除此之外, 这篇博客: Springboot简单授权,限制访问中的 mapper.xml 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • <!--查询url-->
    <select id="selectUrl" resultType="com.thk.domain.Power">
            SELECT p.url FROM rolemiddle as  r
            LEFT JOIN power as p  ON r.powerid=p.id
            WHERE r.roleid=#{id}
    </select>
    
    
    <!--查询全部用户-->
    <select id="selectLists" resultType="com.thk.domain.People">
        <include refid="selectPeopleVo"/>
        <where>
            <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>
            <if test="age != null ">and age = #{age}</if>
            <if test="sex != null ">and sex = #{sex}</if>
            <if test="address != null  and address != ''">and address = #{address}</if>
            <if test="userName != null  and userName != ''">and user_name like concat('%', #{userName}, '%')</if>
            <if test="pwd != null  and pwd != ''">and pwd = #{pwd}</if>
            <if test="email != null  and email != ''">and email = #{email}</if>
            <if test="phonenumber != null  and phonenumber != ''">and phonenumber = #{phonenumber}</if>
            <if test="loginIp != null  and loginIp != ''">and login_ip = #{loginIp}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
            <if test="loginDate != null ">and login_date = #{loginDate}</if>
        </where>
    </select>