案例整理(呕心沥血的教训)

其他的我大部分还是不知道那里出了问了,我这个新建的项目must3终于成功了

  1. 那个品牌名称和企业名称没有,是要在BrandMapper里加注解@ResultMap
  2. 一直报那个com.alibaba:fastjson,找不到,最后在Project->Artifacts->xxx:war exploded->lib->library中把三个都加进去servlet才 显示数据

第二天的问题

  1. 这Servlet中的这里面可能不能有中文,有中文就识别不了
response.getWriter().write("sss");//我原本是写的"哈哈哈添加成功了"
  1. 数据库表tb_brand写成了brand
  2. 做删除功能时,前端id传不进去
int id = JSON.parseObject(params, int.class)//这个不行,int改为Integer就可以进来了
  • 上面id解决了,但是从html页面获取的id还是传不进servlet,最终用(热爱编程的小白白)的代码成功了
  • id传进去后,点删除,数据库并没有操作,后来检查发现是没有提交事务

修改功能:

  1. servlet中response没有修改"sss"为"uuu"
  2. html中update_1()方法没有传递index,row参数
  3. BrandServiceImpl里又忘记添加事务回滚,服
  4. 最后一个问题,一直修改不成功,问题肯定初中BrandMapper里面,最后把注解的代码删掉,换成xml的,就成功了.也不知道为啥

批量修改:

  1. BrandServiceImpl里又忘记添加事务回滚,服*2
let selectionElement = this.multipleSelection[i];//这里不能直接在multipleSelection[i].id
 this.selectIds[i] = selectionElement.id;
  1. 最后一个问题:点完确认删除后,页面没有刷新,因为reps不对
alert("resp=" + resp.data)//resp=yeah
 alert("resp=" + resp)//resp=[object Object]
 if(resp.data === "yeah"){//这里我原本写的是resp === "yeah"

我测试发现没有写回滚代码,也能正常提交事务的

分页查询(后端代码)

要再过一遍

问题一:

String b = request.getParameter("b");
 int b2 = Integer.parseInt(b);//就是我再浏览器传参数写的是b2=xxx,但是应该写b=xxx

问题二:品牌名称和企业名称出不了是因为sql语句上面忘记添加@ResultMap("brandResultMap")

问题三:换页了后没有刷新查询,因为没有在方法里加this.selectAll()

//分页,分条
  handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.selectAll();//zh
  },
  handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.selectAll();
  }

条件搜索

问题一:在前端ajax中传完参数重新查询时报错

Uncaught (in promise) TypeError: this.selectAll is not a function

代码:

}).then(function (resp){//这一行代码的问题                    axios.post("http://localhost:8080/must3_war/deleteServlet",row.id).then((resp)=> {
   if (resp.data === "ddd") {
   this.selectAll();

then中使用了function callback,因此在回调函数里面,this是指向HTTP request event,已经不是外部的默认vue对象了。

简而言之就是then里面不能用this,外面可以用

详情:

问题二:selectAll方法一直把数据传不进去,还是Mapper.xml里没有把相关字段映射

<if test="brand.brandName != null and brand.brandName != ''">
 and brand_name like #{brand.brandName}//这里原本都没有加brand.

问题三:

select count(*) from tb_brand//错误:select count (*) from tb_brand

这个sql语句仅仅是因为count后面加了个空格就无法实现,我艹了

总结mybatis中where查询

  • where中test为true则加入sql语句中
  • if、choose、when、otherwise比较
  • if是直接拼接,重点说一下choose语句

choose(单条件查询)

  • 只有一个条件生效,也就是只执行满足的条件when,没有满足的条件就执行otherwise,表示默认条件。
<where>
 <choose>
         <when test="brand.status != null"><!--相当于case-->
              status = #{brand.status}
         </when>
         <when test="brand.companyName != null and brand.companyName != '' "><!--相当于case-->
              company_name like #{brand.companyName}
         </when>
         <when test="brand.brandName != null and brand.brandName != ''"><!--相当于case-->
              brand_name like #{brand.brandName}
         </when>
 </choose>
 </where>
 limit #{begin}, #{size}

在我搜索的时候,用choose就是只要前面有值,就只查询这一个,第二个无论填什么也不报错,也不查询,相当于没有这条语句

其实就可以按照switch-case来理解,单条件查询,可以-用做下拉框单条件查询功能

where+if(多条件查询)

select *
 from tb_brand
 <where>
         <if test="brand.status != null"><!--相当于case-->
             and status = #{brand.status}
         </if>
         <if test="brand.companyName != null and brand.companyName != '' "><!--相当于case-->
             and company_name like #{brand.companyName}
         </if>
         <if test="brand.brandName != null and brand.brandName != ''"><!--相当于case-->
             and brand_name like #{brand.brandName}
         </if>
 </where>
  • 记得加and

模糊查询(在service里面改一下实体)

String brandName = brand.getBrandName();
 if (brandName != null && brandName.length() > 0) {
     brand.setBrandName("%" + brandName + "%");
 }

多条件的话记得判断数据是否为空

修改status显示的值

public String getStatusStr_ghy() {
     if (status == null) {
         return "未知";
     }
     return status == 0 ?  "禁用" :  "启用";
 }

这里getStatusStr_ghy完全没有任何定义,也可以传到前端去,但是一定要注意这个S要小写,得传statusStr_ghy这个字符串

B站弹幕:

1.fastjson 将对象转为JSON字符串,用的应该是get方法,键是方法名去掉get首字母小写

2.虽然实体类中没有定义statusStr变量,但有getStatusStr这个方法,这里应该是通过get的方法获取prop的

3.而prop对应的status属性,在mabtis中查询的时候对应getStatus方法,同样改为statusStr则对应GetStatusStr方法