1.创建数据库springmvc及表items,且插入一些数据
DROP TABLE IF EXISTS `items`;
CREATE TABLE `items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL COMMENT '商品名称',
`price` float(10,1) NOT NULL COMMENT '商品定价',
`detail` text COMMENT '商品描述',
`pic` varchar(64) DEFAULT NULL COMMENT '商品图片',
`createtime` datetime NOT NULL COMMENT '生产日期',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `springmvc`.`items` (`id`, `name`, `price`, `detail`, `pic`, `createtime`) VALUES ('1', '台式机', '3000.0', '该电脑质量非常好!!!!', NULL, '2019-09-21 13:22:53');
INSERT INTO `springmvc`.`items` (`id`, `name`, `price`, `detail`, `pic`, `createtime`) VALUES ('2', '笔记本', '6000.0', '笔记本性能好,质量好!!!!!', NULL, '2019-09-21 13:22:57');
INSERT INTO `springmvc`.`items` (`id`, `name`, `price`, `detail`, `pic`, `createtime`) VALUES ('3', '背包', '200.0', '名牌背包,容量大质量好!!!!', NULL, '2019-09-21 13:23:02');
2.创建java web工程,此处以第一篇博文创建的demo项目为基准--基于idea创建的demo项目
3.导入需要用到的jar包,且加载依赖到项目中去
4.根据逆向工程,生成mapper文件和pojo类文件,或者手写,以及创建service相关类
下面是各个文件内容:
4.1 ItemsMapper.jav
package cn.springmvc.mapper;
import cn.springmvc.pojo.Items;
import cn.springmvc.pojo.ItemsExample;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface ItemsMapper {
int countByExample(ItemsExample example);
int deleteByExample(ItemsExample example);
int deleteByPrimaryKey(Integer id);
int insert(Items record);
int insertSelective(Items record);
List<Items> selectByExampleWithBLOBs(ItemsExample example);
List<Items> selectByExample(ItemsExample example);
Items selectByPrimaryKey(Integer id);
int updateByExampleSelective(@Param("record") Items record, @Param("example") ItemsExample example);
int updateByExampleWithBLOBs(@Param("record") Items record, @Param("example") ItemsExample example);
int updateByExample(@Param("record") Items record, @Param("example") ItemsExample example);
int updateByPrimaryKeySelective(Items record);
int updateByPrimaryKeyWithBLOBs(Items record);
int updateByPrimaryKey(Items record);
}
4.2 ItemsMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.springmvc.mapper.ItemsMapper" >
<resultMap id="BaseResultMap" type="cn.springmvc.pojo.Items" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="price" property="price" jdbcType="REAL" />
<result column="pic" property="pic" jdbcType&