当前位置: 首页 > wzjs >正文

外贸社交网站排名天津网站备案

外贸社交网站排名,天津网站备案,海报制作网站免费,二级域名做网站注意前言 在现代的 Java Web 开发中,Spring Boot 和 MyBatis 已经成为主流框架组合。为了提升开发效率和简化数据库操作,MyBatis-Plus(简称 MP)应运而生。它是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改…

前言

在现代的 Java Web 开发中,Spring Boot 和 MyBatis 已经成为主流框架组合。为了提升开发效率和简化数据库操作,MyBatis-Plus(简称 MP)应运而生。它是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

本文将详细介绍如何在 Spring Boot 项目中集成并使用 MyBatis-Plus,包括:

  • 环境搭建
  • 基本 CRUD 操作
  • 使用 Wrapper 构造查询条件
  • 注解的使用(如 @TableName@TableId@TableField
  • 分页插件配置
  • 多表关联查询示例

一、环境准备

1. 创建 Spring Boot 项目

你可以使用 Spring Initializr 创建一个基础项目,选择以下依赖:

  • Spring Web
  • MyBatis Framework
  • MySQL Driver

或者使用 IDEA 或 Eclipse 插件创建。

2. 添加 MyBatis-Plus 依赖

pom.xml 文件中添加 MyBatis-Plus 的 Starter 依赖:

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

如果你需要使用代码生成器,也可以加上:

<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.5.1</version>
</dependency>

3. 配置数据库连接

application.yml 中配置数据源:

spring:datasource:url: jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC&characterEncoding=utf8username: rootpassword: your_passworddriver-class-name: com.mysql.cj.jdbc.Drivermybatis-plus:mapper-locations: classpath*:mapper/**/*.xmltype-aliases-package: com.example.entity

二、实体类与注解

1. 表结构示例

假设我们有一个用户表 user

CREATE TABLE user (id BIGINT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(50),age INT,email VARCHAR(100)
);

2. 实体类定义

package com.example.entity;import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;@Data
@TableName("user") // 映射到 user 表
public class User {@TableId(value = "id", type = IdType.AUTO) // 主键映射private Long id;@TableField("name") // 字段映射,默认自动识别驼峰命名转下划线private String name;@TableField("age")private Integer age;@TableField("email")private String email;
}

说明:

  • @TableName: 映射实体类与数据库表名。
  • @TableId: 标识主键字段,可指定数据库列名。
  • @TableField: 映射普通字段,若不写则默认按字段名转换成下划线匹配数据库列。
  • IdType.AUTO: 自动识别主键类型(自增、UUID 等),也可手动设置如 IdType.NONE, IdType.INPUT 等。

三、Mapper 接口与 Service 层

1. Mapper 接口

package com.example.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.User;public interface UserMapper extends BaseMapper<User> {
}

继承 BaseMapper 后即可获得基本的 CRUD 方法。

2. Service 层接口与实现

package com.example.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.example.entity.User;public interface UserService extends IService<User> {
}
package com.example.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import org.springframework.stereotype.Service;@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}

四、Wrapper 查询构造器详解

MyBatis-Plus 提供了强大的查询构造器 QueryWrapperUpdateWrapper,可以链式构建 SQL 条件。

示例:使用 QueryWrapper 查询用户

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;public List<User> getUsersByNameAndAge(String name, int minAge) {return userMapper.selectList(new QueryWrapper<User>().like("name", name) // LIKE '%name%'.ge("age", minAge)); // age >= minAge
}

更多常用方法:

方法说明
eq(column, value)等于
ne(column, value)不等于
gt(column, value)大于
ge(column, value)大于等于
lt(column, value)小于
le(column, value)小于等于
like(column, value)LIKE
in(column, collection)IN 查询
between(column, val1, val2)BETWEEN 查询
orderByAsc(column) / orderByDesc(column)排序

LambdaQueryWrapper(推荐)

避免字符串硬编码错误,推荐使用 LambdaQueryWrapper

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;List<User> users = userMapper.selectList(new LambdaQueryWrapper<User>().like(User::getName, "Tom").ge(User::getAge, 18));

五、分页插件配置

1. 配置分页插件

在配置类中启用分页功能:

package com.example.config;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MyBatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor());return interceptor;}
}

2. 使用分页查询

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;public Page<User> getUsersByPage(int pageNum, int pageSize) {return userMapper.selectPage(new Page<>(pageNum, pageSize), null);
}

你也可以结合 Wrapper:

Page<User> page = userMapper.selectPage(new Page<>(1, 10),new LambdaQueryWrapper<User>().ge(User::getAge, 20)
);

六、多表关联查询(以一对一为例)

1. 新建 Address 表及实体类

CREATE TABLE address (id BIGINT PRIMARY KEY AUTO_INCREMENT,user_id BIGINT,detail VARCHAR(200)
);
package com.example.entity;import com.baomidou.mybatisplus.annotation.*;@Data
@TableName("address")
public class Address {@TableId(type = IdType.AUTO)private Long id;private Long userId;private String detail;
}

2. 修改 User 类(增加关联字段)

package com.example.entity;import lombok.Data;@Data
public class UserVO extends User {private Address address;
}

3. 自定义 SQL 查询(XML 方式)

<!-- UserMapper.xml -->
<select id="selectUserWithAddress" resultType="com.example.entity.UserVO">SELECT u.*, a.detail AS address_detailFROM user uLEFT JOIN address a ON u.id = a.user_idWHERE u.id = #{id}
</select>

4. Mapper 接口中声明方法

UserVO selectUserWithAddress(Long id);

七、总结

通过本文,你应该已经掌握了:

  • 如何在 Spring Boot 中集成 MyBatis-Plus;
  • 使用 @TableName@TableId@TableField 注解进行 ORM 映射;
  • 使用 Wrapper 构建灵活查询条件;
  • 使用分页插件进行分页;
  • 多表关联查询的基本方式。

MyBatis-Plus 是一个非常实用的 MyBatis 扩展库,能够显著提升开发效率,建议结合官方文档进一步深入学习其高级特性,例如自动填充、乐观锁、逻辑删除、多租户等。


参考资料

  • MyBatis-Plus 官方文档
  • Spring Boot 官方文档

文章转载自:

http://YUx0hWYN.zpxwg.cn
http://gV8g1gV0.zpxwg.cn
http://NQjjf2CZ.zpxwg.cn
http://mIWIvz32.zpxwg.cn
http://DDwrcSao.zpxwg.cn
http://KzwejLgY.zpxwg.cn
http://31pLhJYT.zpxwg.cn
http://VIU0NGp2.zpxwg.cn
http://zFch7ZpB.zpxwg.cn
http://ONOZM2Ax.zpxwg.cn
http://lBXbE5Y5.zpxwg.cn
http://gdgfnrk6.zpxwg.cn
http://18PU7awT.zpxwg.cn
http://RcnWGFL3.zpxwg.cn
http://Hn2TjKPF.zpxwg.cn
http://J73S2zqv.zpxwg.cn
http://H7vyXLyG.zpxwg.cn
http://7hQmU2FR.zpxwg.cn
http://fENy5pb7.zpxwg.cn
http://OJigGGRO.zpxwg.cn
http://gVXVmSyR.zpxwg.cn
http://vk8IKfNY.zpxwg.cn
http://6lyyZAT5.zpxwg.cn
http://WpeDmrSB.zpxwg.cn
http://94lL6Tvu.zpxwg.cn
http://hxEJrP6P.zpxwg.cn
http://zfytHPLD.zpxwg.cn
http://NcGjTSNt.zpxwg.cn
http://e1DjlC6R.zpxwg.cn
http://08MAi8mg.zpxwg.cn
http://www.dtcms.com/wzjs/734539.html

相关文章:

  • 新国际网站建设最新新闻热点头条
  • 专注宜昌网站建设wordpress 读者墙
  • 网站建设远洋国际广东省做网站推广公司
  • 链接网站制作松江品划网站建设维护
  • 天津网站建设设计费用安康网站制作公司
  • 摩托车建设网站大连甘井子区社区工作者招聘
  • 手机的网站有哪些网站建设广西
  • 四川建设信息共享网站网络公司网站推广
  • 如何布置网站快速模仿一个网站
  • 宝安建网站的公司wordpress 繁简转换插件
  • 做网站 分类搜索杭州手机网站建设
  • 上海网站推广提供商雅虎搜索引擎
  • 社区网站的作用设计素材免费下载网站
  • 分析海报的网站ui设计作品解析
  • 地方网站怎么做app开发制作的价格
  • 广州建设h5网站南宁论坛
  • 网站建设价格很 好乐云seo网站开发后台
  • 网站集约化建设要求WordPress国外音乐播放器
  • 做蛋糕招聘网站定制和订制的区别
  • 如何关闭wordpress默认编辑器佛山网站优化推广方案
  • 电商网站建站网络推广最好的网站
  • 号网站开发嵌入式软件工程师待遇
  • php网站开发技术文档北京网站备案代理
  • 浙江龙元建设集团 网站搜索平台
  • 动态电子商务网站 制作Wordpress 外链图片6
  • 秦皇岛建设局局官方网站wordpress 上传按钮
  • 中山视角做网站的公司中国三大生产建设兵团
  • 门户网站 架构杭州网站排名服务
  • 装饰网站设计模板下载无锡找厂网站
  • 做通信毕业设计的网站网站建设需要什么硬件和软件