介绍
执行环境:2020idea,jdk-14.0.1
基于spring整合的mybatis,通过注解的方式完成数据库的映射
步骤
创建一个dao项目
创建一个spring项目,项目的名称为dao。
导入pom.xml依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
写好application.yml配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/bugaosuni?useSSL=false
username: bugaosuni
password: bugaosuni
driver-class-name: com.mysql.jdbc.Driver
mybatis:
configuration:
map-underscore-to-camel-case: true
创建映射的数据库对象
我创建的是Goods
package cn.hsl.dao.pojo;
//import com.baomidou.mybatisplus.annotation.TableId;
//import com.baomidou.mybatisplus.annotation.TableName;
//import javax.persistence.Id;
import java.util.Date;
//为了令canal可以自动帮助我们获取对象所引用的
//@TableName("goods")
public class Goods {
//mysql商品基本信息
private int goodsPrice;
// @TableId
// @Id
private int goodsId;
private String goodsName;
private float goodsDiscount;
private String goodsIdentifier;
private String goodsImgUri;
private Date goodsExpire;
private Date goodsProduceDate;
private String goodsProducer;
//差个goods原料
private int goodsNum;
@Override
public String toString() {
return "Goods{" +
"goodsPrice="+goodsPrice+
",goodsId=" + goodsId +
", goodsName='" + goodsName + '\'' +
", goodsDiscount=" + goodsDiscount +
", goodsIdentifier='" + goodsIdentifier + '\'' +
", goodsImgUri='" + goodsImgUri + '\'' +
", goodsExpire=" + goodsExpire +
", goodsProduceDate=" + goodsProduceDate +
", goodsFactory='" + goodsProducer + '\'' +
", goodsNum=" + goodsNum +
'}';
}
public int getGoodsPrice() {
return goodsPrice;
}
public void setGoodsPrice(int goodsPrice) {
this.goodsPrice = goodsPrice;
}
public String getGoodsProducer() {
return goodsProducer;
}
public void setGoodsProducer(String goodsProducer) {
this.goodsProducer = goodsProducer;
}
public int getGoodsId() {
return goodsId;
}
public void setGoodsId(int goodsId) {
this.goodsId = goodsId;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public float getGoodsDiscount() {
return goodsDiscount;
}
public void setGoodsDiscount(float goodsDiscount) {
this.goodsDiscount = goodsDiscount;
}
public String getGoodsIdentifier() {
return goodsIdentifier;
}
public void setGoodsIdentifier(String goodsIdentifier) {
this.goodsIdentifier = goodsIdentifier;
}
public String getGoodsImgUri() {
return goodsImgUri;
}
public void setGoodsImgUri(String goodsImgUri) {
this.goodsImgUri = goodsImgUri;
}
public Date getGoodsExpire() {
return goodsExpire;
}
public void setGoodsExpire(Date goodsExpire) {
this.goodsExpire = goodsExpire;
}
public Date getGoodsProduceDate() {
return goodsProduceDate;
}
public void setGoodsProduceDate(Date goodsProduceDate) {
this.goodsProduceDate = goodsProduceDate;
}
public int getGoodsNum() {
return goodsNum;
}
public void setGoodsNum(int goodsNum) {
this.goodsNum = goodsNum;
}
//如果商品在短时间内被读取很多次,则将商品移存至redis,
}
创建mapper映射
其实这里使用mybatis-plus会更方便点,可以使GoodsMapper继承BaseMapper<[数据库对象]>,然后就会有基本的通过id搜索啥的,不过貌似对数据库的命名有所规范。
package cn.hsl.dao.mapper;
import cn.hsl.dao.pojo.Goods;
//import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import java.util.List;
@Mapper
@Component
public interface GoodsMapper{
//增加商品
@Insert("insert into goods(goods_name,goods_discount,goods_identifier,goods_imgUri,goods_expire,goods_produceDate,goods_producer," +
"goods_num,goods_price) values(#{goodsName},#{goodsDiscount},#{goodsIdentifier},#{goodsImgUri},#{goodsExpire}," +
"#{goodsProduceDate},#{goodsProducer},#{goodsNum},#{goodsPrice})")
public boolean insertGoods(Goods goods);
//删除商品
@Delete("delete from goods where goods_id = #{goodsId}")
public boolean deleteGoodsById(int goodsId);
//修改商品
@Update("update goods set goods_name=#{goodsName},goods_discount=#{goodsDiscount},goods_identifier=#{goodsIdentifier}," +
"goods_imgUri=#{goodsImgUri},goods_expire=#{goodsExpire},goods_produceDate=#{goodsProduceDate}," +
"goods_producer=#{goodsProducer},goods_num=#{goodsNum},goods_price=#{goodsPrice}")
public boolean updateGoodsById(Goods goods);
//查找商品
@Select("select * from goods where goods_id = #{goodsId}")
public Goods selectGoodsById(int goodsId);
//根据商品名查找商品
@Select("select * from goods where goods_name = #{goodsName}")
public List<Goods> selectGoodsesByName(String goodsName);
//根据商品是否是热点商品来查找商品
@Select("select * from goods where goods_isHotSpot = #{goodsHotSpot}")
public List<Goods> selectGoodsesByHotSpot(boolean goodsHotSpot);
}
其他spring项目的启动项加上注解
找到当前项目的启动类,给它加上这个注解:
@MapperScan("bugaosuni")
里面的(“bugaosuni”)就是我的dao包的class