报错如下,进行添加操作出错
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;//商品类型
}
数据库如下