从数据库查询的数据,为什么无法正常的显示到页面?

从数据库查询的数据,为什么无法正常的显示到页面?

// 实体类
   package com.itheima.pojo;

/**
 * 品牌实体类
 */

public class Brand {
    // id 主键
    private Integer id;
    // 品牌名称
    private String brandName;
    // 企业名称
    private String companyName;
    // 排序字段
    private Integer ordered;
    // 描述信息
    private String description;
    // 状态:0:禁用  1:启用
    private Integer status;


    public Brand() {
    }

    public Brand(Integer id, String brandName, String companyName, String description) {
        this.id = id;
        this.brandName = brandName;
        this.companyName = companyName;
        this.description = description;
    }

    public Brand(Integer id, String brandName, String companyName, Integer ordered, String description, Integer status) {
        this.id = id;
        this.brandName = brandName;
        this.companyName = companyName;
        this.ordered = ordered;
        this.description = description;
        this.status = status;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getBrandName() {
        return brandName;
    }

    public void setBrandName(String brandName) {
        this.brandName = brandName;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public Integer getOrdered() {
        return ordered;
    }

    public void setOrdered(Integer ordered) {
        this.ordered = ordered;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getStatus() {
        return status;
    }
    //逻辑视图
    public String getStatusStr() {

        if(this.status == 1){
            return "启用";
        }

        return "禁用";
    }
    public void setStatus(Integer status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Brand{" +
                "id=" + id +
                ", brandName='" + brandName + '\'' +
                ", companyName='" + companyName + '\'' +
                ", ordered=" + ordered +
                ", description='" + description + '\'' +
                ", status=" + status +
                '}';
    }
}


package com.itheima.mapper;

import com.itheima.pojo.Brand;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;


package com.itheima.web;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/selectAllServlet")
public class SelectAllServlet extends HttpServlet {
    private BrandService brandService = new BrandService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 调用Service查询
        List<Brand> brands = brandService.selectAll();

        //2. 将集合转换为JSON数据   序列化
        String jsonString = JSON.toJSONString(brands);

        //3. 响应数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}



// 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a href="addBrand.html"><input type="button" value="新增"></a><br>
<hr>
<div id="app">
    <table id="brandTable" border="1" cellspacing="0" width="100%">
        <tr>
            <th>序号</th>
            <th>品牌名称</th>
            <th>企业名称</th>
            <th>排序</th>
            <th>品牌介绍</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
        <tr v-for="(brand,i) in brands" align="center">
            <td>{{i + 1}}</td>
            <td>{{brand.brandName}}</td>
            <td>{{brand.companyName}}</td>
            <td>{{brand.ordered}}</td>
            <td>{{brand.description}}</td>
            <td>{{brand.statusStr}}</td>
            <td><a href="#">修改</a> <a href="#">删除</a></td>
        </tr>

    </table>
</div>

<script src="js/axios-0.18.0.js"></script>
<script src="js/vue.js"></script>

<script>

    new Vue({
        el: "#app",
        data(){
            return{
                brands:[]
            }
        },
        mounted(){
            // 页面加载完成后,发送异步请求,查询数据
            var _this = this;
            axios({
                method:"get",
                url:"http://localhost:8080/selectAllServlet"
            }).then(function (resp) {
                _this.brands = resp.data;
            })
        }
    })

</script>
</body>
</html>

https://img1.sycdn.imooc.com//climg/6269035409f908aa38120606.jpg

https://img1.sycdn.imooc.com//climg/6269037909ec412821300390.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小尤 2022-04-27 18:30:12

同学你好,1、出现500异常是内部服务器错误,建议同学查看编译器控制台,查看是否存在异常信息,如果存在则反馈。

2、同学反馈的代码不全,老师无法进行测试,建议同学将mapper文件一并反馈。

祝学习愉快!

  • 提问者 qq_慕盖茨6087126 #1
    // mapper
    package com.itheima.mapper;
    
    import com.itheima.pojo.Brand;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.ResultMap;
    import org.apache.ibatis.annotations.Select;
    import org.apache.ibatis.annotations.Update;
    
    import java.util.List;
    
    public interface BrandMapper {
    
        /**
         * 查询所有
         * @return
         */
        @Select("select * from tb_brand")
        @ResultMap("brandResultMap")
        List<Brand> selectAll();
    
        @Insert("insert into tb_brand values(null,#{brandName},#{companyName},#{ordered},#{description},#{status})")
        void add(Brand brand);
    
        /**
         * 根据id查询
         * @param id
         * @return
         */
        @Select("select * from tb_brand where id = #{id}")
        @ResultMap("brandResultMap")
        Brand selectById(int id);
    
        /**
         * 修改
         * @param brand
         */
        @Update("update tb_brand set brand_name = #{brandName},company_name = #{companyName},ordered = #{ordered},description = #{description},status = #{status} where id = #{id}")
        void update(Brand brand);
    
    }
    
    // Sevice
    package com.itheima.service;
    
    import com.itheima.mapper.BrandMapper;
    import com.itheima.pojo.Brand;
    import com.itheima.util.SqlSessionFactoryUtils;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    
    import java.util.List;
    
    public class BrandService {
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
    
    
        /**
         * 查询所有
         * @return
         */
        public List<Brand> selectAll(){
            //调用BrandMapper.selectAll()
    
            //2. 获取SqlSession
            SqlSession sqlSession = factory.openSession();
            //3. 获取BrandMapper
            BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    
            //4. 调用方法
            List<Brand> brands = mapper.selectAll();
    
            sqlSession.close();
    
            return brands;
        }
    
        /**
         * 添加
         * @param brand
         */
        public void add(Brand brand){
    
            //2. 获取SqlSession
            SqlSession sqlSession = factory.openSession();
            //3. 获取BrandMapper
            BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    
            //4. 调用方法
            mapper.add(brand);
    
            //提交事务
            sqlSession.commit();
            //释放资源
            sqlSession.close();
    
        }
    
    
    
        /**
         * 根据id查询
         * @return
         */
        public Brand selectById(int id){
            //调用BrandMapper.selectAll()
    
            //2. 获取SqlSession
            SqlSession sqlSession = factory.openSession();
            //3. 获取BrandMapper
            BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    
            //4. 调用方法
            Brand brand = mapper.selectById(id);
    
            sqlSession.close();
    
            return brand;
        }
    
    
        /**
         * 修改
         * @param brand
         */
        public void update(Brand brand){
    
            //2. 获取SqlSession
            SqlSession sqlSession = factory.openSession();
            //3. 获取BrandMapper
            BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    
            //4. 调用方法
            mapper.update(brand);
    
            //提交事务
            sqlSession.commit();
            //释放资源
            sqlSession.close();
    
        }
    }


    2022-04-27 18:41:07
  • 提问者 qq_慕盖茨6087126 #2
    // BrandMapper.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.itheima.mapper.BrandMapper">
    
    
    
        <resultMap id="brandResultMap" type="brand">
            <result column="brand_name" property="brandName"></result>
            <result column="company_name" property="companyName"></result>
        </resultMap>
    </mapper>
    
    
    // Mybatis-config.xml
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <!--起别名-->
        <typeAliases>
            <package name="com.itheima.pojo"/>
        </typeAliases>
    
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql:///db1?useSSL=false&amp;useServerPrepStmts=true"/>
                    <property name="username" value="root"/>
                    <property name="password" value="12345678"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <!--扫描mapper-->
            <package name="com.itheima.mapper"/>
        </mappers>
    </configuration>


    2022-04-27 18:43:34
  • 同学你好,无法测试同学代码,同学提供的代码缺少SqlSessionFactoryUtils类,导致无法进行测试。并建议同学查看控制台中是否存在异常,如果存在建议一并反馈

    祝学习愉快!

    2022-04-28 10:35:53
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师