一、整合mybatis
项目依赖(有的依赖是在创建springboot的时候就有了,根据你自己的项目来选择添加)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 连接MySQL 依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!-- alibaba数据库连接池依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.1</version>
</dependency>
<!-- lombok依赖 在实体类中使用注解@Data 可以免写get、set方法。简化代码 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
</dependencies>
application.yml配置(有的是application.properties,两者只是编写风格不一样而已)
#项目启动端口号
server:
port: 8081
spring:
application:
#应用名称
name: springboottest
#连接数据库
datasource:
# localhost是数据库所在主机,你也可以使用ip。test是数据库名称
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
# 驱动
driver-class-name: com.mysql.cj.jdbc.Driver
# 用户密码
username: root
password: 123456
# 指定连接池
type: com.alibaba.druid.pool.DruidDataSource
#配置mapper
mybatis:
mapper-locations: classpath:mapper/*.xml
启动类上加上@MapperScan注解,里面的值是你mapper文件对应接口的文件夹
@MapperScan("com.example.springbootest.dao")
建表并添加测试数据
我这里使用的是Navicat进行建表(我的表建的比较复杂,你可以建得简单一点)
测试代码
实体类,@Data注解免写set、get方法,如果引用依赖后人不起作用,需要下载插件lombok
/**
* (Product)实体类
*
* @author makejava
* @since 2021-08-06 09:17:35
*/
@Data
public class Product implements Serializable {
private static final long serialVersionUID = 121293417784218945L;
/**
* 主键ID
*/
private Integer id;
/**
* 商品编码
*/
private String productNumber;
/**
* 商品名称
*/
private String productName;
/**
* 规格
*/
private String specification;
/**
* 商品分类
*/
private String productClassify;
/**
* 品牌
*/
private String productBrand;
/**
* 单位
*/
private String productUnit;
/**
* 税率
*/
private String taxRate;
/**
* 含税价格
*/
private Object taxIncluded;
/**
* 上架状态
*/
private Integer putawayState;
/**
* 上架时间
*/
private Date putawayTime;
/**
* 下架时间
*/
private Date outawayTime;
/**
* 产地
*/
private String originPlace;
/**
* 库存数量
*/
private Integer warehouseNumber;
/**
* 备注
*/
private String remark;
}
controller(控制层)
/**
* (Product)表控制层
*
* @author makejava
* @since 2021-08-06 09:17:40
*/
@RestController
@RequestMapping("product")
public class ProductController {
/**
* 服务对象
*/
@Resource
private ProductService productService;
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("selectOne")
public Product selectOne(Integer id) {
return this.productService.queryById(id);
}
}
业务层接口
/**
* (Product)表服务接口
*
* @author makejava
* @since 2021-08-06 09:17:38
*/
public interface ProductService {
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
Product queryById(Integer id);
}
业务层实现类
/**
* (Product)表服务实现类
*
* @author makejava
* @since 2021-08-06 09:17:39
*/
@Service("productService")
public class ProductServiceImpl implements ProductService {
@Resource
private ProductDao productDao;
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
@Override
public Product queryById(Integer id) {
return this.productDao.queryById(id);
}
}
dao层
/**
* (Product)表数据库访问层
*
* @author makejava
* @since 2021-08-06 09:17:37
*/
public interface ProductDao {
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
Product queryById(Integer id);
}
mapper文件,
namespace对应的值是你mapper文件对应的dao层接口类,是自定义的返回类型,type是对应的实体类,id自己随意定义。property是实体类的属性名称,column是你数据库表的字段名称,jdbcType是数据类型
<?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="com.example.springbootest.dao.ProductDao">
<resultMap type="com.example.springbootest.entity.Product" id="ProductMap">
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="productNumber" column="product_number" jdbcType="VARCHAR"/>
<result property="productName" column="product_name" jdbcType="VARCHAR"/>
<result property="specification" column="specification" jdbcType="VARCHAR"/>
<result property="productClassify" column="product_classify" jdbcType="VARCHAR"/>
<result property="productBrand" column="product_brand" jdbcType="VARCHAR"/>
<result property="productUnit" column="product_unit" jdbcType="VARCHAR"/>
<result property="taxRate" column="tax_rate" jdbcType="VARCHAR"/>
<result property="taxIncluded" column="tax_included" jdbcType="VARCHAR"/>
<result property="putawayState" column="putaway_state" jdbcType="INTEGER"/>
<result property="putawayTime" column="putaway_time" jdbcType="TIMESTAMP"/>
<result property="outawayTime" column="outaway_time" jdbcType="TIMESTAMP"/>
<result property="originPlace" column="origin_place" jdbcType="VARCHAR"/>
<result property="warehouseNumber" column="warehouse_number" jdbcType="INTEGER"/>
<result property="remark" column="remark" jdbcType="VARCHAR"/>
</resultMap>
<!--查询单个-->
<select id="queryById" resultMap="ProductMap">
select
id, product_number, product_name, specification, product_classify, product_brand, product_unit, tax_rate, tax_included, putaway_state, putaway_time, outaway_time, origin_place, warehouse_number, remark
from test.product
where id = #{id}
</select>
</mapper>
使用postman测试,有返回结果,整合成功