关于Mybatis的@Param注解

 报错如下,进行添加操作出错

Dao层如下:

package com.soft.dao;

import com.soft.entity.Goods;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface GoodsMapper {

    @Select("select * from goods")
    List<Goods> getAllGoods();

    @Insert("insert into goods (goods_name,goods_price,goods_introduce,goods_date,goods_img,goods_type,)" +
            "values (#{goodsName},#{goodsPrice},#{goodsIntroduce},#{goodsDate},#{goodsImg},#{goodsType})")
    int addGoods(@Param("goods") Goods goods);

    @Update("update goods " +
            "set goods_name = #{goodsName},goods_price= #{goodsPrice},goods_introduce = #{goodsIntroduce}," +
            "goods_date= #{goodsDate},goods_img =#{goodsImg},goods_type = #{goodsType}," +
            "where goods_id = #{goodsId}")
    int updateGoods(@Param("goods") Goods goods);

}

Service 如下:

package com.soft.service;

import com.soft.entity.Goods;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public interface GoodsService {

    List<Goods> getAllGoods();

    int addGoods(Goods goods);

    int updateGoods(Goods goods);
}

ServiceImpl 如下:

package com.soft.service.Impl;

import com.soft.dao.GoodsMapper;
import com.soft.entity.Goods;
import com.soft.service.GoodsService;

import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    private GoodsMapper goodsMapper;

    public void setGoodsMapper(GoodsMapper goodsMapper) {
        this.goodsMapper = goodsMapper;
    }

    @Override
    public List<Goods> getAllGoods() {
        return goodsMapper.getAllGoods();
    }

    @Override
    public int addGoods(Goods goods) {
        return goodsMapper.addGoods(goods);
    }

    @Override
    public int updateGoods(Goods goods) {
        return goodsMapper.updateGoods(goods);
    }
}

测试代码如下:

import com.soft.entity.Goods;
import com.soft.service.GoodsService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class GoodsTest {
    @Test
    public void getAllGoods(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        GoodsService goodsServiceImpl = (GoodsService) context.getBean("GoodsServiceImpl");
        List<Goods> allGoods = goodsServiceImpl.getAllGoods();
        System.out.println(allGoods);
    }


   @Test
    public void addGoods(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        GoodsService goodsServiceImpl = (GoodsService) context.getBean("GoodsServiceImpl");
        Goods goods = new Goods(7,"运动裤",1000.23,"宽松透气",getCurrentDate(),"/img/01.png",1);

        int i = goodsServiceImpl.addGoods(goods);
        if(i > 1){
            System.out.println("添加成功=>"+goods);
        }
    }

    public Date getCurrentDate() {
        //产生一个不带毫秒的时间,不然,SQL时间和JAVA时间格式不一致
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date tm = new Date();
        try {
            tm= sdf.parse(sdf.format(new Date()));
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        return tm;
    }
}

实体类如下

package com.soft.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Goods {
    private int goodsId;//商品id
    private String goodsName;//商品名称
    private double goodsPrice;//商品价格
    private String goodsIntroduce;//商品介绍
    private Date goodsDate;//上架时间
    private String goodsImg;//商品图片
    private int goodsType;//商品类型
}

数据库如下

package com.soft.dao;
import com.soft.entity.Goods;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface GoodsMapper {
    @Select("select * from goods")
    List<Goods> getAllGoods();
    @Insert("insert into goods (goods_name,goods_price,goods_introduce,goods_date,goods_img,goods_type,)" +
            "values (#{goods.goodsName},#{goods.goodsPrice},#{goods.goodsIntroduce},#{goods.goodsDate},#{goods.goodsImg},#{goods.goodsType})")
    int addGoods(@Param("goods") Goods goods);
    @Update("update goods " +
            "set goods_name = #{goods.goodsName},goods_price= #{goods.goodsPrice},goods_introduce = #{goods.goodsIntroduce}," +
            "goods_date= #{goods.goodsDate},goods_img =#{goods.goodsImg},goods_type = #{goods.goodsType}," +
            "where goods_id = #{goods.goodsId}")
    int updateGoods(@Param("goods") Goods goods);
}

 

你的注解配置的不对。。把@Params去掉试一试。

@Insert("insert into goods (goods_name,goods_price,goods_introduce,goods_date,goods_img,goods_type,)" +

"values (#{goodsName},#{goodsPrice},#{goodsIntroduce},#{goodsDate},#{goodsImg},#{goodsType})")

int addGoods(@Param("goods") Goods goods);

这样的语法是告诉 框架 我要传递一个 goods参数进来。显然的#{goodsName}是在goods下的。所以如果硬要写@Params

应该写成#{goods.goodsName}

int addGoods(@Param("goods") Goods goods); 你注入这个Goods对象的时候,给这个对象取了名字叫goods,那你的sql语句就应该改成:"values (#{goods.goodsName},#{goods.goodsPrice},#{goods.goodsIntroduce},#{goods.goodsDate},#{goods.goodsImg},#{goods.goodsType})")这样。

去掉@Param("goods")注解后,显示这个错误

您的问题已经有小伙伴解答了,请点击【采纳】按钮,采纳帮您提供解决思路的答案,给回答的人一些鼓励哦~~

ps:开通问答VIP,享受5次/月 有问必答服务,了解详情↓↓↓

【电脑端】戳>>>  https://vip.csdn.net/askvip?utm_source=1146287632
【APP 】  戳>>>  https://mall.csdn.net/item/52471?utm_source=1146287632