一篇文章带你掌握主流数据库框架——MyBatis

MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。

在之前的文章中我们学习了MYSQL和JDBC,但是这些东西远远不足以支持我们书写JavaWeb相关的内容

在学习MyBatis前,请先学习Java,Mysql,JDBC,Maven内容

MyBatis学前须知

首先我们先简单介绍一下MyBatis:

  • Mybats是一款优秀的持久层框架,用于简化JDBC开发
  • MyBatis本是Apache的一个开源项目iBatis,2010年由apache software foundation 迁移到了google code,并改名为MyBatis
  • 官网:MyBatis中文网

我们再来介绍一下JavaEE概念:

  • JavaEE三层架构:表现层,业务层,持久层
  • 表现层:负责接收客户端请求,向客户端响应结果
  • 业务层:它负责业务逻辑处理
  • 持久层:负责将数据保存到数据库的那一层代码

最后我们了解一下框架:

  • 框架是一个半成品软件,是一套可重用的,通用的软件基础代码模型
  • 在框架的基础上构建软件编写更加有效,规范,通用,可扩展

MyBatis产生背景

我们在前面提到MyBatis的产生是为了简化JDBC开发

那么我们就先来介绍一下JDBC的缺点:

我们通过一段代码进行解析:

  1. package com.itheima.jdbc;
  2. import org.junit.Test;
  3. import java.sql.*;
  4. import java.util.Date;
  5. @Test
  6. public void testPreparedStatement2() throws Exception {
  7. // 前置链接操作
  8. String url = "jdbc:mysql:///db1?useSSL=false&useServerPrepStmts=true";
  9. String username = "root";
  10. String password = "1234";
  11. Connection conn = DriverManager.getConnection(url, username, password);
  12. // 接收用户输入 用户名和密码
  13. String name = "zhangsan";
  14. String pwd = "' or '1' = '1";
  15. // 定义sql
  16. String sql = "select * from tb_user where username = ? and password = ?";
  17. // 获取pstmt对象
  18. PreparedStatement pstmt = conn.prepareStatement(sql);
  19. Thread.sleep(10000);
  20. // 设置?的值
  21. pstmt.setString(1,name);
  22. pstmt.setString(2,pwd);
  23. ResultSet rs = null;
  24. // 执行sql
  25. rs = pstmt.executeQuery();
  26. // 设置?的值
  27. pstmt.setString(1,"aaa");
  28. pstmt.setString(2,"bbb");
  29. // 执行sql
  30. rs = pstmt.executeQuery();
  31. // 判断登录是否成功
  32. if(rs.next()){
  33. System.out.println("登录成功~");
  34. }else{
  35. System.out.println("登录失败~");
  36. }
  37. //7. 释放资源
  38. rs.close();
  39. pstmt.close();
  40. conn.close();
  41. }
  42. }

在上述代码中,我们可以注意到:

  1. /*
  2. 硬编码概念: 代码展现形式固定,如果有所更改需要手动修改代码
  3. 硬编码动作:注册驱动,获得链接,SQL语句
  4. */
  5. // 前置链接操作
  6. String url = "jdbc:mysql:///db1?useSSL=false&useServerPrepStmts=true";
  7. String username = "root";
  8. String password = "1234";
  9. Connection conn = DriverManager.getConnection(url, username, password);
  10. // 接收用户输入 用户名和密码
  11. String name = "zhangsan";
  12. String pwd = "' or '1' = '1";
  13. /*
  14. 操作繁琐:大量代码堆叠
  15. 操作繁琐动作:手动设置参数,手动封装结果
  16. */
  17. // 获取pstmt对象
  18. PreparedStatement pstmt = conn.prepareStatement(sql);
  19. Thread.sleep(10000);
  20. // 设置?的值
  21. pstmt.setString(1,name);
  22. pstmt.setString(2,pwd);
  23. ResultSet rs = null;
  24. // 执行sql
  25. rs = pstmt.executeQuery();
  26. // 设置?的值
  27. pstmt.setString(1,"aaa");
  28. pstmt.setString(2,"bbb");
  29. // 执行sql
  30. rs = pstmt.executeQuery();

因而为了解决JDBC的这些缺点,MyBatis应运而生!

MyBatis解决思想:

  • 硬编码 -> 配置文件

    • 注册驱动,获得连接:在mybatis-config.xml设置其连接池信息
    • SQL语句:设置专门的Mapper接口和Mapper.xml设置其信息
  • 操作繁琐 - > 自动完成
    • 采用SqlSession对象的各类方法直接封装

MyBatis免除了几乎所有的JDBC代码以及设置参数和获得结果集的工作

MyBatis快速入门

我们将以一个案例进行MyBatis的快速入门(资源来自B站黑马程序员老陈的JavaWeb课程)

我们以步骤形式逐步进行:

  1. 准备数据库数据

  1. 创建模块,导入坐标(这里采用Maven创建项目,在项目自动生成的pom.xml中导入模块坐标即可)
  1. <!--
  2. 我们使用mybatis,导入mybatis坐标
  3. 我们使用mysql,导入mysql坐标
  4. 我们需要进行单元测试,导入junit坐标
  5. 我们需要日志,查看错误信息,导入日志坐标(注意:需要导入logback.xml包,可在网络查询)
  6. ->
  7. <dependencies>
  8. <!--mybatis 依赖-->
  9. <dependency>
  10. <groupId>org.mybatis</groupId>
  11. <artifactId>mybatis</artifactId>
  12. <version>3.5.5</version>
  13. </dependency>
  14. <!--mysql 驱动-->
  15. <dependency>
  16. <groupId>mysql</groupId>
  17. <artifactId>mysql-connector-java</artifactId>
  18. <version>5.1.46</version>
  19. </dependency>
  20. <!--junit 单元测试-->
  21. <dependency>
  22. <groupId>junit</groupId>
  23. <artifactId>junit</artifactId>
  24. <version>4.13</version>
  25. <scope>test</scope>
  26. </dependency>
  27. <!-- 添加slf4j日志api -->
  28. <dependency>
  29. <groupId>org.slf4j</groupId>
  30. <artifactId>slf4j-api</artifactId>
  31. <version>1.7.20</version>
  32. </dependency>
  33. <!-- 添加logback-classic依赖 -->
  34. <dependency>
  35. <groupId>ch.qos.logback</groupId>
  36. <artifactId>logback-classic</artifactId>
  37. <version>1.2.3</version>
  38. </dependency>
  39. <!-- 添加logback-core依赖 -->
  40. <dependency>
  41. <groupId>ch.qos.logback</groupId>
  42. <artifactId>logback-core</artifactId>
  43. <version>1.2.3</version>
  44. </dependency>
  45. </dependencies>
  1. 编写MyBatis核心配置文件(替换连接信息,解决硬编码问题)
  1. <!--
  2. 创建mybatis-config.xml,
  3. 写入下列信息(MyBatis官网可查找)
  4. <?xml version="1.0" encoding="UTF-8" ?>
  5. <!DOCTYPE configuration
  6. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  7. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  8. <configuration>
  9. <environments default="development">
  10. <environment id="development">
  11. <transactionManager type="JDBC"/>
  12. <dataSource type="POOLED">
  13. <property name="driver" value="${driver}"/>
  14. <property name="url" value="${url}"/>
  15. <property name="username" value="${username}"/>
  16. <property name="password" value="${password}"/>
  17. </dataSource>
  18. </environment>
  19. </environments>
  20. <mappers>
  21. <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  22. </mappers>
  23. </configuration>
  24. 然后我们需要对上述信息进行修改,使其完成连接数据库的问题
  25. -->
  26. <?xml version="1.0" encoding="UTF-8" ?>
  27. <!DOCTYPE configuration
  28. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  29. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  30. <configuration>
  31. <environments default="development">
  32. <environment id="development">
  33. <transactionManager type="JDBC"/>
  34. <!--注意:这里dataSource表示数据库连接--->
  35. <dataSource type="POOLED">
  36. <!--
  37. 我们需要修改下述信息
  38. driver:固定com.mysql.jdbc.Driver
  39. url:jdbc:mysql:/// + 数据库名称 + ?useSSL=false
  40. username:数据库账号
  41. password:数据库密码
  42. -->
  43. <property name="driver" value="com.mysql.jdbc.Driver"/>
  44. <property name="url" value="jdbc:mysql:///mybatis?useSSL=false"/>
  45. <property name="username" value="root"/>
  46. <property name="password" value="123456"/>
  47. </dataSource>
  48. </environment>
  49. </environments>
  50. <!--这里mapper表示映射地址:我们所需要的Mapper.xml,在后续我们会在Mapper.xml中书写SQL语句-->
  51. <mappers>
  52. <mapper resource="com/itheima/mapper/UserMapper.xml"/>
  53. </mappers>
  54. </configuration>
  1. 创建SQL映射文件(统一管理sql语句,解决硬编码问题)
  1. <!--
  2. 同样自己创建Mapper.xml文档,这里注意在前面加上前缀,如果你是Usr用户的数据库操作,命名为UserMapper.xml便于区分
  3. 导入下述代码(同样,在Mybatis官网可以找到)
  4. <?xml version="1.0" encoding="UTF-8" ?>
  5. <!DOCTYPE mapper
  6. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  7. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  8. <mapper namespace="org.mybatis.example.BlogMapper">
  9. <select id="selectBlog" resultType="Blog">
  10. select * from Blog where id = #{id}
  11. </select>
  12. </mapper>
  13. 在创建完成后,不要忘记回到上一步,把mapper的resource地址改为该文档所在地址
  14. -->
  15. <?xml version="1.0" encoding="UTF-8" ?>
  16. <!DOCTYPE mapper
  17. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  18. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  19. <!--
  20. namespace:名称空间,在后续需要与Mapper接口名称一致保证互联(这里暂时设置为test)
  21. id:查找方法的唯一表示
  22. resultType:返回类型
  23. 在<select></select>中间书写语句即可
  24. 后续会继续介绍
  25. -->
  26. <mapper namespace="test">
  27. <select id="selectAll" resultType="User">
  28. select * from Blog where id = #{id}
  29. </select>
  30. </mapper>
  1. 1 定义POJO类(与数据库信息完全相同的类)
  1. // 注意:数据为private,书写get和set方法以及toString方法
  2. package com.itheima.pojo;
  3. // alt + 鼠标左键 整列编辑
  4. public class User {
  5. private Integer id;
  6. private String username;
  7. private String password;
  8. private String gender;
  9. private String addr;
  10. public Integer getId() {
  11. return id;
  12. }
  13. public void setId(Integer id) {
  14. this.id = id;
  15. }
  16. public String getUsername() {
  17. return username;
  18. }
  19. public void setUsername(String username) {
  20. this.username = username;
  21. }
  22. public String getPassword() {
  23. return password;
  24. }
  25. public void setPassword(String password) {
  26. this.password = password;
  27. }
  28. public String getGender() {
  29. return gender;
  30. }
  31. public void setGender(String gender) {
  32. this.gender = gender;
  33. }
  34. public String getAddr() {
  35. return addr;
  36. }
  37. public void setAddr(String addr) {
  38. this.addr = addr;
  39. }
  40. @Override
  41. public String toString() {
  42. return "User{" +
  43. "id=" + id +
  44. ", username='" + username + '\'' +
  45. ", password='" + password + '\'' +
  46. ", gender='" + gender + '\'' +
  47. ", addr='" + addr + '\'' +
  48. '}';
  49. }
  50. }
  1. 2 主代码展示
  1. // 创建主代码
  2. package com.itheima;
  3. import com.itheima.pojo.User;
  4. import org.apache.ibatis.io.Resources;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.util.List;
  11. /**
  12. * Mybatis 快速入门代码
  13. */
  14. public class MyBatisDemo {
  15. public static void main(String[] args) throws IOException {
  16. //1. 加载mybatis的核心配置文件,获取 SqlSessionFactory(官网下载)
  17. /*
  18. 下述为官网下载,我们只需要修改第一行的mybatis-config.xml地址即可
  19. String resource = "org/mybatis/example/mybatis-config.xml";
  20. InputStream inputStream = Resources.getResourceAsStream(resource);
  21. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  22. */
  23. String resource = "mybatis-config.xml";
  24. InputStream inputStream = Resources.getResourceAsStream(resource);
  25. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  26. //2. 获取SqlSession对象,用它来执行sql(sqlSessionFactory方法)
  27. SqlSession sqlSession = sqlSessionFactory.openSession();
  28. //3. 执行sql(这里的test.selectList是我们的UserMapper.xml中所设置的名称空间.id)
  29. List<User> users = sqlSession.selectList("test.selectAll");
  30. System.out.println(users);
  31. //4. 释放资源
  32. sqlSession.close();
  33. }
  34. }

至此,我们已经了解了MyBatis的整体创建流程

在下面我们会以上述入门为标准,进行各种流程上的简化开发

解决SQL语句警告问题(IDEA正式版)

我们在Mapper.xml中书写sql语句时,可能会出现sql表名显示红色(报错)现象

产生原因:IDEA和数据库没有建立连接,不识别表信息

解决方法:在IDEA中配置MYSQL数据库连接

解决优点:代码不再报错,显示所有SQL语句以及表列的补全信息

解决方法:

  1. 在Database中打开加号,逐步打开Data Source,MYSQL
  2. 打开页面后,填写USer,Password即可

Mapper代理开发

我们在入门代码中创建了Mapper.xml,并在其中书写代码

我们在主项目的代码中包含有以下这段:

  1. List<User> users = sqlSession.selectList("test.selectAll");

但test.selectAll属于硬编码阶段,且书写方式麻烦

因而产生了Mapper代理开发,同样我们采用案例的形式逐步书写:

  1. 定义与SQL映射文件同名的Mapper接口,并将该接口与SQL映射文件放置在同一目录级别下(IDEA2022版已解决这个问题)

  1. 设置SQL映射文件的namespace属性为Mapper接口全限定名(接口与xml文件产生连接)
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <!--
  6. namespace:名称空间
  7. -->
  8. <mapper namespace="com.itheima.mapper.UserMapper">
  9. <!--statement-->
  10. <select id="selectAll" resultType="user">
  11. select *
  12. from tb_user;
  13. </select>
  14. </mapper>
  1. 在Mapper接口中定义方法,方法名就是SQL映射文件中sql语句的id,并保持参数类型和返回值类型一致
  1. package com.itheima.mapper;
  2. import com.itheima.pojo.User;
  3. import org.apache.ibatis.annotations.Param;
  4. import org.apache.ibatis.annotations.Select;
  5. import java.util.Collection;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Set;
  9. public interface UserMapper {
  10. List<User> selectAll();
  11. }
  1. 主代码展示:
  1. package com.itheima;
  2. import com.itheima.mapper.UserMapper;
  3. import com.itheima.pojo.User;
  4. import org.apache.ibatis.io.Resources;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.util.List;
  11. /**
  12. * Mybatis 代理开发
  13. */
  14. public class MyBatisDemo2 {
  15. public static void main(String[] args) throws IOException {
  16. //1. 加载mybatis的核心配置文件,获取 SqlSessionFactory
  17. String resource = "mybatis-config.xml";
  18. InputStream inputStream = Resources.getResourceAsStream(resource);
  19. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  20. //2. 获取SqlSession对象,用它来执行sql
  21. SqlSession sqlSession = sqlSessionFactory.openSession();
  22. //3.1 获取UserMapper接口的代理对象
  23. //(采用sqlSession方法获得接口类产生对象,调用对象的方法[这里方法来自xml],并根据Mapper接口设置的返回参数)
  24. UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
  25. List<User> users = userMapper.selectAll();
  26. System.out.println(users);
  27. //4. 释放资源
  28. sqlSession.close();
  29. }
  30. }

Mapper补充内容

在mybatis-config.xml文件中我们设置过mapper内容:

  1. <mappers>
  2. <!--加载sql映射文件-->
  3. <mapper resource="com/itheima/mapper/UserMapper.xml">
  4. </mappers>

但随着sql映射文件增多,单个书写过于麻烦,所以开发出了package方法

  1. <mappers>
  2. <!--加载sql映射文件-->
  3. <!-- <mapper resource="com/itheima/mapper/UserMapper.xml"/>-->
  4. <!--Mapper代理方式:通过包扫描的方法,将包中所对应的mapper.xml映射过来-->
  5. <package name="com.itheima.mapper"/>
  6. </mappers>

MyBatis核心配置文件

Mybatis核心配置文件就是我们之前写入的mybatis-config.xml文件

现在我们对MyBatis的相关内容进行分析:

  1. 标签:

    • 类型别名可为 Java 类型设置一个缩写名字。
    • 它仅用于 XML 配置,意在降低冗余的全限定类名书写。
    • 我们可以在标签内书写package标签,并设置文件夹名称,则后续内容中不需要添加该文件夹名称
  2. 标签:
    • MyBatis 可以配置成适应多种环境,这种机制有助于将 SQL 映射应用于多种数据库之中
    • 通过default设置信息来更换数据库,default的值为下述environment的id值
  3. Mapper,dataSource以及内部信息均已介绍,这里不再介绍

下面给出代码展示:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <typeAliases>
  7. <package name="com.itheima.pojo"/>
  8. </typeAliases>
  9. <!--
  10. environments:配置数据库连接环境信息。可以配置多个environment,通过default属性切换不同的environment
  11. -->
  12. <environments default="development">
  13. <environment id="development">
  14. <transactionManager type="JDBC"/>
  15. <dataSource type="POOLED">
  16. <!--数据库连接信息-->
  17. <property name="driver" value="com.mysql.jdbc.Driver"/>
  18. <property name="url" value="jdbc:mysql:///mybatis?useSSL=false"/>
  19. <property name="username" value="root"/>
  20. <property name="password" value="123456"/>
  21. </dataSource>
  22. </environment>
  23. <environment id="test">
  24. <transactionManager type="JDBC"/>
  25. <dataSource type="POOLED">
  26. <!--数据库连接信息-->
  27. <property name="driver" value="com.mysql.jdbc.Driver"/>
  28. <property name="url" value="jdbc:mysql:///mybatis?useSSL=false"/>
  29. <property name="username" value="root"/>
  30. <property name="password" value="1234"/>
  31. </dataSource>
  32. </environment>
  33. </environments>
  34. <mappers>
  35. <!--加载sql映射文件-->
  36. <!-- <mapper resource="com/itheima/mapper/UserMapper.xml"/>-->
  37. <!--Mapper代理方式-->
  38. <package name="com.itheima.mapper"/>
  39. </mappers>
  40. </configuration>

MyBatis进阶内容(增删改查)

在下述内容中,我们会通过一个案例进行MyBatis的增删改查并且补充相应知识点

在开始前,我们同样准备环境:

  1. 数据库表tb_brand

  1. 实体类Brand
  1. package com.itheima.pojo;
  2. /**
  3. * 品牌
  4. *
  5. * alt + 鼠标左键:整列编辑
  6. *
  7. * 在实体类中,基本数据类型建议使用其对应的包装类型
  8. */
  9. public class Brand {
  10. // id 主键
  11. private Integer id;
  12. // 品牌名称
  13. private String brandName;
  14. // 企业名称
  15. private String companyName;
  16. // 排序字段
  17. private Integer ordered;
  18. // 描述信息
  19. private String description;
  20. // 状态:0:禁用 1:启用
  21. private Integer status;
  22. public Integer getId() {
  23. return id;
  24. }
  25. public void setId(Integer id) {
  26. this.id = id;
  27. }
  28. public String getBrandName() {
  29. return brandName;
  30. }
  31. public void setBrandName(String brandName) {
  32. this.brandName = brandName;
  33. }
  34. public String getCompanyName() {
  35. return companyName;
  36. }
  37. public void setCompanyName(String companyName) {
  38. this.companyName = companyName;
  39. }
  40. public Integer getOrdered() {
  41. return ordered;
  42. }
  43. public void setOrdered(Integer ordered) {
  44. this.ordered = ordered;
  45. }
  46. public String getDescription() {
  47. return description;
  48. }
  49. public void setDescription(String description) {
  50. this.description = description;
  51. }
  52. public Integer getStatus() {
  53. return status;
  54. }
  55. public void setStatus(Integer status) {
  56. this.status = status;
  57. }
  58. @Override
  59. public String toString() {
  60. return "Brand{" +
  61. "id=" + id +
  62. ", brandName='" + brandName + '\'' +
  63. ", companyName='" + companyName + '\'' +
  64. ", ordered=" + ordered +
  65. ", description='" + description + '\'' +
  66. ", status=" + status +
  67. '}';
  68. }
  69. }
  1. 测试用例(在test文件夹下的java文件下创建test即可)

  1. 安装MyBatisX插件(方便对应Mapper.xml和Mapper接口)

查询数据

我们将会介绍三种数据查询方法:

  • 查询所有数据
  • 根据ID查询单个数据
  • 根据条件查询数据

接下来我们逐一讲解:

查询所有数据

查询所有数据步骤:

  1. 编写接口方法:Mapper接口(参数:无 返回类型:List)
  1. package com.itheima.mapper;
  2. import com.itheima.pojo.Brand;
  3. import org.apache.ibatis.annotations.Param;
  4. import org.apache.ibatis.annotations.ResultMap;
  5. import org.apache.ibatis.annotations.Select;
  6. import java.util.List;
  7. import java.util.Map;
  8. public interface BrandMapper {
  9. /**
  10. * 查询所有
  11. */
  12. List<Brand> selectAll();
  13. }
  1. 编写SQL语句(在xml中编写)
  1. <select id="selectAll" resultType="brand">
  2. select *
  3. from tb_brand;
  4. </select>
  1. 执行方式
  1. @Test
  2. public void testSelectAll() throws IOException {
  3. //1. 获取SqlSessionFactory
  4. String resource = "mybatis-config.xml";
  5. InputStream inputStream = Resources.getResourceAsStream(resource);
  6. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  7. //2. 获取SqlSession对象
  8. SqlSession sqlSession = sqlSessionFactory.openSession();
  9. //3. 获取Mapper接口的代理对象
  10. BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
  11. //4. 执行方法
  12. List<Brand> brands = brandMapper.selectAll();
  13. System.out.println(brands);
  14. //5. 释放资源
  15. sqlSession.close();
  16. }

但是这种方法中,我们会发现brandName和companyName并没有显示

主要原因:

  • brandName在MYSQL中定义为brand_name;companyName在MYSQL中定义为company_name

解决方法有两种:

  • 给所有名称不同的属性添加别名,使其与MYSQL属性同名
  1. <mapper namespace="com.itheima.mapper.BrandMapper">
  2. <select id="selectAll" resultMap="brandResultMap">
  3. select id, brand_name as brandName, company_name as companyName, ordered, description, status
  4. from tb_brand;
  5. </select>
  6. </mapper>
  • 在xml中添加一段ResultMap属性(推荐!!!)
  1. <mapper namespace="com.itheima.mapper.BrandMapper">
  2. <!--
  3. 数据库表的字段名称 和 实体类的属性名称 不一样,则不能自动封装数据
  4. * 起别名:对不一样的列名起别名,让别名和实体类的属性名一样
  5. * 缺点:每次查询都要定义一次别名
  6. * sql片段
  7. * 缺点:不灵活
  8. * resultMap:
  9. 1. 定义<resultMap>标签
  10. 2. 在<select>标签中,使用resultMap属性替换 resultType属性
  11. -->
  12. <!--
  13. id:唯一标识
  14. type:映射的类型,支持别名
  15. -->
  16. <resultMap id="brandResultMap" type="brand">
  17. <!--
  18. id:完成主键字段的映射
  19. column:表的列名
  20. property:实体类的属性名
  21. result:完成一般字段的映射
  22. column:表的列名
  23. property:实体类的属性名
  24. -->
  25. <result column="brand_name" property="brandName"/>
  26. <result column="company_name" property="companyName"/>
  27. </resultMap>
  28. <select id="selectAll" resultMap="brandResultMap">
  29. select *
  30. from tb_brand;
  31. </select>
  32. <select id="selectAll" resultMap="brand">
  33. select *
  34. from tb_brand;
  35. </select>
  36. </mapper>

单个查询

单个查询步骤:

  1. 编写接口方法:Mapper接口(参数:id 返回类型:Brand)
  1. Brand selectById(int id);
  1. 编写SQL语句
  1. <select id="selectById" resultMap="brandResultMap">
  2. select *
  3. from tb_brand
  4. where id = #{id};
  5. </select>
  1. 执行方法,测试
  1. @Test
  2. public void testSelectById() throws IOException {
  3. //接收参数
  4. int id = 1;
  5. //1. 获取SqlSessionFactory
  6. String resource = "mybatis-config.xml";
  7. InputStream inputStream = Resources.getResourceAsStream(resource);
  8. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  9. //2. 获取SqlSession对象
  10. SqlSession sqlSession = sqlSessionFactory.openSession();
  11. //3. 获取Mapper接口的代理对象
  12. BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
  13. //4. 执行方法
  14. Brand brand = brandMapper.selectById(id);
  15. System.out.println(brand);
  16. //5. 释放资源
  17. sqlSession.close();
  18. }

知识点讲解:

  • {}:会将其替换为 ? 放置SQL注入

  • ${}:拼接sql,会存在SQL注入问题
  • 使用时机:
    • 参数传递:#{}
    • 表名或列名不固定的情况下:${}

条件查询

这里我们介绍多条件查询:

  1. 编写接口方法:Mapper接口(参数:所有查询条件 返回结果:List)
  1. /**
  2. * 条件查询
  3. * * 参数接收
  4. * 1. 散装参数:如果方法中有多个参数,需要使用@Param("SQL参数占位符名称")
  5. * 2. 对象参数:对象的属性名称要和参数占位符名称一致
  6. * 3. map集合参数
  7. *
  8. */
  9. List<Brand> selectByCondition(@Param("status") int status, @Param("companyName") String companyName, @Param("brandName") String brandName);
  10. List<Brand> selectByCondition(Brand brand);
  11. List<Brand> selectByCondition(Map map);
  1. 编写SQL语句:
  1. <!--
  2. 条件查询:
  3. 这里#{}中的元素和Param所书写的属性相同;
  4. 如果是Brand,则与Brand的属性相同;
  5. 如果是Map,则与Map集合的键相同即可
  6. -->
  7. <select id="selectByCondition" resultMap="brandResultMap">
  8. select *
  9. from tb_brand
  10. where status = #{status}
  11. and company_name like #{companyName}
  12. and brand_name like #{brandName}
  13. </select>
  1. 执行方法,测试:
  1. @Test
  2. public void testSelectByCondition() throws IOException {
  3. //接收参数
  4. int status = 1;
  5. String companyName = "华为";
  6. String brandName = "华为";
  7. // 处理参数
  8. companyName = "%" + companyName + "%";
  9. brandName = "%" + brandName + "%";
  10. //封装对象
  11. /* Brand brand = new Brand();
  12. brand.setStatus(status);
  13. brand.setCompanyName(companyName);
  14. brand.setBrandName(brandName);*/
  15. Map map = new HashMap();
  16. // map.put("status" , status);
  17. map.put("companyName", companyName);
  18. // map.put("brandName" , brandName);
  19. //1. 获取SqlSessionFactory
  20. String resource = "mybatis-config.xml";
  21. InputStream inputStream = Resources.getResourceAsStream(resource);
  22. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  23. //2. 获取SqlSession对象
  24. SqlSession sqlSession = sqlSessionFactory.openSession();
  25. //3. 获取Mapper接口的代理对象
  26. BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
  27. //4. 执行方法
  28. //List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
  29. // List<Brand> brands = brandMapper.selectByCondition(brand);
  30. List<Brand> brands = brandMapper.selectByCondition(map);
  31. System.out.println(brands);
  32. //5. 释放资源
  33. sqlSession.close();
  34. }

动态查询

动态查询概念:

  • 在所有的查询条件中,用户可能并不会全部查询,可能只有挑选条件查询
  • 这时如果以之前的代码,会导致导入值为null而导致查询错误

动态SQL语句:

  • if语句
  1. <!--
  2. if标签:整体框架
  3. test:判断条件
  4. <if test="title != null">
  5. sql语句内容
  6. </if>
  7. -->
  8. <select id="findActiveBlogWithTitleLike"
  9. resultType="Blog">
  10. SELECT * FROM BLOG
  11. WHERE state = ‘ACTIVE’
  12. <if test="title != null">
  13. AND title like #{title}
  14. </if>
  15. </select>
  • where语句
  1. <!--
  2. 动态条件查询
  3. * if: 条件判断
  4. * test:逻辑表达式
  5. * 问题:
  6. * 恒等式
  7. * <where> 替换 where 关键字
  8. -->
  9. <!--这里的where会根据实际情况,自行添加and或者删除and-->
  10. <select id="selectByCondition" resultMap="brandResultMap">
  11. select *
  12. from tb_brand
  13. /* where 1 = 1*/
  14. <where>
  15. <if test="status != null">
  16. and status = #{status}
  17. </if>
  18. <if test="companyName != null and companyName != '' ">
  19. and company_name like #{companyName}
  20. </if>
  21. <if test="brandName != null and brandName != '' ">
  22. and brand_name like #{brandName}
  23. </if>
  24. </where>
  • choose语句
  1. <!--
  2. <choose> <!--相当于switch-->
  3. <when test="status != null"> <!--相当于case-->
  4. -->
  5. <select id="findActiveBlogLike"
  6. resultType="Blog">
  7. SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  8. <choose>
  9. <when test="title != null">
  10. AND title like #{title}
  11. </when>
  12. <when test="author != null and author.name != null">
  13. AND author_name like #{author.name}
  14. </when>
  15. <otherwise>
  16. AND featured = 1
  17. </otherwise>
  18. </choose>
  19. </select>

我们根据两个案例来解释上述语法:

  • 多条件查询语句:
  1. 编写接口方法:Mapper接口(参数:Brand 返回类型:List)
  1. List<Brand> selectByCondition(Brand brand);
  1. 编写SQL语句:
  1. <!--
  2. 动态条件查询
  3. * if: 条件判断
  4. * test:逻辑表达式
  5. * 问题:
  6. * 恒等式
  7. * <where> 替换 where 关键字
  8. -->
  9. <select id="selectByCondition" resultMap="brandResultMap">
  10. select *
  11. from tb_brand
  12. /* where 1 = 1*/
  13. <!--这里使用where,防止无条件或者and位置错误-->
  14. <where>
  15. <if test="status != null">
  16. and status = #{status}
  17. </if>
  18. <if test="companyName != null and companyName != '' ">
  19. and company_name like #{companyName}
  20. </if>
  21. <if test="brandName != null and brandName != '' ">
  22. and brand_name like #{brandName}
  23. </if>
  24. </where>
  25. </select>
  1. 执行方法,测试:
  1. @Test
  2. public void testSelectByCondition() throws IOException {
  3. //接收参数
  4. int status = 1;
  5. String companyName = "华为";
  6. String brandName = "华为";
  7. // 处理参数
  8. companyName = "%" + companyName + "%";
  9. brandName = "%" + brandName + "%";
  10. //封装对象
  11. Brand brand = new Brand();
  12. brand.setStatus(status);
  13. brand.setCompanyName(companyName);
  14. brand.setBrandName(brandName);
  15. //1. 获取SqlSessionFactory
  16. String resource = "mybatis-config.xml";
  17. InputStream inputStream = Resources.getResourceAsStream(resource);
  18. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  19. //2. 获取SqlSession对象
  20. SqlSession sqlSession = sqlSessionFactory.openSession();
  21. //3. 获取Mapper接口的代理对象
  22. BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
  23. //4. 执行方法
  24. List<Brand> brands = brandMapper.selectByCondition(brand);
  25. System.out.println(brands);
  26. //5. 释放资源
  27. sqlSession.close();
  28. }
  • 单条件动态查询:
  1. 编写接口方法:Mapper接口(参数:Brand 返回类型:List)
  1. List<Brand> selectByConditionSingle(Brand brand);
  1. 编写SQL语句:
  1. <select id="selectByConditionSingle" resultMap="brandResultMap">
  2. select *
  3. from tb_brand
  4. <!--这里使用where防止无条件出现导致错误-->
  5. <where>
  6. <choose><!--相当于switch-->
  7. <when test="status != null"><!--相当于case-->
  8. status = #{status}
  9. </when>
  10. <when test="companyName != null and companyName != '' "><!--相当于case-->
  11. company_name like #{companyName}
  12. </when>
  13. <when test="brandName != null and brandName != ''"><!--相当于case-->
  14. brand_name like #{brandName}
  15. </when>
  16. </choose>
  17. </where>
  18. </select>
  1. 执行方法,测试:
  1. @Test
  2. public void testSelectByConditionSingle() throws IOException {
  3. //接收参数
  4. int status = 1;
  5. String companyName = "华为";
  6. String brandName = "华为";
  7. // 处理参数
  8. companyName = "%" + companyName + "%";
  9. brandName = "%" + brandName + "%";
  10. //封装对象
  11. Brand brand = new Brand();
  12. //brand.setStatus(status);
  13. brand.setCompanyName(companyName);
  14. //brand.setBrandName(brandName);
  15. //1. 获取SqlSessionFactory
  16. String resource = "mybatis-config.xml";
  17. InputStream inputStream = Resources.getResourceAsStream(resource);
  18. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  19. //2. 获取SqlSession对象
  20. SqlSession sqlSession = sqlSessionFactory.openSession();
  21. //3. 获取Mapper接口的代理对象
  22. BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
  23. //4. 执行方法
  24. List<Brand> brands = brandMapper.selectByConditionSingle(brand);
  25. System.out.println(brands);
  26. //5. 释放资源
  27. sqlSession.close();
  28. }

添加数据

在这小节我们会讲到添加功能并附带返回主键:

添加功能

添加功能步骤:

  1. 编写接口方法:Mapper接口(参数:Brand 返回类型:void )
  1. void add(Brand brand);
  1. 编写MYSQL语句:
  1. <!--
  2. useGeneratedKeys:对于支持自动生成记录主键的数据库,如:MySQL,SQL Server,此时设置useGeneratedKeys参数值为true,在执行添加记录之后可以获取到数据库自动生成的主键ID。
  3. keyProperty:后面跟数据库中自动增长的列名,这时该属性值就会反馈在Java代码中
  4. -->
  5. <insert id="add" useGeneratedKeys="true" keyProperty="id">
  6. insert into tb_brand (brand_name, company_name, ordered, description, status)
  7. values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
  8. </insert>
  1. 代码调试,测试:
  1. @Test
  2. public void testAdd() throws IOException {
  3. //接收参数
  4. int status = 1;
  5. String companyName = "波导手机";
  6. String brandName = "波导";
  7. String description = "手机中的战斗机";
  8. int ordered = 100;
  9. //封装对象
  10. Brand brand = new Brand();
  11. brand.setStatus(status);
  12. brand.setCompanyName(companyName);
  13. brand.setBrandName(brandName);
  14. brand.setDescription(description);
  15. brand.setOrdered(ordered);
  16. //1. 获取SqlSessionFactory
  17. String resource = "mybatis-config.xml";
  18. InputStream inputStream = Resources.getResourceAsStream(resource);
  19. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  20. //2. 获取SqlSession对象
  21. SqlSession sqlSession = sqlSessionFactory.openSession();
  22. // 这里openSession的参数未设置时为手动提交信息,设置true后为自动提交事务
  23. //SqlSession sqlSession = sqlSessionFactory.openSession(true);
  24. //3. 获取Mapper接口的代理对象
  25. BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
  26. //4. 执行方法(这里就可以对brand的id进行提取,并输出)
  27. brandMapper.add(brand);
  28. Integer id = brand.getId();
  29. System.out.println(id);
  30. // 执行add后并未立刻执行,我们需要提交事务才能反馈到数据库中
  31. sqlSession.commit();
  32. //5. 释放资源
  33. sqlSession.close();
  34. }

修改数据

该小节中我们会介绍两种修改方法:

  • 全字段修改
  • 单字段修改

我们会在下述内容中一一讲解:

全字段修改

  1. 编写接口方法:Mapper接口(参数:所有数据 返回类型:int(修改行)或void)
  1. int update(Brand brand);
  1. 编写MYSQL语句:
  1. <update id="update">
  2. update tb_brand
  3. set brand_name = #{brandName},
  4. company_name = #{companyName},
  5. ordered = #{ordered},
  6. description = #{description},
  7. status = #{status}
  8. where id = #{id};
  9. </update>
  1. 执行方法,测试:
  1. @Test
  2. public void testUpdate() throws IOException {
  3. //接收参数
  4. int status = 0;
  5. String companyName = "波导手机";
  6. String brandName = "波导";
  7. String description = "波导手机,手机中的战斗机";
  8. int ordered = 200;
  9. int id = 6;
  10. //封装对象
  11. Brand brand = new Brand();
  12. brand.setStatus(status);
  13. brand.setCompanyName(companyName);
  14. brand.setBrandName(brandName);
  15. brand.setDescription(description);
  16. brand.setOrdered(ordered);
  17. brand.setId(id);
  18. //1. 获取SqlSessionFactory
  19. String resource = "mybatis-config.xml";
  20. InputStream inputStream = Resources.getResourceAsStream(resource);
  21. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  22. //2. 获取SqlSession对象
  23. SqlSession sqlSession = sqlSessionFactory.openSession();
  24. //SqlSession sqlSession = sqlSessionFactory.openSession(true);
  25. //3. 获取Mapper接口的代理对象
  26. BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
  27. //4. 执行方法
  28. int count = brandMapper.update(brand);
  29. System.out.println(count);
  30. //提交事务
  31. sqlSession.commit();
  32. //5. 释放资源
  33. sqlSession.close();
  34. }

单字段修改

  1. 编写接口方法:Mapper接口(参数:所有数据 返回类型:int(修改行)或void)
  1. int update(Brand brand);
  1. 编写MYSQL语句:
  1. <update id="update">
  2. update tb_brand
  3. <set>
  4. <if test="brandName != null and brandName != ''">
  5. brand_name = #{brandName},
  6. </if>
  7. <if test="companyName != null and companyName != ''">
  8. company_name = #{companyName},
  9. </if>
  10. <if test="ordered != null">
  11. ordered = #{ordered},
  12. </if>
  13. <if test="description != null and description != ''">
  14. description = #{description},
  15. </if>
  16. <if test="status != null">
  17. status = #{status}
  18. </if>
  19. </set>
  20. where id = #{id};
  21. </update>
  1. 执行方法,测试:
  1. @Test
  2. public void testUpdate() throws IOException {
  3. //接收参数
  4. int status = 0;
  5. String companyName = "波导手机";
  6. String brandName = "波导";
  7. String description = "波导手机,手机中的战斗机";
  8. int ordered = 200;
  9. int id = 6;
  10. //封装对象
  11. Brand brand = new Brand();
  12. brand.setStatus(status);
  13. // brand.setCompanyName(companyName);
  14. // brand.setBrandName(brandName);
  15. // brand.setDescription(description);
  16. // brand.setOrdered(ordered);
  17. brand.setId(id);
  18. //1. 获取SqlSessionFactory
  19. String resource = "mybatis-config.xml";
  20. InputStream inputStream = Resources.getResourceAsStream(resource);
  21. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  22. //2. 获取SqlSession对象
  23. SqlSession sqlSession = sqlSessionFactory.openSession();
  24. //SqlSession sqlSession = sqlSessionFactory.openSession(true);
  25. //3. 获取Mapper接口的代理对象
  26. BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
  27. //4. 执行方法
  28. int count = brandMapper.update(brand);
  29. System.out.println(count);
  30. //提交事务
  31. sqlSession.commit();
  32. //5. 释放资源
  33. sqlSession.close();
  34. }

删除功能

这节介绍删除功能的两种形式:

  • 单个删除
  • 多个删除

我们将会在下面一一讲解:

单个删除

  1. 编写接口方法:Mapper接口(参数:id 返回类型:void)
  1. void deleteById(int id);
  1. 编写MYSQL:
  1. <delete id="deleteById">
  2. delete from tb_brand where id = #{id};
  3. </delete>
  1. 代码执行,测试:
  1. @Test
  2. public void testDeleteById() throws IOException {
  3. //接收参数
  4. int id = 6;
  5. //1. 获取SqlSessionFactory
  6. String resource = "mybatis-config.xml";
  7. InputStream inputStream = Resources.getResourceAsStream(resource);
  8. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  9. //2. 获取SqlSession对象
  10. SqlSession sqlSession = sqlSessionFactory.openSession();
  11. //SqlSession sqlSession = sqlSessionFactory.openSession(true);
  12. //3. 获取Mapper接口的代理对象
  13. BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
  14. //4. 执行方法
  15. brandMapper.deleteById(id);
  16. //提交事务
  17. sqlSession.commit();
  18. //5. 释放资源
  19. sqlSession.close();
  20. }

多个删除

  1. 编写接口方法:Mapper接口(参数:int[] id 返回类型:void)
  1. void deleteByIds(int[] ids);
  1. 编写MYSQL代码:
  1. <!--
  2. mybatis会将数组参数,封装为一个Map集合。
  3. * 默认:array = 数组
  4. * 使用@Param注解改变map集合的默认key的名称
  5. -->
  6. <!--
  7. foreach:类似于for强化语句
  8. collection:集合/数组
  9. item:数组中的单个值
  10. separator:分割符
  11. open:开始处添加符号
  12. close:结尾处添加符合
  13. -->
  14. <delete id="deleteByIds">
  15. delete from tb_brand where id
  16. in
  17. <!--这部分整合出来类似于:(id1,id2,id3....)-->
  18. <foreach collection="array" item="id" separator="," open="(" close=")">
  19. #{id}
  20. </foreach>
  21. ;
  22. </delete>
  1. 代码运行,测试:
  1. @Test
  2. public void testDeleteByIds() throws IOException {
  3. //接收参数
  4. int[] ids = {5,7,8};
  5. //1. 获取SqlSessionFactory
  6. String resource = "mybatis-config.xml";
  7. InputStream inputStream = Resources.getResourceAsStream(resource);
  8. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  9. //2. 获取SqlSession对象
  10. SqlSession sqlSession = sqlSessionFactory.openSession();
  11. //SqlSession sqlSession = sqlSessionFactory.openSession(true);
  12. //3. 获取Mapper接口的代理对象
  13. BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
  14. //4. 执行方法
  15. brandMapper.deleteByIds(ids);
  16. //提交事务
  17. sqlSession.commit();
  18. //5. 释放资源
  19. sqlSession.close();
  20. }

参数传递

MyBatis接口方法中可以接收各种各样的参数,MyBatis底层对这些参数有着不同的封装处理方法

我们在下面一一介绍:

多参数传递

在接口多参数传递中会把所有参数转移为Map集合

会转化为两种Map集合:

  • arg集合,下标从0开始: arg[0]

  • Param集合,下标从1开始: Param[1]

我们在xml中也可以直接使用arg或param来直接表示(但不推荐,因为不能直观得到信息)

我们所推荐的做法是使用@Param注解:

  • 在参数前加上注解@Param("")

  • 注意:这里使用Param注解实际上是对Param集合的键进行修改,也就是说你的Param集合将不再能够使用

单参数传递

常见的单参数传递分为六种:

  1. POJO类型: 直接使用, 属性名 和 参数占位符 名称一致即可

  2. Map集合: 直接使用,键名 和 参数占位符 名称一致即可

  3. Collection: 封装为Map集合

    • map.put("arg0",collection集合);
    • map.put("collection",collection集合);
  4. List: 封装为Map集合

    • map.put("arg0",List集合);
    • map.put("collection",List集合);
    • map.put("list",List集合);
  5. Array:封装为Map

    • map.put("arg0",数组);
    • map.put("array",数组);
  6. 其他类型:直接使用

注解开发

我们先来介绍注解开发的格式:

  • 注解开发写在Mapper接口的方法中
  1. // 相当于节省了xml的一步,直接在接口中定义方法
  2. @Select("select * from tb_user where id = #{id}")
  3. List<Brand> selectAll();

当然,注解的方法也分为四种:

  • @Select
  • @Insert
  • @Update
  • @Delete

注解的优缺点:

  • 优点 : 注解完成简单功能,方便快捷
  • 缺点 : 注解会导致Java代码繁琐,在接口中书写大量Java和MYSQL代码导致可读性变差

使用注解来映射简单语句会使代码显得更加简洁,但对于稍微复杂一点的语句,Java 注解不仅力不从心,还会让你本就复杂的 SQL 语句更加混乱不堪。 因此,如果你需要做一些很复杂的操作,最好用 XML 来映射语句。

选择何种方式来配置映射,以及认为是否应该要统一映射语句定义的形式,完全取决于你和你的团队。 换句话说,永远不要拘泥于一种方式,你可以很轻松的在基于注解和 XML 的语句映射方式间自由移植和切换。

结束语

好的,关于MyBatis的内容就介绍到这里,希望能为你带来帮助!

附录

该文章属于学习内容,具体参考B站黑马程序员陈老师的JavaWeb课程

这里附上链接:01-MyBatis简介_哔哩哔哩_bilibili

一篇文章带你掌握主流数据库框架——MyBatis的更多相关文章

  1. 一篇文章带你掌握主流基础框架——Spring

    一篇文章带你掌握主流基础框架--Spring 这篇文章中我们将会介绍Spring的框架以及本体内容,包括核心容器,注解开发,AOP以及事务等内容 那么简单说明一下Spring的必要性: Spring技 ...

  2. 一篇文章带你掌握主流办公框架——SpringBoot

    一篇文章带你掌握主流办公框架--SpringBoot 在之前的文章中我们已经学习了SSM的全部内容以及相关整合 SSM是Spring的产品,主要用来简化开发,但我们现在所介绍的这款框架--Spring ...

  3. 一篇文章带你掌握主流服务层框架——SpringMVC

    一篇文章带你掌握主流服务层框架--SpringMVC 在之前的文章中我们已经学习了Spring的基本内容,SpringMVC隶属于Spring的一部分内容 但由于SpringMVC完全针对于服务层使用 ...

  4. 一篇文章带你了解NoSql数据库——Redis简单入门

    一篇文章带你了解NoSql数据库--Redis简单入门 Redis是一个基于内存的key-value结构数据库 我们会利用其内存存储速度快,读写性能高的特点去完成企业中的一些热门数据的储存信息 在本篇 ...

  5. 一篇文章带你掌握MyBatis简化框架——MyBatisPlus

    一篇文章带你掌握MyBatis简化框架--MyBatisPlus 我们在前面的文章中已经学习了目前开发所需的主流框架 类似于我们所学习的SpringBoot框架用于简化Spring开发,我们的国人大大 ...

  6. 一篇文章带你了解网页框架——Vue简单入门

    一篇文章带你了解网页框架--Vue简单入门 这篇文章将会介绍我们前端入门级别的框架--Vue的简单使用 如果你以后想从事后端程序员,又想要稍微了解前端框架知识,那么这篇文章或许可以给你带来帮助 温馨提 ...

  7. MYSQL(基本篇)——一篇文章带你走进MYSQL的奇妙世界

    MYSQL(基本篇)--一篇文章带你走进MYSQL的奇妙世界 MYSQL算是我们程序员必不可少的一份求职工具了 无论在什么岗位,我们都可以看到应聘要求上所书写的"精通MYSQL等数据库及优化 ...

  8. 一篇文章带你了解服务器操作系统——Linux简单入门

    一篇文章带你了解服务器操作系统--Linux简单入门 Linux作为服务器的常用操作系统,身为工作人员自然是要有所了解的 在本篇中我们会简单介绍Linux的特点,安装,相关指令使用以及内部程序的安装等 ...

  9. MYSQL(进阶篇)——一篇文章带你深入掌握MYSQL

    MYSQL(进阶篇)--一篇文章带你深入掌握MYSQL 我们在上篇文章中已经学习了MYSQL的基本语法和概念 在这篇文章中我们将讲解底层结构和一些新的语法帮助你更好的运用MYSQL 温馨提醒:该文章大 ...

随机推荐

  1. UiPath文本操作Set Text的介绍和使用

    一.Set Text的介绍 向输入框/文本框写入文本的一种操作 二.Set Text在UiPath中的使用 1.打开设计器,在设计库中新建一个Sequence,为序列命名及设置Sequence存放的路 ...

  2. JQuery select与radio的取值与赋值

    radio 取:$("input[name='NAME']:checked").val(); 赋:$("input[name='NAME'][value='指定值']&q ...

  3. 7 个有趣的 Python 实战项目,超级适合练手

    关于Python,有一句名言:不要重复造轮子. 但是问题有三个: 1.你不知道已经有哪些轮子已经造好了,哪个适合你用.有名有姓的的著名轮子就400多个,更别说没名没姓自己在制造中的轮子. 2.确实没重 ...

  4. 解决github.com 的响应时间过长以及hosts配置不能保存的问题

    github.com 的响应时间过长 1 获取github可以使用的DNS域名 DNS查询 选择TTL值最小的 2 修改hosts配置 打开之后在最后加上如下内容,保存即可 3 出现hosts不能保存 ...

  5. 腾讯云原生数据库TDSQL-C入选信通院《云原生产品目录》

    近日,中国信通院.云计算开源产业联盟正式对外发布<云原生产品目录>,腾讯云原生数据库TDSQL-C凭借其超强性能.极致效率的弹性伸缩和完善的产品化解决方案体系,成功入围目录. 全球数字经济 ...

  6. go-zero微服务实战系列(十、分布式事务如何实现)

    在分布式应用场景中,分布式事务问题是不可回避的,在目前流行的微服务场景下更是如此.比如在我们的商城系统中,下单操作涉及创建订单和库存扣减操作两个操作,而订单服务和商品服务是两个独立的微服务,因为每个微 ...

  7. System.Web.Mvc 找到的程序集清单定义与程序集引用不匹配

    System.IO.FileLoadException: 未能加载文件或程序集"System.Web.Mvc, Version=5.0.0.0, Culture=neutral, Publi ...

  8. 修改 hosts

    不会牛逼操作 -1. 位置.格式 所有系统都差不多,都是 啥啥/etc/hosts 这样的 . 具体去查即可 . 格式: ip + 域名 域名不能含有通配符 hosts 可以绕过 dns 解析,直接访 ...

  9. .Net 5.0快速上手 Redis

    1. Redis的安装地址: https://files.cnblogs.com/files/lbjlbj/Redis3.7z   2.开启服务: 找到redis目录 打开cmd 输入redis-se ...

  10. Linux使用netstat查看网络状态

    查看本机的网络状态.使用netstat查看网络状态.显示系统端口使用情况.UDP类型的端口.TCP类型的端口.只显示所有监听端口.只显示所有监听tcp端口. 命令使用举例 命令 说明 netstat ...