Springmvc入门基础(三) ---与mybatis框架整合

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&
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值