编程知识 cdmana.com

为什么调用了BrandMapper里面的addBrand方法,这个方法已经给我一个返回值了,也在方法里面提交事务了,但是数据库还是没有增加数据

问题遇到的现象和发生背景

问题遇到的现象:为什么调用了BrandMapper里面的addBrand方法,这个方法已经给我一个返回值了,也在方法里面提交事务了,但是数据库还是没有增加数据

发生背景:itheima javaweb课程最后面利用Element、vue、axios的综合案例

问题相关代码(PS:为了突出重点只给出关键部分)

<script src="js/vue.js"></script><script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
<!-- 为添加按钮绑定一个对话框 -->    <el-dialog title="编辑品牌" :visible.sync="dialogVisible" width="30%" :before-close="handleClose"><!-- 对话框里面的表单-->        <el-form ref="form" :model="brand" label-width="80px">            <el-form-item label="品牌名称">                <el-input v-model="brand.brandName"></el-input>            </el-form-item>            <el-form-item label="企业名称">                <el-input v-model="brand.companyName"></el-input>            </el-form-item>            <el-form-item label="订单量">                <el-input v-model="brand.ordered"></el-input>            </el-form-item>            <el-form-item label="备注" prop="description">                <el-input type="textarea" v-model="brand.description"></el-input>            </el-form-item>            <el-form-item label="状态" prop="status">                <el-switch active-value="1" inactive-value="0" v-model="brand.status"></el-switch>            </el-form-item>            <el-form-item>`                <el-button type="primary" @click="addBrandOnSubmit">提交</el-button>                <el-button @click="dialogVisible=false">取消</el-button>            </el-form-item>        </el-form>    </el-dialog>
<script> new Vue({ el:"#app", methods:{ addBrandOnSubmit() { var _this=this; axios({ method:"post", url:"http://localhost:8080/ElementDemo/addBrandServlet", data:_this.brand }).then(function (resq){ }) }
package com.itheima.service;import com.itheima.POJO.Brand;import com.itheima.mapper.BrandMapper;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 sqlSessionFactory = sqlSessionFactoryUtils.getSqlSessionFactory();       public int addBrand(Brand brand){        SqlSession sqlSession = sqlSessionFactory.openSession();        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);        int i = mapper.addBrand(brand);        sqlSession.commit();        sqlSession.close();        return i;    }
package com.itheima.web.servlet;import com.alibaba.fastjson.JSON;import com.itheima.POJO.Brand;import com.itheima.service.brandService;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.BufferedReader;import java.io.IOException;import java.nio.charset.StandardCharsets;@WebServlet(name = "addBrandServlet", value = "/addBrandServlet")public class addBrandServlet extends HttpServlet {    private brandService brandService = new brandService();    @Override    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        System.out.println("doget...");        BufferedReader reader = request.getReader();        String s = reader.readLine();        Brand brand = JSON.parseObject(s, Brand.class);        int i = brandService.addBrand(brand);        System.out.println("以增加"+i+"条记录");        String jsonString = JSON.toJSONString(i);        response.setContentType("text/json;charset=utf-8");        response.getWriter().write(jsonString);    }    @Override    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        System.out.println("dopost...");        this.doGet(request, response);    }}
package com.itheima.mapper;import com.itheima.POJO.Brand;import org.apache.ibatis.annotations.Delete;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Select;import java.util.List;import java.util.Map;public interface BrandMapper {    @Insert("insert into t_brand values (null,#{brandName},#{companyName},#{ordered},#{description},#{status});")    int addBrand(Brand brand); 

运行结果:

img


可以看出来addBrand方法已经给出返回值了

img


但是数据库里面的数据并没有增加

img


在addBrand方法里也提交了事务

img


我填写的信息

我想要达到的结果:表单提交之后数据库的数据会增加这个新品牌的信息




采纳答案:

img

img


一个是t开头,一个是tb开头?


版权声明
本文为[CSDN问答]所创,转载请带上原文链接,感谢
https://ask.csdn.net/questions/7711879

Scroll to Top