Java学习路线-59:MyBatis数据持久层框架

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: Java学习路线-59:MyBatis数据持久层框架

MyBatis 第一个程序

MyBatis 是基于 Java 的数据持久层框架


持久化:数据从瞬时状态变为持久状态

持久层:完成持久化工作的代码块 DAO


简而言之:


MyBatis 将数据存入数据库中,从数据库中取数据


通过框架可以减少重复代码,提高开发效率


MyBatis 是一个半自动化的 ORM 框架

Object Relationship Mapping


文档:

https://mybatis.org/mybatis-3/zh/index.html


1、依赖



<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.4</version>
</dependency>

2、配置数据库

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/data"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="StudentMapper.xml"/>
    </mappers>
</configuration>

3、SQL 工厂类

package com.pengshiyu.mybatis.util;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MyBatisUtil {
    public static SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        return sqlSessionFactory;
    }
    public static SqlSession getSqlSession() throws IOException {
        SqlSession session = getSqlSessionFactory().openSession();
        return session;
    }
}

4、创建实体类

package com.pengshiyu.mybatis.entity;
public class Student {
    private int id;
    private String name;
    private int age;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

5、编写 SQL 语句映射文件

StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.pengshiyu.mybatis.entity.StudentMapper">
    <select id="selectStudent" resultType="com.pengshiyu.mybatis.entity.Student">
        select * from students where id = #{id}
    </select>
</mapper>

6、测试

package com.pengshiyu.mybatis.test;
import com.pengshiyu.mybatis.entity.Student;
import com.pengshiyu.mybatis.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
public class Demo {
    public static void main(String[] args) throws IOException {
        SqlSession session = MyBatisUtil.getSqlSession();
        Student student = session.selectOne("com.pengshiyu.mybatis.entity.StudentMapper.selectStudent", 3);
        System.out.println(student);
        session.close();
    //    Student{id=3, name='李白', age=30}
    }
}

curd 操作

StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.pengshiyu.mybatis.entity.StudentMapper">
    <select id="selectStudent" resultType="com.pengshiyu.mybatis.entity.Student">
        select * from students where id = #{id}
    </select>
    <select id="selectAllStudent" resultType="com.pengshiyu.mybatis.entity.Student">
        select * from students
    </select>
    <insert id="insertStudent" parameterType="com.pengshiyu.mybatis.entity.Student">
        insert into students(name, age) values(#{name}, #{age})
    </insert>
    <update id="updateStudent" parameterType="com.pengshiyu.mybatis.entity.Student">
        update students set name = #{name}, age = #{age} where id = #{id}
    </update>
    <delete id="deleteStudent">
        delete from students where id = #{id}
    </delete>
</mapper>
package com.pengshiyu.mybatis.dao;
import com.pengshiyu.mybatis.entity.Student;
import com.pengshiyu.mybatis.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
import java.util.List;
public class StudentDao {
    public Student select(int id) throws IOException {
        SqlSession session = MyBatisUtil.getSqlSession();
        Student student = session.selectOne("com.pengshiyu.mybatis.entity.StudentMapper.selectStudent", id);
        session.close();
        return student;
    }
    public List<Student> selectAll() throws IOException {
        SqlSession session = MyBatisUtil.getSqlSession();
        List<Student> students = session.selectList("com.pengshiyu.mybatis.entity.StudentMapper.selectAllStudent");
        session.close();
        return students;
    }
    public int insert(Student student) throws IOException {
        SqlSession session = MyBatisUtil.getSqlSession();
        int result = session.insert("com.pengshiyu.mybatis.entity.StudentMapper.insertStudent", student);
        session.commit();
        session.close();
        return result;
    }
    public int update(Student student) throws IOException {
        SqlSession session = MyBatisUtil.getSqlSession();
        int result = session.update("com.pengshiyu.mybatis.entity.StudentMapper.updateStudent", student);
        session.commit();
        session.close();
        return result;
    }
    public int delete(int id) throws IOException {
        SqlSession session = MyBatisUtil.getSqlSession();
        int result = session.delete("com.pengshiyu.mybatis.entity.StudentMapper.deleteStudent", id);
        session.commit();
        session.close();
        return result;
    }
}
package com.pengshiyu.mybatis.test;
import com.pengshiyu.mybatis.dao.StudentDao;
import com.pengshiyu.mybatis.entity.Student;
import java.io.IOException;
import java.util.List;
public class Demo {
    public static void main(String[] args) throws IOException {
        StudentDao studentDao = new StudentDao();
        // 查询
        Student student = studentDao.select(3);
        System.out.println(student);
        // Student{id=3, name='李白', age=30}
        // 写入
        Student student = new Student();
        student.setName("Jack");
        student.setAge(23);
        System.out.println(studentDao.inset(student));
        // 1
        // 更新
        Student student = studentDao.select(16);
        student.setAge(33);
        student.setName("Tom");
        System.out.println(studentDao.update(student));
        // 1
        // 删除数据
        System.out.println(studentDao.delete(12));
        // 1
        // 查询多条数据
        List<Student> students = studentDao.selectAll();
        for(Student student: students){
            System.out.println(student);
        }
    }
}

配置文件解析

配置文件

每个数据库对应一个 SqlSessionFactory 实例

dataSource:

  1. UNPOOLED 每次请求时打开和关闭连接
  2. POOLED 使用连接池
  3. JNDI 能在如 EJB 或应用服务器这类容器中使用


mapper 文件

namespace 命名规则:

包名+类名/包名+mapper 文件名

  1. parameterType 参数类型
  2. resultType 返回结果类型
  3. useGeneratedKeys=“true” 使用自增主键

配置优化

执行流程

  1. 读取核心配置文件
  2. sqlSessionFactory 类
  3. sqlSession
  4. 执行相关操作


1、可以将数据库配置单独放在一个文件里边

db.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/data
username=root
password=123456
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 加载数据库配置 -->
    <properties resource="db.properties" />
    <settings>
        <!-- 打印sql日志 -->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="StudentMapper.xml"/>
    </mappers>
</configuration>

2、别名配置

<configuration>
    <typeAliases>
        <!-- 指定单个类的别名 -->
        <typeAlias type="com.pengshiyu.mybatis.entity.Student" alias="Student"/>
        <!-- 指定整个包下的类都是别名 -->
        <package name="com.pengshiyu.mybatis.entity"/>
    </typeAliases>
</configuration>

使用别名

<mapper namespace="com.pengshiyu.mybatis.entity.StudentMapper">
    <select id="selectAllStudent" resultType="Student">
        select * from students
    </select>
</mapper>

属性名和列名不一致

MyBatis 会根据列名取赋值,会将列名转为小写

1、为列名指定别名

<mapper namespace="com.pengshiyu.mybatis.entity.StudentMapper">
    <select id="selectStudent" resultType="Student">
        select id, name, age as old from students where id = #{id}
    </select>
</mapper>

2、使用结果映射类型

<mapper namespace="com.pengshiyu.mybatis.entity.StudentMapper">
    <select id="selectStudent" resultMap="StudentMap">
        select id, name, age from students where id = #{id}
    </select>
    <resultMap id="StudentMap" type="Student">
        <!-- id为主键 -->
        <id column="id" property="id" />
        <!-- column是数据库表的列名,property是实体类属性名 -->
        <result column="name" property="name"/>
        <result column="age" property="age"/>
    </resultMap>
</mapper>

分页的实现

1、sql 中实现

如果将数据看做下标从 0 开始,那么就是数据切片 [startIndex, pageSize)

<mapper namespace="com.pengshiyu.mybatis.entity.StudentMapper">
    <select id="selectAllStudent" parameterType="Map" resultType="Student">
        select * from students limit #{offset}, #{limit}
    </select>
</mapper>
public class StudentDao {
    public List<Student> selectAll(int currentPage, int pageSize) throws IOException {
        SqlSession session = MyBatisUtil.getSqlSession();
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("offset", (currentPage - 1) * pageSize);
        map.put("limit", pageSize);
        List<Student> students = session.selectList(
            "com.pengshiyu.mybatis.entity.StudentMapper.selectAllStudent", map);
        session.close();
        return students;
    }
}
public class Demo {
    public static void main(String[] args) throws IOException {
        StudentDao studentDao = new StudentDao();
        // 查询第二页的数据,每页2条
        List<Student> students = studentDao.selectAll(2 , 2);
        for(Student student: students){
            System.out.println(student);
        }
    }
}

2、使用 RowBounds

<mapper namespace="com.pengshiyu.mybatis.entity.StudentMapper">
    <select id="selectAllStudent"  resultType="Student">
        select * from students
    </select>
</mapper>
import org.apache.ibatis.session.RowBounds;
public class StudentDao {
    public List<Student> selectAll(int currentPage, int pageSize) throws IOException {
        SqlSession session = MyBatisUtil.getSqlSession();
        RowBounds rowBounds = new RowBounds((currentPage - 1) * pageSize, pageSize);
        List<Student> students = session.selectList(
            "com.pengshiyu.mybatis.entity.StudentMapper.selectAllStudent",
            null, rowBounds);
        session.close();
        return students;
    }
}

通过打印的日志发现:

  1. sql 限制起始位置和返回数量,currentPage=2, pageSize=2 时返回 2 条数据
  2. RowBounds 不限制起始位置,currentPage=2, pageSize=2 时返回 4 条数据

注解开发

面向接口编程

扩展性好,分层开发中,上层不用管具体实现,

大家都遵循共同的实现,开发变得容易,规范性更好

DAO 接口

package com.pengshiyu.mybatis.dao;
import com.pengshiyu.mybatis.entity.Student;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface IStudentDao {
    @Select("select * from students")
    public List<Student> getList();
}

修改配置文件

<configuration>
    <mappers>
<!--        <mapper resource="StudentMapper.xml"/>-->
        <mapper class="com.pengshiyu.mybatis.dao.IStudentDao"/>
    </mappers>
</configuration>

测试使用

package com.pengshiyu.mybatis.test;
import com.pengshiyu.mybatis.dao.IStudentDao;
import com.pengshiyu.mybatis.dao.StudentDao;
import com.pengshiyu.mybatis.entity.Student;
import com.pengshiyu.mybatis.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
import java.util.List;
public class Demo {
    public static void main(String[] args) throws IOException {
        SqlSession session =  MyBatisUtil.getSqlSession();
        IStudentDao studentDao = session.getMapper(IStudentDao.class);
        List<Student> students = studentDao.getList();
        for(Student student : students){
            System.out.println(student);
        }
    }
}

多对一的处理

多个学生 student 对一个老师 teacher

1、数据库表设计

create table teachers(
  id int PRIMARY key auto_increment,
  name varchar(10)
);
create table students(
  id int PRIMARY key auto_increment,
  name varchar(10),
  teacher_id int
);
insert into teachers(name) values("王老师");
insert into teachers(name) values("李老师");
insert into teachers(name) values("赵老师");
insert into students(name, teacher_id) values("宋江", 1);
insert into students(name, teacher_id) values("李逵", 1);
insert into students(name, teacher_id) values("鲁智深", 2);
insert into students(name, teacher_id) values("林冲", 3);
insert into students(name, teacher_id) values("高俅", 3);

、实体类

Teacher

package com.pengshiyu.mybatis.entity;
public class Teacher {
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Teacher{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

Student

package com.pengshiyu.mybatis.entity;
public class Student {
    private int id;
    private String name;
    private Teacher teacher;
    public Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", teacher=" + teacher +
                '}';
    }
}

3、映射文件

多对一处理方式:

(1)按结果嵌套

查询一次

<mapper namespace="com.pengshiyu.mybatis.entity.StudentMapper">
    <select id="selectAllStudent" resultMap="StudentMap">
        select s.id sid, s.name sname, t.id tid, t.name tname
        from students as s
        left join teachers as t
        on s.teacher_id = t.id
    </select>
    <resultMap id="StudentMap" type="Student">
        <!-- 主键 -->
        <id column="sid" property="id"/>
        <result column="sname" property="name"/>
        <!-- 关联对象-->
        <association property="teacher" javaType="Teacher">
            <id column="tid" property="id"/>
            <result column="tname" property="name"/>
        </association>
    </resultMap>
</mapper>
  1. 按查询嵌套

会查询 n 次,n 是 Student 数量

<mapper namespace="com.pengshiyu.mybatis.entity.StudentMapper">
    <select id="selectAllStudent" resultMap="StudentTeacher">
        select * from students
    </select>
    <resultMap id="StudentTeacher" type="Student">
        <!-- 关联对象-->
        <association  property="teacher"  column="teacher_id"
        javaType="Teacher" select="getTeacher">
            <id column="tid" property="id"/>
            <result column="tname" property="name"/>
        </association>
    </resultMap>
    <select id="getTeacher" resultType="Teacher">
        select * from teachers where id = #{id}
    </select>
</mapper>

4、引入映射文件

mybatis-config.xml

<configuration>
    <typeAliases>
        <package name="com.pengshiyu.mybatis.entity"/>
    </typeAliases>
    <mappers>
        <mapper resource="StudentMapper.xml"/>
    </mappers>
</configuration>

5、Dao 编写

package com.pengshiyu.mybatis.dao;
import com.pengshiyu.mybatis.entity.Student;
import com.pengshiyu.mybatis.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
import java.util.List;
public class StudentDao {
    public List<Student> selectAll() throws IOException {
        SqlSession session = MyBatisUtil.getSqlSession();
        List<Student> students = session.selectList(
                "com.pengshiyu.mybatis.entity.StudentMapper.selectAllStudent");
        session.close();
        return students;
    }
}

6、测试类

package com.pengshiyu.mybatis.test;
import com.pengshiyu.mybatis.dao.StudentDao;
import com.pengshiyu.mybatis.entity.Student;
import java.io.IOException;
import java.util.List;
public class Demo {
    public static void main(String[] args) throws IOException {
        StudentDao studentDao = new StudentDao();
        List<Student> students = studentDao.selectAll();
        for(Student student: students){
            System.out.println(student);
        }
    }
}

查询结果

Student{id=1, name='宋江', teacher=Teacher{id=1, name='王老师'}}
Student{id=2, name='李逵', teacher=Teacher{id=1, name='王老师'}}
Student{id=3, name='鲁智深', teacher=Teacher{id=2, name='李老师'}}
Student{id=4, name='林冲', teacher=Teacher{id=3, name='赵老师'}}
Student{id=5, name='高俅', teacher=Teacher{id=3, name='赵老师'}}

一对多关系

Teacher

package com.pengshiyu.mybatis.entity;
import java.util.List;
public class Teacher {
    private int id;
    private String name;
    private List<Student> students;
    public List<Student> getStudents() {
        return students;
    }
    public void setStudents(List<Student> students) {
        this.students = students;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Teacher{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", students=" + students +
                '}';
    }
}

TeacherMapper

查询一次

<mapper namespace="com.pengshiyu.mybatis.entity.TeacherMapper">
    <select id="selectOneTeacher" resultMap="TeacherStudent">
        select t.id tid, t.name tname, s.id sid, s.name sname
        from teachers t left join students s
        on t.id = s.teacher_id
        where t.id = #{id}
    </select>
    <resultMap id="TeacherStudent" type="Teacher">
        <id column="tid" property="id"/>
        <result column="tname" property="name"/>
        <!-- 关联集合 -->
        <collection property="students" ofType="Student">
            <id column="sid" property="id"/>
            <result column="sname" property="name"/>
        </collection>
    </resultMap>
</mapper>

查询两次

<mapper namespace="com.pengshiyu.mybatis.entity.TeacherMapper">
    <select id="selectOneTeacher" resultMap="TeacherStudent">
        select *
        from teachers
        where id = #{id}
    </select>
    <resultMap id="TeacherStudent" type="Teacher">
        <!-- 关联集合 -->
        <!-- column 是外键 -->
        <collection property="students" column="id" ofType="Student"
                    select="getStudentByTeacherId">
        </collection>
    </resultMap>
    <select id="getStudentByTeacherId" resultType="Student">
        select * from students where teacher_id = #{id}
    </select>
</mapper>

mybatis-config.xml

<configuration>
    <typeAliases>
        <package name="com.pengshiyu.mybatis.entity"/>
    </typeAliases>
    <mappers>
        <mapper resource="TeacherMapper.xml"/>
    </mappers>
</configuration>

TeacherDao

package com.pengshiyu.mybatis.dao;
import com.pengshiyu.mybatis.entity.Student;
import com.pengshiyu.mybatis.entity.Teacher;
import com.pengshiyu.mybatis.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
import java.util.List;
public class TeacherDao {
    public Teacher selectOne(int id) throws IOException {
        SqlSession session = MyBatisUtil.getSqlSession();
        Teacher teacher = session.selectOne(
                "com.pengshiyu.mybatis.entity.TeacherMapper.selectOneTeacher", id);
        session.close();
        return teacher;
    }
}
package com.pengshiyu.mybatis.test;
import com.pengshiyu.mybatis.dao.StudentDao;
import com.pengshiyu.mybatis.dao.TeacherDao;
import com.pengshiyu.mybatis.entity.Student;
import com.pengshiyu.mybatis.entity.Teacher;
import java.io.IOException;
import java.util.List;
public class Demo {
    public static void main(String[] args) throws IOException {
        TeacherDao teacherDao = new TeacherDao();
        Teacher teacher = teacherDao.selectOne(1);
        System.out.println(teacher);
        for(Student student: teacher.getStudents()){
            System.out.println(student);
        }
    }
}

输出

Teacher{id=1, name='王老师', students=[
    Student{id=1, name='宋江', teacher=null},
    Student{id=2, name='李逵', teacher=null}
    ]
}
Student{id=1, name='宋江', teacher=null}
Student{id=2, name='李逵', teacher=null}

动态 SQL

根据不同的查询条件,生成不同的 sql

<mapper namespace="com.pengshiyu.mybatis.entity.StudentMapper">
    <select id="selectAllStudent" resultType="Student">
        select * from students
        <where>
            <if test="name != null">
                name = #{name}
            </if>
        </where>
    </select>
</mapper>

sql:

select * from students WHERE name = ?
package com.pengshiyu.mybatis.dao;
import com.pengshiyu.mybatis.entity.Student;
import com.pengshiyu.mybatis.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
pblic class StudentDao {
    public List<Student> selectAllStudent(String name) throws IOException {
        SqlSession session = MyBatisUtil.getSqlSession();
        Map<String, String> map = new HashMap<>();
        map.put("name", name);
        List<Student> students = session.selectList(
                "com.pengshiyu.mybatis.entity.StudentMapper.selectAllStudent",
                map
        );
        session.close();
        return students;
    }
}
package com.pengshiyu.mybatis.test;
import com.pengshiyu.mybatis.dao.StudentDao;
import com.pengshiyu.mybatis.entity.Student;
import java.io.IOException;
import java.util.List;
public class Demo {
    public static void main(String[] args) throws IOException {
        StudentDao studentDao = new StudentDao();
        List<Student> students = studentDao.selectAllStudent("宋江");
        for(Student student: students){
            System.out.println(student);
        }
    }
}
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
15天前
|
前端开发 JavaScript Java
java常用数据判空、比较和类型转换
本文介绍了Java开发中常见的数据处理技巧,包括数据判空、数据比较和类型转换。详细讲解了字符串、Integer、对象、List、Map、Set及数组的判空方法,推荐使用工具类如StringUtils、Objects等。同时,讨论了基本数据类型与引用数据类型的比较方法,以及自动类型转换和强制类型转换的规则。最后,提供了数值类型与字符串互相转换的具体示例。
|
2月前
|
算法 Java 数据处理
从HashSet到TreeSet,Java集合框架中的Set接口及其实现类以其“不重复性”要求,彻底改变了处理唯一性数据的方式。
从HashSet到TreeSet,Java集合框架中的Set接口及其实现类以其“不重复性”要求,彻底改变了处理唯一性数据的方式。HashSet基于哈希表实现,提供高效的元素操作;TreeSet则通过红黑树实现元素的自然排序,适合需要有序访问的场景。本文通过示例代码详细介绍了两者的特性和应用场景。
50 6
|
2月前
|
存储 Java API
深入剖析Java Map:不只是存储数据,更是设计艺术的体现!
【10月更文挑战第17天】在Java编程中,Map是一种重要的数据结构,用于存储键值对,并展现了设计艺术的精髓。本文深入剖析了Map的设计原理和使用技巧,包括基本概念、设计艺术(如哈希表与红黑树的空间时间权衡)、以及使用技巧(如选择合适的实现类、避免空指针异常等),帮助读者更好地理解和应用Map。
108 3
|
22天前
|
JSON Java 程序员
Java|如何用一个统一结构接收成员名称不固定的数据
本文介绍了一种 Java 中如何用一个统一结构接收成员名称不固定的数据的方法。
24 3
|
1月前
|
Java 程序员 容器
Java中的变量和常量:数据的‘小盒子’和‘铁盒子’有啥不一样?
在Java中,变量是一个可以随时改变的数据容器,类似于一个可以反复打开的小盒子。定义变量时需指定数据类型和名称。例如:`int age = 25;` 表示定义一个整数类型的变量 `age`,初始值为25。 常量则是不可改变的数据容器,类似于一个锁死的铁盒子,定义时使用 `final` 关键字。例如:`final int MAX_SPEED = 120;` 表示定义一个名为 `MAX_SPEED` 的常量,值为120,且不能修改。 变量和常量的主要区别在于变量的数据可以随时修改,而常量的数据一旦确定就不能改变。常量主要用于防止意外修改、提高代码可读性和便于维护。
|
1月前
|
存储 缓存 安全
在 Java 编程中,创建临时文件用于存储临时数据或进行临时操作非常常见
在 Java 编程中,创建临时文件用于存储临时数据或进行临时操作非常常见。本文介绍了使用 `File.createTempFile` 方法和自定义创建临时文件的两种方式,详细探讨了它们的使用场景和注意事项,包括数据缓存、文件上传下载和日志记录等。强调了清理临时文件、确保文件名唯一性和合理设置文件权限的重要性。
70 2
|
1月前
|
Java
Java 8 引入的 Streams 功能强大,提供了一种简洁高效的处理数据集合的方式
Java 8 引入的 Streams 功能强大,提供了一种简洁高效的处理数据集合的方式。本文介绍了 Streams 的基本概念和使用方法,包括创建 Streams、中间操作和终端操作,并通过多个案例详细解析了过滤、映射、归并、排序、分组和并行处理等操作,帮助读者更好地理解和掌握这一重要特性。
30 2
|
2月前
|
存储 SQL 小程序
JVM知识体系学习五:Java Runtime Data Area and JVM Instruction (java运行时数据区域和java指令(大约200多条,这里就将一些简单的指令和学习))
这篇文章详细介绍了Java虚拟机(JVM)的运行时数据区域和JVM指令集,包括程序计数器、虚拟机栈、本地方法栈、直接内存、方法区和堆,以及栈帧的组成部分和执行流程。
39 2
JVM知识体系学习五:Java Runtime Data Area and JVM Instruction (java运行时数据区域和java指令(大约200多条,这里就将一些简单的指令和学习))
|
1月前
|
存储 分布式计算 Java
存算分离与计算向数据移动:深度解析与Java实现
【11月更文挑战第10天】随着大数据时代的到来,数据量的激增给传统的数据处理架构带来了巨大的挑战。传统的“存算一体”架构,即计算资源与存储资源紧密耦合,在处理海量数据时逐渐显露出其局限性。为了应对这些挑战,存算分离(Disaggregated Storage and Compute Architecture)和计算向数据移动(Compute Moves to Data)两种架构应运而生,成为大数据处理领域的热门技术。
57 2
|
1月前
|
SQL Java OLAP
java实现“数据平滑升级”
java实现“数据平滑升级”
44 2
下一篇
DataWorks