沈阳网站制作公司哪家好,网站源代码上传,链接生成二维码,手工视频制作网站目录
一、什么是 TKMybatis
二、TKMybatis 使用
2.1 Springboot 项目中加入依赖
2.2 使用讲解
2.2.1 实体类中使用
2.2.2 dao中使用
2.2.3 Service 层中使用
2.3 实际案例
2.3.1 dao 层使用
2.3.2 service 层使用 一、什么是 TKMybatis
TKMybatis 是基于 Mybatis 框…目录
一、什么是 TKMybatis
二、TKMybatis 使用
2.1 Springboot 项目中加入依赖
2.2 使用讲解
2.2.1 实体类中使用
2.2.2 dao中使用
2.2.3 Service 层中使用
2.3 实际案例
2.3.1 dao 层使用
2.3.2 service 层使用 一、什么是 TKMybatis
TKMybatis 是基于 Mybatis 框架开发的一个工具内部实现了对单表的基本数据操作只需要简单继承 TKMybatis 提供的接口就能够实现无需编写任何 sql 即能完成单表操作。 二、TKMybatis 使用
2.1 Springboot 项目中加入依赖
!--通用mapper起步依赖--
dependencygroupIdtk.mybatis/groupIdartifactIdmapper-spring-boot-starter/artifactIdversion2.0.4/version
/dependency在 POJO 类中加入依赖
!--每个工程都有Pojo都需要用到该包对应的注解--
dependencygroupIdjavax.persistence/groupIdartifactIdpersistence-api/artifactIdversion1.0/versionscopecompile/scope
/dependency在启动类中配置 MapperScan 扫描
SpringBootApplication
MapperScan(basePackages {com.tom.order.mapper})
public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}
}2.2 使用讲解
2.2.1 实体类中使用
在实体类中常用的注解和意义为
Table描述数据库表信息主要属性有name(表名)、schema、catalog、uniqueConstraints等。
Id指定表主键字段无属性值。
Column描述数据库字段信息主要属性有name(字段名)、columnDefinition、insertable、length、nullable(是否可为空)、precision、scale、table、unique、updatable等。
ColumnType描述数据库字段类型可对一些特殊类型作配置进行特殊处理主要属性有jdbcType、column、typeHandler等。
其他注解如Transient、ColumnResult、JoinColumn、OrderBy、Embeddable等暂不描述 2.2.2 dao中使用
单表操作只需要继承 tk.mybatis 下的 Mapper 接口即可使用
import tk.mybatis.mapper.common.Mapper;Repository
public interface BrandMapper extends MapperBrand {
}查看具体使用内部都已经封装了基本的单表操作 2.2.3 Service 层中使用 操作类型介绍增加Mapper.insert(record)保存一个实体null的属性也会保存不会使用数据库默认值Mapper.insertSelective(record)保存一个实体忽略空值即没提交的值会使用使用数据库默认值 删除Mapper.delete(record);根据实体属性作为条件进行删除查询条件使用等号Mapper.deleteByExample(example)根据Example条件删除数据Mapper.deleteByPrimaryKey(key)根据主键字段进行删除方法参数必须包含完整的主键属性 修改Mapper.updateByExample(record,example)根据Example条件更新实体record包含的全部属性null值会被更新Mapper.updateByExampleSelective(record, example)根据Example条件更新实体record包含的不是null的属性值Mapper.updateByPrimaryKey(record)根据主键更新实体全部字段null值会被更新Mapper.updateByPrimaryKeySelective(record)根据主键更新属性不为null的值 查询Mapper.select(record)根据实体中的属性值进行查询查询条件使用等号Mapper.selectAll()查询全部结果Mapper.selectByExample(example)根据Example条件进行查询Mapper.selectByPrimaryKey(key)根据主键字段进行查询方法参数必须包含完整的主键属性查询条件使用等号Mapper.selectCount(record)根据实体中的属性查询总数查询条件使用等号Mapper.selectCountByExample(example)根据Example条件进行查询总数Mapper.selectOne(record) 根据实体中的属性进行查询只能有一个返回值有多个结果是抛出异常查询条件使用等号。 但是如果存在某个属性为int则会初始化为0。可能影响到实际使用 2.3 实际案例
2.3.1 dao 层使用 import tk.mybatis.mapper.common.Mapper;/*** DAO 使用通用Mapper* DSO接口需要继承 tk.mybatis.mapper.common.Mapper*/
Repository
public interface BrandMapper extends MapperBrand {}2.3.2 service 层使用
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;import java.util.List;Service
public class BrandServiceImpl implements BrandService {Autowiredprivate BrandMapper brandMapper;public Example createExample(Brand brand) {// 自定义条件搜索对象 ExampleExample example new Example(Brand.class);Example.Criteria criteria example.createCriteria(); //条件构造器if (brand ! null) {if (!StringUtils.isEmpty(brand.getName())) {criteria.andLike(name, % brand.getName() %);}if (!StringUtils.isEmpty(brand.getLetter())) {criteria.andEqualTo(letter, brand.getLetter());}}return example;}Overridepublic ListBrand findAll() {return brandMapper.selectAll();}Overridepublic ListBrand findList(Brand brand) {Example example createExample(brand);return brandMapper.selectByExample(example);}Overridepublic Brand findById(Integer id) {return brandMapper.selectByPrimaryKey(id);}/*** 分页查询* param page 当前页* param size 每页显示的条数* return*/Overridepublic PageInfoBrand pageSearch(Integer page, Integer size) {// 分页实现// 后面的查询必须是紧跟集合查询PageHelper.startPage(page, size);// 查询集合ListBrand brands brandMapper.selectAll();return new PageInfoBrand(brands);}Overridepublic PageInfoBrand pageSearchAndCondition(Brand brand, Integer page, Integer size) {// 开始分页PageHelper.startPage(page, size);// 搜索数据Example example createExample(brand);ListBrand list brandMapper.selectByExample(example);return new PageInfoBrand(list);}/*** 增加品牌* param brand*/Overridepublic void add(Brand brand) {// 使用通用 Mapper.insertSelective// 方法中但凡带有selective就会忽略空值int i brandMapper.insertSelective(brand);}/*** 根据id修改品牌* param brand*/Overridepublic void update(Brand brand) {// 使用通用mapper.update();brandMapper.updateByPrimaryKeySelective(brand);}/*** 根据id删除* param id*/Overridepublic void del(Integer id) {brandMapper.deleteByPrimaryKey(id);}
}三、扩展介绍
https://juejin.im/post/5cbfd158f265da038860b82f --------------------- 作者AldarLin 来源CSDN 原文https://blog.csdn.net/qq_34416331/article/details/106322596 版权声明本文为作者原创文章转载请附上博文链接 内容解析ByCSDN,CNBLOG博客文章一键转载插件