目录一、什么是 MyBatis二、MyBatis 入门2.1 准备工作2.2 配置数据库连接2.3 编写持久层的代码三、MyBatis 基础操作3.1 打印日志3.2 参数传递3.3 增加 Insert3.4 删Delete3.5 改Update3.6 查Select一、什么是 MyBatisMyBatis 是一款优秀的持久层框架用于简化 JDBC 的开发。它本是 Apache 的开源项目 iBatis2010 年迁移到 Google Code 并改名为 MyBatis2013 年迁移到 GitHub。所谓持久层指的就是持久化操作的层通常指数据访问层Dao用来操作数据库。简单来说MyBatis 是更简单完成程序和数据库交互的框架也就是更简单的操作和读取数据库的工具。二、MyBatis 入门MyBatis 操作数据库的步骤准备工作创建 SpringBoot 工程、数据库表准备、实体类引入 MyBatis 相关依赖配置数据库连接信息编写 SQL 语句注解/XML测试2.1 准备工作创建工程创建 SpringBoot 工程并导入 MyBatis 起步依赖和 MySQL 驱动包。!-- Source: https://mvnrepository.com/artifact/org.mybatis/mybatis -- dependency groupIdorg.mybatis/groupId artifactIdmybatis/artifactId version3.5.19/version scopecompile/scope /dependency !-- Source: https://mvnrepository.com/artifact/mysql/mysql-connector-java -- dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version8.0.33/version scopecompile/scope /dependency2.2 配置数据库连接在 application.yml 中配置spring: datasource: url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncodingutf8useSSLfalse username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver2.3 编写持久层的代码Mapper public interface UserInfoMapper { Select(select username, password, age, gender, phone from user_info) ListUserInfo queryAllUser(); }Mapper 注解 表示这是 Mybatis 的 Mapper 的接口运行的时候会自动生成类对象并交给 Spring 的 IOC 容器管理。Select 注解代表是 Select 查询注解内容是 select 的 sqlInsert 注解代表是 insert 插入注解内容是 insert 的 sqlUpdate 注解代表是 update 修改注解内容是 update 的 sqlDelete 注解代表是 delete 删除注解内容是 delete 的 sql三、MyBatis 基础操作3.1 打印日志在配置文件中开启 MyBatis 日志可以查看 SQL 执行参数传递和执行结果yml格式为mybatis: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl3.2 参数传递使用 #{} 获取方法中的参数Select(select username, password, age, gender, phone from user_info where id #{id}) UserInfo queryById(Integer id);特例如果方法只有一个普通类型的参数#{} 里面的属性名可以随便写建议和参数名保持一致。可以通过 Param 设置参数别名Select(select username, password, age, gender, phone from user_info where id #{userid}) UserInfo queryById(Param(userid) Integer id);3.3 增加 InsertInsert(insert into user_info (username, password, age, gender, phone) values (#{username},#{password},#{age},#{gender},#{phone})) Integer insert(UserInfo userInfo);返回主键如果需要获取新插入数据的自增id需要添加 Options 注解Options(useGeneratedKeys true, keyProperty id, keyColumn id) Insert(insert into user_info (username, age, gender, phone) values (#{userInfo.username},#{userInfo.age},#{userInfo.gender},#{userInfo.phone})) Integer insert(Param(userInfo) UserInfo userInfo);useGeneratedKeys令 Mybatis 使用 JDBC 的 getGeneratedKeys 方法取出数据库内部生成的主键。keyPropertyjava实体类中接收主键的属性名把数据库生成的主键值注入到实体对象的这个字段上。keyColumn数据库表中的主键列名告诉MyBatis数据库表返回的主键是哪一列3.4 删DeleteDelete(delete from user_info where id #{id}) void delete(Integer id);3.5 改UpdateUpdate(update user_info set username#{username} where id#{id}) void update(UserInfo userInfo);3.6 查Select查询的时候只有 Java 对象属性和数据库字段一模一样的时候才会进行赋值。Java对象使用驼峰命名方式数据库字段使用蛇形命名方式这两者就对应不上如何解决。解决方法1.起别名在SQL中给列明起别名保持与实体类属性名一致。Select(select id, username, password, age, gender, phone, delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from user_info) ListUserInfo queryAllUser();2.结果映射: 使用Results注解Select(select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info) Results({ Result(column delete_flag, property deleteFlag), Result(column create_time, property createTime), Result(column update_time, property updateTime) }) ListUserInfo queryAllUser();如果其他 SQL 也希望复用这个映射关系可以给 Result 定义名称这里就把下面的映射关系的名字定义为 resultMap然后通过 ResultMap(value resultMap) 来复用Results(id resultMap, value { Result(column delete_flag, property deleteFlag), Result(column create_time, property createTime), Result(column update_time, property updateTime) })3.开启驼峰自动转换推荐mybatis: configuration: map-underscore-to-camel-case: true驼峰命名规则 abc_xyz abcXyz。表中字段用蛇形命名发Java 属性用驼峰命名发开启这个配置之后自动映射。