MyBatis快速上手与知识点总结

阅读提示:
本文默认已经预装预装maven

1、MyBatis概述

1.1 MyBatis概述

  • 持久层框架,用于简化JDBC开发,是对JDBC的封装

    持久层:

  • 负责将数据保存到数据库的代码部分
  • Java EE三层架构:表现层、业务层、持久层

1.2 JDBC缺点

  • 硬编码,不利于维护
  • 注册驱动、获取连接
  • SQL语句
  • 操作繁琐
  • 手动设置参数
  • 手动封装结果集

1.3 MyBatis优化

  • 硬编码 –> 配置文件
  • 繁琐惭怍 –> 框架封装自动完成

2、MyBatis快速入门

  • 需求:查询user表中的所有数据
  • SQL
create database mybatis;
use mybatis;

drop table if exists tb_user;

create table tb_user(
    id int primary key auto_increment,
    username varchar(20),
    password varchar(20),
    gender char(1),
    addr varchar(30)
);

INSERT INTO tb_user VALUES (1, 'zhangsan', '123', '男', '北京');
INSERT INTO tb_user VALUES (2, '李四', '234', '女', '天津');
INSERT INTO tb_user VALUES (3, '王五', '11', '男', '西安');
  • 代码实现
  • 创建模块,导入坐标

    在pom.xml中配置文件中添加依赖的坐标 注意:需要在项目的resources目录下创建logback的配置文件


        org.mybatis
        mybatis
        3.5.5

        mysql
        mysql-connector-java
        5.1.46

        junit
        junit
        4.13
        test

        org.slf4j
        slf4j-api
        1.7.20

        ch.qos.logback
        logback-classic
        1.2.3

        ch.qos.logback
        logback-core
        1.2.3

  • 编写MyBatis核心文件

    核心文件用于替换信息,解决硬编码问题 在模块下的resources目录下创建mybatis的配置文件 mybatis-config.xml


  • 编写SQL映射文件

    SQL映射文件用于统一管理SQL语句,解决硬编码问题 在模块的resources目录下创建映射配置文件 UserMaooer.xml


        select * from tb_user;

  • 编码
    • 实体类
package priv.dandelion.entity;

public class User {

    private Integer id;
    private String username;
    private String password;
    private String gender;
    private String address;

    public User() {
    }

    public User(Integer id, String username, String password, String gender, String address) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.gender = gender;
        this.address = address;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", gender='" + gender + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

+ 测试类
public static void main(String[] args) throws IOException {
    // 加载mybatis的核心配置文件,获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    // 获取Session对象,执行SQL语句
    SqlSession sqlSession = sqlSessionFactory.openSession();
    // 执行SQL,处理结果
    List users = sqlSession.selectList("test.selectAll");
    System.out.println(users);
    // 释放资源
    sqlSession.close();
}

3、Mapper代理开发

3.1 Mapper代理开发概述

解决形如上述测试类中 List<user> users = sqlSession.selectList("test.selectAll");</user>的硬编码问题

  • 解决原生方式中的硬编码
  • 简化后期的SQL执行

3.2 使用Mapper代理要求

  • 定义与SQL映射文件同名的Mapper接口,并且将Mapper接口和SQL映射文件放置在同一目录下

    maven项目开发时要求code和resources分开,可在resources中创建相同包文件来是实现上述效果

  • 设置SQL映射文件的 namespace属性未Mapper接口的全限定名
  • 在Mapper接口中定义方法,方法名就是SQL映射文件中SQL语句的 id,并且参数类型和返回值类型一致

3.3 案例代码实现

  • 修改SQL映射文件 UserMapper.xml

    同时还要修改其路径


        select * from tb_user;

  • 创建对应的Mapper接口 UserMapper.interface
public interface UserMapper {
    List selectAll();
}
  • 修改mybatis核心配置文件中加载SQL映射的 <mapper></mapper>的路径

  • 测试代码
public static void main(String[] args) throws IOException {
    // 加载mybatis的核心配置文件,获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    // 获取Session对象,执行SQL语句
    SqlSession sqlSession = sqlSessionFactory.openSession();
    // 执行SQL,处理结果
    // 获取UserMapper接口的代理对象
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    List users = userMapper.selectAll();
    System.out.println(users);
    // 释放资源
    sqlSession.close();
}
  • 改进

    如果Mapper接口名称和SQL映射文件名称相同,并在同一目录下,则可以使用包扫描的方式简化SQL映射文件的加载,简化mybatis核心配置文件


4、核心配置文件

4.1 多环境配置

在核心配置文件的 environments 标签中其实是可以配置多个 environment ,使用 id 给每段环境起名,在 environments 中使用 default='&#x73AF;&#x5883;id' 来指定使用哪儿段配置。我们一般就配置一个 environment 即可


4.2 类型别名

映射配置文件中的 resultType属性需要配置数据封装的类型(类的全限定名),繁琐
Mybatis 提供了 &#x7C7B;&#x578B;&#x522B;&#x540D;(typeAliases) 可以简化这部分的书写



        select * from tb_user;

5、配置文件实现CRUD

5.1 环境准备

  • SQL
-- 删除tb_brand表
drop table if exists tb_brand;
-- 创建tb_brand表
create table tb_brand
(
    -- id 主键
    id           int primary key auto_increment,
    -- 品牌名称
    brand_name   varchar(20),
    -- 企业名称
    company_name varchar(20),
    -- 排序字段
    ordered      int,
    -- 描述信息
    description  varchar(100),
    -- 状态:0:禁用  1:启用
    status       int
);
-- 添加数据
insert into tb_brand (brand_name, company_name, ordered, description, status)
values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0),
       ('华为', '华为技术有限公司', 100, '华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界', 1),
       ('小米', '小米科技有限公司', 50, 'are you ok', 1);

SELECT * FROM tb_brand;
  • 实体类
public class Brand {

    private Integer id;
    private String brand_name;
    private String company_name;
    private Integer ordered;
    private String description;
    private Integer status;

    public Brand() {
    }

    public Brand(
            Integer id,
            String brand_name,
            String company_name,
            Integer ordered,
            String description,
            Integer status
    ) {
        this.id = id;
        this.brand_name = brand_name;
        this.company_name = company_name;
        this.ordered = ordered;
        this.description = description;
        this.status = status;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getBrand_name() {
        return brand_name;
    }

    public void setBrand_name(String brand_name) {
        this.brand_name = brand_name;
    }

    public String getCompany_name() {
        return company_name;
    }

    public void setCompany_name(String company_name) {
        this.company_name = company_name;
    }

    public Integer getOrdered() {
        return ordered;
    }

    public void setOrdered(Integer ordered) {
        this.ordered = ordered;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Brand{" +
                "id=" + id +
                ", brand_name='" + brand_name + '\'' +
                ", company_name='" + company_name + '\'' +
                ", ordered=" + ordered +
                ", description='" + description + '\'' +
                ", status=" + status +
                '}';
    }
}
  • 安装插件MyBatisX
  • 步骤
  • 编写接口方法 Mapper&#x63A5;&#x53E3;
    • 参数
    • 返回值
  • 在SQL映射文件中编写SQL语句
    • MyBatisX插件自动补全
    • 编写SQL
    • 若数据库字段名和实体类字段名不同,则需要解决该问题 (见 5.2 SQL映射文件)
  • 编写执行测试
    • 获取SqlSessionFactory
    • 获取sqlSession对象
    • 获取mapper接口的代理对象
    • 执行方法
    • 释放资源

5.2 查询所有数据

本节要点:

  1. 测试类的编写方式
  2. 解决数据库字段和实体类字段名不同的问题

  3. 编写接口方法

public interface BrandMapper {
    public List selectAll();
}
  • 编写SQL映射文件

        select *
        from tb_brand;

  • 编写测试方法
@Test
public void testSelectAll() throws IOException {
    // 获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    // 获取sqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession();

    // 获取mapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

    // 执行方法
    List brands = brandMapper.selectAll();
    System.out.println(brands);

    // 释放资源
    sqlSession.close();
}

5.3 查询

本节要点:

  1. MyBatis的SQL映射文件中,SQL语句如何接收对应参数

  2. 使用占位符进行参数传递

  3. 占位符名称和参数保持一致
  4. 占位符
    • #{&#x5360;&#x4F4D;&#x7B26;&#x540D;}:会替换为 ?防止SQL注入,一般用于替换字段值
    • ${&#x5360;&#x4F4D;&#x7B26;&#x540D;}:存在SQL注入问题,一般 用于执行动态SQL语句,如表名列名不固定的情况(见)
  5. 编写接口方法
void update(Brand brand);
  • SQL映射文件查询代码标签

    select *
    from tb_brand where id = #{id};

  • 测试方法
@Test
    public void testSelectByCondition() throws IOException {
        // 接收参数
        int status = 1;
        String companyName = "华为";
        String brandName = "华为";

        // 获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =
            new SqlSessionFactoryBuilder().build(inputStream);

        // 获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 获取mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        // 执行方法
        companyName = "%" + companyName + "%";
        brandName = "%" + brandName + "%";
        List brands = brandMapper.selectByCondition(status, companyName, brandName);
        System.out.println(brands);

        // 释放资源
        sqlSession.close();
    }

5.4 多条件查询

本节要点:

  1. 多条件查询:如果有多个参数,需要使用 @Paran("SQL&#x53C2;&#x6570;&#x5360;&#x4F4D;&#x7B26;&#x540D;&#x79F0;")注解
  2. 多条件的动态条件查询:对象属性名称要和参数占位符名称一致
    (详见5-2解决数据库字段和实体类字段名不同的问题)
  3. 单条件的动态条件查询:保证key要和参数占位符名称一致

  4. 多条件查询

  5. SQL映射文件

    select *
    from tb_brand
    where status = #{status}
    and company_name like #{companyName}
    and brand_name like #{brandName}

  • 散装参数
    • 接口
// 散装参数
List selectByCondition(
    @Param("status")int status,
    @Param("companyName")String companyName,
    @Param("brandName")String brandName
);
+ 测试方法
@Test
    public void testSelectByCondition() throws IOException {
        // 接收参数
        int status = 1;
        String companyName = "华为";
        String brandName = "华为";

        // 获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =
            new SqlSessionFactoryBuilder().build(inputStream);

        // 获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 获取mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        // 执行方法
        companyName = "%" + companyName + "%";
        brandName = "%" + brandName + "%";
        List brands = brandMapper.selectByCondition(
            status, companyName, brandName);
        System.out.println(brands);

        // 释放资源
        sqlSession.close();
    }
  • 对象参数
    • 接口
// 对象参数
List selectByCondition(Brand brand);
+ 测试方法
// 细节不表,仅展示执行方法
// 执行方法
Brand brand = new Brand();
brand.setStatus(status);
brand.setCompanyName("%" + companyName + "%");
brand.setBrandName("%" + brandName + "%");
List brands = brandMapper.selectByCondition(brand);
System.out.println(brands);
  • map集合参数
    • 接口
// 集合参数
List selectByCondition(Map map);
+ 测试方法
// 细节不表,仅展示执行方法
// 执行方法
Map map = new HashMap();
map.put("status", status);
map.put("companyName", "%" + companyName + "%");
map.put("brandName", "%" + brandName + "%");
List brands = brandMapper.selectByCondition(map);
System.out.println(brands);
  • 多条件动态条件查询

    优化条件查询,如页面上表单存在多个条件选项,但实际填写表单仅使用部分条件筛选的情况

  • SQL映射文件

    select *
    from tb_brand

            status = #{status}

            and company_name like #{companyName}

            and brand_name like #{brandName}

注:
1. if标签的 test属性中可以包含逻辑或等逻辑判断,使用 andor进行连接
2. 若条件SQL中同时包含 AND等连接符
* 对所有的条件前都加 AND,并在 WHERE后加任意真判断,即 WHERE 1=1 AND ... AND ...
* 加入 <if></if>判断标签造轮子,自行决定添加 AND的条件
* 使用 <where></where>标签替换原SQL中的 WHERE关键字,MyBatis将自动进行语法修正,如示例所示
* 单条件的动态条件查询
优化条件查询:如表单中存在多个条件筛选,但仅有其中一个生效的情况
– 使用标签
+ choose标签类似于 Java中的 switch
+ when标签类似于 Java中的 case
+ otherwise标签类似于 Java中的 default
– SQL映射文件


    select *
    from tb_brand

                status = #{status}

                company_name like #{companyName}

                brand_name like #{brandName}

5.6 添加数据与MyBatis事务

  • 添加
  • 接口
// 添加
void add(Brand brand);
  • SQL映射文件

    insert into tb_brand (brand_name, company_name, ordered, description, status)
    values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});

  • 测试方法
@Test
public void testAdd() throws IOException {
    // 接收参数
    int status = 1;
    String companyName = "aaa";
    String brandName = "xxx";
    String description = "这是一段介绍";
    int ordered = 100;

    // 获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory =
        new SqlSessionFactoryBuilder().build(inputStream);

    // 获取sqlSession对象
    // SqlSession sqlSession = sqlSessionFactory.openSession();
    SqlSession sqlSession = sqlSessionFactory.openSession(true);

    // 获取mapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

    // 执行方法
    Brand brand = new Brand();
    brand.setStatus(1);
    brand.setBrandName(brandName);
    brand.setCompanyName(companyName);
    brand.setDescription(description);
    brand.setOrdered(ordered);
    brandMapper.add(brand);
    System.out.println("添加成功");

    // 提交事务
    // sqlSession.commit();

    // 释放资源
    sqlSession.close();
}
  • Mybatis事务

    MyBatis默认手动事务,执行添加等操作时会自动回滚

  • MyBayis事务处理的方法
// 方法一:在获取sqlSession对象时设置参数,开启自动事务
SqlSession sqlSession = sqlSessionFactory.openSession(true);
sqlSession.close();
// 方法二:手动提交事务
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.commit();
sqlSession.close();
  • 添加 – 主键返回

    传入实体类对象进行数据添加,在数据添加完成后,会将 id信息写回该实体类对象

  • SQL映射文件

    insert into tb_brand (brand_name, company_name, ordered, description, status)
    values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});

  • 获取写回信息
Integer id = brand.getId();

5.7 修改

  • 修改整条数据
  • 接口
// 修改,可使用int返回值,返回受影响行数
void update(Brand brand);
  • SQL映射文件

    update tb_brand
    set brand_name = #{brandName},
    set company_name = #{companyName},
    set ordered = #{ordered},
    set description = #{description},
    set status = #{status}
    where id = #{id};

  • 修改部分字段

    优化上述代码应对仅修改部分属性导致其他属性数据丢失问题 使用 <set></set>标签替换set关键字列表, 区别于 <where></where> 标签,注意语法

  • SQL映射文件

    update tb_brand

            brand_name = #{brandName},

            company_name = #{companyName},

            ordered = #{ordered},

            description = #{description},

            status = #{status},

    where id = #{id};

  • 测试代码
@Test
public void testUpdate() throws IOException {
    // 接收参数
    int id = 5;
    int status = 0;
    String companyName = "AAA";
    String brandName = "XXX";
    int ordered = 300;

    // 获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory =
        new SqlSessionFactoryBuilder().build(inputStream);

    // 获取sqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession(true);

    // 获取mapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

    // 执行方法
    Brand brand = new Brand();
    brand.setId(id);
    brand.setStatus(status);
    brand.setBrandName(brandName);
    brand.setCompanyName(companyName);
    brand.setOrdered(ordered);
    brandMapper.update(brand);
    System.out.println("修改成功");

    // 释放资源
    sqlSession.close();
}

5.8 删除数据

  • 删除一条数据
  • 接口
// 删除一条数据
void deleteById(int id);
  • SQL映射文件

    delete
    from tb_brand
    where id = #{id};

  • 测试
@Test
public void testDeleteById() throws IOException {
    // 接收参数
    int id = 5;

    // 获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory =
        new SqlSessionFactoryBuilder().build(inputStream);

    // 获取sqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession(true);

    // 获取mapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

    // 执行方法
    brandMapper.deleteById(id);
    System.out.println("删除成功");

    // 释放资源
    sqlSession.close();
}
  • 批量删除数据

    用于解决删除时传入参数为数组的情况

  • 使用 <foreach></foreach>标签代替SQL语句中的 id in (?, ?, ..., ?)
    • collection属性为 MyBatis封装后数组对应的 key,封装后属性值应为 array(见注释)
    • MyBatis默认会将数组参数封装为 Map集合,其 keyarray,即 array = ids
    • 可在接口中对参数数组使用 @Param注解,将封装后的 key手动命名,则可在映射文件中使用
    • separator属性为分隔符
    • openclose属性分别为在 <foreach></foreach>前后拼接字符,主要用于代码规范,示例中未展示
  • 接口
// 删除多个数据
void deleteByIds(@Param("ids")int[] ids);
  • SQL映射文件

    delete
    from tb_brand
    where id
    in (

        #{id}

    );

  • 测试
@Test
public void testDeleteByIds() throws IOException {
    // 接收参数
    int[] ids = {6, 7};

    // 获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory =
        new SqlSessionFactoryBuilder().build(inputStream);

    // 获取sqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession(true);

    // 获取mapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

    // 执行方法
    brandMapper.deleteByIds(ids);
    System.out.println("删除成功");

    // 释放资源
    sqlSession.close();
}

5.9 MyBatis参数传递

  • 概述
  • 多个参数

    设有如下代码:

User select(@Param("username") String username,@Param("password") String password);

MyBatis会将散装的多个参数封装为Map集合
– 若不使用 @Param注解,则会使用以下命名规则:

map.put("arg0",参数值1);
map.put("arg1",参数值2);
map.put("param1",参数值1);
map.put("param2",参数值2);

即Map集合中的参数的key分别为 arg0, arg1, param1, param2
– 使用 @Param注解会将Map集合中的参数的arg替换为指定内容,增强其可读性
* 单个参数
– POJO类型:直接使用,要求属性名和参数占位符名称一致(见 5.2 SQL映射文件)
– Map集合类型:直接使用,要求key和参数占位符名称一致(见 5.2 SQL映射文件)
– Collection集合类型:封装Map集合,可以使用@Param注解替换Map集合中默认arg键名

map.put("arg0",collection集合);
map.put("collection",collection集合;
  • List集合类型:封装为Map集合,可以使用@Param注解,替换Map集合中默认的arg键名
map.put("arg0",list集合);
map.put("collection",list集合);
map.put("list",list集合);
  • Array类型:封装为Map集合,可以使用@Param注解,替换Map集合中默认的arg键名
map.put("arg0",数组);
map.put("array",数组);
  • 其他类型:直接使用,与参数占位符无关,但尽量见名知意

6、通过注解实现CRUD

  • 概述
  • 用于简化开发,可以对简单的查询使用注解进行操作,以替换 xml中的 statement
  • 对于复杂的查询,仍然建议使用 xml配置文件,否则代码会十分混乱
  • 使用方法
  • 注解(部分)
    • 查询 : @Select
    • 添加 : @Insert
    • 修改 : @Update
    • 删除 : @Delete
  • 示例
    • 使用注解简化查询
    • 原接口
Brand selectById(int id);
  * 原SQL映射文件

    select *
    from tb_brand
    where id = #{id};

  * 使用注解进行开发
    - 接口
@ResultMap("brandResultMap")        // 解决数据库和实体类字段名称不同
@Select("select * from tb_brand where id = #{id}")  // 查询语句
Brand selectById(int id);
    - SQL映射文件:不再需要原先的 statement

Original: https://www.cnblogs.com/dandelion-000-blog/p/16636393.html
Author: Dandelion_000
Title: MyBatis快速上手与知识点总结

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/609806/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • webkitdirectory实现文件夹上传

    webkitdirectory HTMLInputElement.webkitdirectory是属于 <input>元素的一个HTML属性webkitdirector…

    Java 2023年6月16日
    078
  • Spring AOP

    AOP简介: 面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。 *作用:在不惊动原始设计的基础上为其进行功能增强。 AOP核心概念 (1)Aspec…

    Java 2023年6月6日
    063
  • RenderX java的xml打印

    http://www.zdnet.com.cn/techupdate/apply/collaboration/story/0,3800030473,39347913,00.htm …

    Java 2023年5月29日
    088
  • 分布式任务调度平台XXL-JOB安装及使用

    一、为什么需要任务调度平台 在Java中,传统的定时任务实现方案,比如Timer,Quartz等都或多或少存在一些问题: 不支持集群、不支持统计、没有管理平台、没有失败报警、没有监…

    Java 2023年6月5日
    076
  • JAVA中容器设计的进化史:从白盒到黑盒,再到跻身为设计模式之一的迭代器

    大家好,又见面了。 在我们的项目编码中,不可避免的会用到一些 容器类,我们可以直接使用 List、 Map、 Set、 Array等类型。当然,为了体现业务层面的含义,我们也会根据…

    Java 2023年6月7日
    098
  • Linux中配置snappy压缩

    Linux中配置Snappy 首先查看本地库当前状态 1.查看命令:hadoop checknative 2.此时可以看到当前状态都是false,把这写问价你上传到/$HADOOP…

    Java 2023年6月9日
    069
  • [学习笔记] Java枚举

    在Java中,枚举是一种特殊的类,一般用于表示一组常量; 定义枚举时使用 enum关键字,各个常量使用逗号分隔; 也可以在类的内部定义枚举: 每个枚举都以内部类的形式实现,且所有的…

    Java 2023年6月5日
    067
  • rocket5.0支持延时队列

    ### 刚刚查看了rocketmq5.0的发布的源码,新增了不少功能,最关心的是居然支持延时队列了,对于大部分人来说还是方便了很多。 #### 我们来看看是如何使用的 //定时/延…

    Java 2023年6月5日
    060
  • 心存好奇,心怀敬意

    走出舒适圈难,是因为走出去,除了要吃学习的苦,还要忍受心里的苦。 看到郭德纲的一段话: 从出生就挨打,一天八个嘴巴。这到 25 岁,铁罗汉活金刚一样,什么都不在乎。吃亏要趁早,一帆…

    Java 2023年6月16日
    068
  • DNS服务端搭建

    Docker使用 sameersbn/bind 镜像搭建dns服务器 https://hub.docker.com/r/sameersbn/bind进行下面测试的时候记得将本机的d…

    Java 2023年5月30日
    086
  • Spring中controller中关于GET和POST请求的参数接收

    Spring中controller中关于GET和POST请求的参数接收 GET请求 参数用&符号连接在一起[/get?name=tom] //无参 //没有任何参数的请求 …

    Java 2023年6月9日
    052
  • java.time.LocalDate格式化 及 LocalDate转Date

    undefined import java.time.LocalDate; import java.time.LocalDateTime; import java.time.Zon…

    Java 2023年6月15日
    072
  • SpringBoot集成消息队列

    背景 最近在对公司开发框架进行优化,框架内涉及到多处入库的日志记录,例如登录日志/操作日志/访问日志/业务执行日志,集成在业务代码中耦合度较高且占用业务操作执行时间,所以准备集成相…

    Java 2023年6月15日
    067
  • LeetCode.1170-比较字符串中最小字符的出现频率(Compare Strings by Frequency of the Smallest Char)

    这是小川的第 412次更新,第 444篇原创 看题和准备 今天介绍的是 LeetCode算法题中 Easy级别的第 263题(顺位题号是 1170)。在一个非空字符串s上定义一个函…

    Java 2023年6月5日
    067
  • Win10命令行快速安装JDK环境

    主要内容 用scoop包管理自动下载安装jdk,自动配置环境,一键安装(把里面命令跑一遍就行了) 需要工具 Powershell(自带可) https://docs.microso…

    Java 2023年5月30日
    064
  • 5个必知的高级SQL函数

    5个必知的高级SQL函数 SQL是关系数据库管理的标准语言,用于与数据库通信。它广泛用于存储、检索和操作数据库中存储的数据。SQL不区分大小写。用户可以访问存储在关系数据库管理系统…

    Java 2023年6月15日
    0117
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球