MyBatisPlus--MyBatisX插件

MyBatisX一款基于 IDEA 的快速开发插件,为效率而生。

官网地址:https://baomidou.com/pages/ba5b24

1、安装

打开 IDEA,进入 File -> Settings -> Plugins -> Browse Repositories,输入 mybatisx 搜索并安装。如下图:
在这里插入图片描述

2、代码快速生成

快速创建新的SpringBoot工程,参考MyBatisPlus–入门案例_杼蛘的博客-CSDN博客

a>在IDEA中打开数据库,选择数据源,如下:
在这里插入图片描述
b>会显示如下界面,填入数据库相关信息:
在这里插入图片描述
c>点击测试连接,如下图:
在这里插入图片描述

若填入信息均正确,但是连接失败,可选择低版本MySQL,如下图:

在这里插入图片描述

d>点击确定,会显示如下图,会将该数据库所有的表显示出来:
在这里插入图片描述
e>选中任意一个表,右键->MybatisX-Generator,会弹出如下图:
在这里插入图片描述

描述说明
module path工程路径
base package基础包,所有生成的内容都会在该包下,一般自定义
encoding编码,保持默认
superClass父类
base path主程序路径
relative package实体类生成的包,一般命名为pojoentity
ignore field prefix忽略表中字段前缀,如t_id,生成实体属性时,将忽略前缀t_
ignore field suffix忽略表中字段后缀,如id_s,生成实体属性时,将忽略后缀_s
ignore table prefix忽略表前缀,如t_user,生成实体类时,将忽略前缀t_
ignore table suffix忽略表后缀,如user_s,生成实体类时,将忽略后缀_s

若IDEA版本无该右键选项,建议重启IDEA或升级一下

f>点击module path框,弹出如下,直接选择新创建工程:
在这里插入图片描述
g>填好相关内容,如下:
在这里插入图片描述
h>点击Next,如下图,并选择如下:
在这里插入图片描述
t>点击Finish,查看工程的项目结构,如下图:
在这里插入图片描述

3、快速生成CRUD

插入

打开自动生成的UserMapper接口,输入名称,如插入一条用户记录,将不为null的字段进行插入,如下:
在这里插入图片描述

名称必须规范,查询以select开头、插入用insert开头、修改用update开头、删除用delete开头

不过MyBatisX会有名称提示,一般直接选择,如敲inser,选择第一个:

在这里插入图片描述

光标位于名称后面,按alt+回车,如下图(选择第二个):
在这里插入图片描述
此时方法自动补全,如下:

int insertSelective(User user);

查看对应mapper映射文件,路径src/main/resources/mapper/UserMapper.xml,自动生成:

<insert id="insertSelective">
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="id != null">id,</if>
        <if test="name != null">name,</if>
        <if test="age != null">age,</if>
        <if test="email != null">email,</if>
    </trim>
    values
    <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="id != null">#{id,jdbcType=BIGINT},</if>
        <if test="name != null">#{name,jdbcType=VARCHAR},</if>
        <if test="age != null">#{age,jdbcType=INTEGER},</if>
        <if test="email != null">#{email,jdbcType=VARCHAR},</if>
    </trim>
</insert>
删除

若根据id和age组合条件来进行删除,如下:
在这里插入图片描述
光标位于名称后面,按alt+回车,选择[MyBatisX]Generate MyBatis Sql,自动补全方法如下:

int deleteByIdAndAge(@Param("id") Long id, @Param("age") Integer age);

查看对应mapper映射文件,路径src/main/resources/mapper/UserMapper.xml,自动生成:

<delete id="deleteByIdAndAge">
    delete
    from user
    where id = #{id,jdbcType=NUMERIC}
    AND age = #{age,jdbcType=NUMERIC}
</delete>
修改

若通过id修改age和email,如下:
在这里插入图片描述
光标位于名称后面,按alt+回车,选择[MyBatisX]Generate MyBatis Sql,自动补全方法如下:

int updateAgeAndEmailById(@Param("age") Integer age, @Param("email") String email, @Param("id") Long id);

查看对应mapper映射文件,路径src/main/resources/mapper/UserMapper.xml,自动生成:

<update id="updateAgeAndEmailById">
    update user
    set age   = #{age,jdbcType=NUMERIC},
    email = #{email,jdbcType=VARCHAR}
    where id = #{id,jdbcType=NUMERIC}
</update>
查询

若根据name来进行模糊查询,如下:
在这里插入图片描述
光标位于名称后面,按alt+回车,选择[MyBatisX]Generate MyBatis Sql,自动补全方法如下:

List<User> selectAllByNameLike(@Param("name") String name);

查看对应mapper映射文件,路径src/main/resources/mapper/UserMapper.xml,自动生成:

<resultMap id="BaseResultMap" type="com.aiw.mybatisx.pojo.User">
    <id property="id" column="id" jdbcType="BIGINT"/>
    <result property="name" column="name" jdbcType="VARCHAR"/>
    <result property="age" column="age" jdbcType="INTEGER"/>
    <result property="email" column="email" jdbcType="VARCHAR"/>
</resultMap>
<select id="selectAllByNameLike" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from user
    where
    name like #{name,jdbcType=VARCHAR}
</select>

其它查询自测,步骤是一样的

4、测试

添加测试方法,测试类路径为src/test/java/com/aiw/mybatisx/MybatisxApplicationTests.java

@SpringBootTest
class MybatisxApplicationTests {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Test
    public void testMyBatisX() {
        userMapper.insertSelective(null);
        userMapper.deleteByIdAndAge(1L, 18);
        userMapper.updateAgeAndEmailById(20, "100@qq.com", 3L);
        userMapper.selectAllByNameLike("王");
    }

}
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
MybatisPlus 是 Mybatis 的增强工具,可以简化 Mybatis 的开发。MybatisX 是一款 Mybatis 开发插件,可以提高 Mybatis 的开发效率。下面是 MybatisPlus+SpringBoot+MybatisX 的联合步骤: 1. 在 pom.xml 文件中添加 MybatisPlus 和 MybatisX 的依赖。 ```xml <!-- MybatisPlus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> <!-- MybatisX --> <dependency> <groupId>io.github.mybatisx</groupId> <artifactId>mybatisx-boot-starter</artifactId> <version>2.1.0</version> </dependency> ``` 2. 配置 MybatisPlus 和 MybatisX。 ```java @Configuration public class MybatisConfig { @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } @Bean public MybatisXConfigurer mybatisXConfigurer() { return new MybatisXConfigurer(); } } ``` 3. 编写实体类和 Mapper 接口,使用 MybatisPlus 提供的注解。 ```java @Data @TableName("user") public class User { @TableId(type = IdType.AUTO) private Long id; private String username; private String password; } public interface UserMapper extends BaseMapper<User> { } ``` 4. 在 application.yml 中配置数据库连接信息和 MybatisPlus 的相关信息。 ```yaml spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 username: root password: root mybatis-plus: mapper-locations: classpath:/mapper/*Mapper.xml type-aliases-package: com.example.demo.entity global-config: db-config: id-type: auto field-strategy: not_null logic-delete-value: 1 logic-not-delete-value: 0 configuration: map-underscore-to-camel-case: true cache-enabled: false mybatisx: enabled: true ``` 5. 在 Controller 中使用 Mapper 接口进行数据库操作。 ```java @RestController public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/user/{id}") public User getUser(@PathVariable("id") Long id) { return userMapper.selectById(id); } @PostMapping("/user") public boolean addUser(@RequestBody User user) { return userMapper.insert(user) > 0; } @PutMapping("/user") public boolean updateUser(@RequestBody User user) { return userMapper.updateById(user) > 0; } @DeleteMapping("/user/{id}") public boolean deleteUser(@PathVariable("id") Long id) { return userMapper.deleteById(id) > 0; } } ``` 这样,就完成了 MybatisPlus+SpringBoot+MybatisX 的整合。在 Controller 中,可以直接注入 Mapper 接口进行数据库操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杼蛘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值