当前位置: 首页> 文旅> 酒店 > 淮南定制网站建设公司_京津冀协同发展10周年_瑞昌网络推广_网站免费优化软件

淮南定制网站建设公司_京津冀协同发展10周年_瑞昌网络推广_网站免费优化软件

时间:2025/7/10 8:01:58来源:https://blog.csdn.net/qq_74623042/article/details/144546482 浏览次数:0次
淮南定制网站建设公司_京津冀协同发展10周年_瑞昌网络推广_网站免费优化软件

这里写目录标题

  • Mybatis-plus
    • 快速使用
      • 1.引入起步依赖
      • 2.继承BaseMapper
      • 3.常见注解
        • TableName
        • @TableId
        • @TableField 的常见使用场景
      • 4.常见配置
    • 条件构造器
      • AbstractWrepper
    • 自定义 SQL
    • Service 接口
      • 使用 IService 接口
      • 实现 接口案例
        • controller
        • service
        • Mapper

Mybatis-plus

  1. MybatisPlus 对 Mybatis 只做增强不做修改。
  2. 简单配置,即可快速进行单表 CRUD(增删改查),提高效率。

快速使用

1.引入起步依赖

MybatisPlus 依赖能 代替 Mybatis 依赖

<!--MybatisPlus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.3.1</version></dependency>

2.继承BaseMapper

为Mapper类继承 BaseMapper<实体类名>

public interface UserMapper extends BaseMapper<User> {
}

3.常见注解

mybatisPlus 中默认约定为

  1. 类名驼峰下划线 作为 表名
  2. 名为 id 的字段作为 主键
  3. 变量名驼峰下划线 作为 表的字段名
TableName

如果不遵守约定就得使用以下注解:

@TableName("数据库表名")
public class User {@TableId(value = "表中主键名")private Long userid;
}
@TableId
@TableId 属性名作用
value绑定表中主键名
typeIdType 枚举
IdType枚举值的作用
AUTO:数据库中 主键自增长
INPUT:通过 set 方法 自行输入
ASSIGN_ID:分配 ID,接口 IdentifierGenerator 的方法 nextId 来生成 id ,默认实现类为 DefaultIdentifierGenerator 雪花算法
@TableField 的常见使用场景
  1. 成员变量名 与 数据库字段名 不一致
  2. 成员变量名 以 is 开头,且是 布尔值
  3. 成员变量名 与 数据库关键字冲突

成员变量 不是数据库字段

 @TableField("username")private String name;@TableField("is_married")private Boolean isMarried; //为bool值 is会被去除 对应字段名: married@TableField("`order`")private Integer order;@TableField(exist = false)private String address;     // 数据库不使用该属性

4.常见配置

MyBatisPlus 的配置项 继承了 MyBatis原生配置 和一些自己 特有的配置

mybatis-plus:type-aliases-package: com.itheima.mp.domain.po # 别名扫描包mapper-locations: classpath*:/mapper/**/*.xml# Mapper.xml文件地址,默认值  /** 可以扫描子文件夹configuration: # 配置map-underscore-to-camel-case: true  # 是否开启下划线和驼峰的映射cache-enabled: false  # 是否开启二级缓存global-config:  # 全局配置db-config:id-type: assign_id  # id为雪花算法生成update-strategy: not_null # 更新策略:只更新非空字段

条件构造器

目的:当查询条件不根据 ID 查询时,就要传入 条件构造器 来查询。
在这里插入图片描述

在这里插入图片描述

AbstractWrepper

实现SQL中的 where 部分:

函数作用
eq(“列名”,值)=
ne(“列名”,值)!=
gt(“列名”,值)>
ge(“列名”,值)>=
lt(“列名”,值)<
le (“列名”,值)<=
like模糊
notLike非模糊
likeLeft%在左
likeRight%在右
// 基于QueryWrapper案例@Testvoid testQueryWrapper(){
//查询name 带 o 的,存款 >= 1000 的 id, username,info,balance// 1. 构建查询条件  where部分QueryWrapper<User>wrapper=new QueryWrapper<User>().select("id","username","info","balance").like("username","o").ge("balance",1000);//2. 进行查询List<User>users=userMapper.selectList(wrapper);users.forEach(System.out::println);}// 基于QueryWrapper案例@Testvoid testUpdateByQueryWrapper(){//更新 username 为jack 用户 的 余额 为2000User user = new User(); // set  部分user.setBalance(2000);//  where 条件QueryWrapper<User>wrapper=new QueryWrapper<User>().eq("username","jack");userMapper.update(user,wrapper);//           要更新的用户  更新条件}
 // 基于UpdateWrapper案例@Testvoid testUpdateWrapper(){List<Long>ids= Collections.unmodifiableList(Arrays.asList(1L,2L,3L));// update user set balance=balance -200 where id in (1,2,3)UpdateWrapper<User>wrapper=new UpdateWrapper<User>().setSql("balance=balance-200")    // set 部分// .setSql("status=status-1").in("id",ids);          //whereuserMapper.update(null,wrapper);}

自定义 SQL

可以利用 MyBatisPlus 的 Wrapper 来构建复杂的 Where条件,然后自己定义SQL语句中剩下的部分。

  1. 基于Wrapper构建 where 条件
@Testvoid testConstomSqlUpdate(){List<Long>ins=Collections.unmodifiableList(Arrays.asList(1L,2L,3L));int amount=200;QueryWrapper<User>wrapper=new QueryWrapper<User>().in("id",ins);//传入   条件构建  减少的参数userMapper.updateBalanceByIds(wrapper,amount);}
  1. mapper方法参数中用 @Param() 注解声明 wrapper 变量名称,必须是 ew
                  // Constants  常量    或传入  "ew"
void updateBalanceByIds(@Param(Constants.WRAPPER) QueryWrapper<User> wrapper, @Param("amount") int amount);
  1. 自定义SQL 并使用 Wrapper 条件
<update id="updateBalanceByIds"><!-- ew.customSqlSegment  自定义SQL片段-->     update user set balance=balance-#{amount} ${ew.customSqlSegment}
</update>

Service 接口

使用 IService 接口

MP 的 IService 接口中定义了许多业务的实现功能。为了方便使用,所以使用 ServiceImpl 类中实现了 IService接口。

当使用MP框架时,我们可以使用这些功能,来减少代码量。

在这里插入图片描述

使用方法为:

  1. 使用自定义 Service 接口继承 IService 接口并给泛型传入 实体对象名

  2. 创建自定义接口的 实现类,并继承 ServiceImpl 实现类。

public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService{
...
}

实现 接口案例

基于Restful风格实现下列接口:
在这里插入图片描述

controller
@RequestMapping("/users")
@RestController
@RequiredArgsConstructor    //Lombok 自动注入常量
@Api(tags = "用户管理接口")
public class UserController {private final IUserService userService;/* private  IUserService userService;public UserController(IUserService userService) {   //使用 spring推荐的 构造方法注入this.userService = userService;}*/@ApiOperation("新增用户接口")@PostMappingpublic void saveUser(@RequestBody UserFormDTO userDTO){// 1. 把DTO 拷贝到POUser user=BeanUtil.copyProperties(userDTO, User.class);  //hutool包 拷贝userService.save(user);}@ApiOperation("删除用户接口")@DeleteMapping("{id}")public void saveUser(@ApiParam("用户id") @PathVariable("id")Long id){// 1. 把DTO 拷贝到POuserService.removeById(id);}@ApiOperation("根据id查询用户接口")@GetMapping("{id}")public UserVO queryUserById(@ApiParam("用户id") @PathVariable("id")Long id){// 1. 查询POUser user=userService.getById(id);return BeanUtil.copyProperties(user,UserVO.class);}@ApiOperation("根据id查询用户接口")@GetMappingpublic List<UserVO> queryUserById(@ApiParam("用户id集合") @RequestParam("ids")List<Long> ids){// 1. 查询POList<User> users=userService.listByIds(ids);return BeanUtil.copyToList(users,UserVO.class);}// 开发复杂模块@ApiOperation("扣减用户余额接口")@PutMapping("/{id}/deduction/{money}")public void deductMoneyById(@ApiParam("用户id")@PathVariable("id")Long id,@ApiParam("扣减的金额")@PathVariable("money") Integer money){userService.deductBalance(id,money);}
}
service

除了扣减用户余额接口的接口,均可使用 IService 中定义的功能实现。所以在 自定义service 层中不需要实现。

实现扣减余额:

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {@Overridepublic void deductBalance(Long id, Integer money) {//1.查询用户User user=this.getById(id);//2.效验用户状态if (user==null||user.getStatus()==2){throw new RuntimeException("用户状态异常!");}//3.效验余额是否充足if (user.getBalance()<money){throw new RuntimeException("用户余额不足!");}//4.扣减余额baseMapper.deductBalance(id,money); //实现具体的扣费方式需要 自定义Mapper//以多态的方式调用}
}
Mapper

自定义 Mapper 中实现数据层操作。

public interface UserMapper extends BaseMapper<User> {@Update("update user set balance=balance - #{money} where id=#{id}")void deductBalance(@Param("id") Long id,@Param("money") Integer money);
}
关键字:淮南定制网站建设公司_京津冀协同发展10周年_瑞昌网络推广_网站免费优化软件

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: