爱打羽球的码猿 2021-05-30 16:57 采纳率: 66.7%
浏览 76
已采纳

关于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;//商品类型
}

数据库如下

  • 写回答

5条回答 默认 最新

  • CSDN专家-林老师 2021-05-30 17:09
    关注
    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上