网页网站关系,企业网站强制备案,空压机东莞网站建设,wordpress文章导入MyBatis 框架学习(I) 文章目录 MyBatis 框架学习(I)1. 介绍2. 准备测试3. MyBatis 注解基础操作3.1 日志输出3.2 Insert 操作3.3 Delete 操作3.4 Update 操作3.5 Select 操作 总结 1. 介绍
之前我们学习过利用JDBC操作数据库进行项目开发#xff0c;但我们发现它操作起来…MyBatis 框架学习(I) 文章目录 MyBatis 框架学习(I)1. 介绍2. 准备测试3. MyBatis 注解基础操作3.1 日志输出3.2 Insert 操作3.3 Delete 操作3.4 Update 操作3.5 Select 操作 总结 1. 介绍
之前我们学习过利用JDBC操作数据库进行项目开发但我们发现它操作起来会比较复杂而MyBatis就是一款简化JDBC操作的优秀持久层框架 持久层可以理解为进行持久化操作的层通常指数据访问层(dao)是用来操作数据库的 简单来说MyBatis就是更简单完成程序和数据库交互的框架也是一个更好操作数据库的工具
2. 准备测试
在我们开始MyBatis具体操作之前我们需要完成环境的配置
创建SpringBoot工程、引入相关依赖数据库准备、实体类准备配置MyBatis连接信息创建Mapper接口
先创建SpringBoot项目 在创建项目时引入相关驱动依赖pom.xml就会自动引入这些相关依赖 项目创建好后我们来创建测试用的数据库
-- 创建数据库
DROP DATABASE IF EXISTS mybatis_test;
CREATE DATABASE mybatis_test DEFAULT CHARACTER SET utf8mb4;
-- 使⽤数据数据
USE mybatis_test;
-- 创建表[⽤⼾表]
DROP TABLE IF EXISTS userinfo;
CREATE TABLE userinfo (id INT ( 11 ) NOT NULL AUTO_INCREMENT,username VARCHAR ( 127 ) NOT NULL,password VARCHAR ( 127 ) NOT NULL,age TINYINT ( 4 ) NOT NULL,gender TINYINT ( 4 ) DEFAULT 0 COMMENT 1-男 2-⼥ 0-默认,phone VARCHAR ( 15 ) DEFAULT NULL,delete_flag TINYINT ( 4 ) DEFAULT 0 COMMENT 0-正常, 1-删除,create_time DATETIME DEFAULT now(),update_time DATETIME DEFAULT now(),PRIMARY KEY ( id )
) ENGINE INNODB DEFAULT CHARSET utf8mb4;
-- 添加⽤⼾信息
INSERT INTO mybatis_test.userinfo ( username, password, age, gender, phone )
VALUES ( admin, admin, 18, 1, 18612340001 );
INSERT INTO mybatis_test.userinfo ( username, password, age, gender, phone )
VALUES ( zhangsan, zhangsan, 18, 1, 18612340002 );
INSERT INTO mybatis_test.userinfo ( username, password, age, gender, phone )
VALUES ( lisi, lisi, 18, 1, 18612340003 );
INSERT INTO mybatis_test.userinfo ( username, password, age, gender, phone )
VALUES ( wangwu, wangwu, 18, 1, 18612340004 );同时创建实体类即该实体类中的属性要与数据库中的字段对应
import lombok.Data;
import java.util.Date;Data
public class UserInfo {private Integer id;private String username;private String password;private Integer age;private Integer gender;private String phone;private Integer deleteFlag;private Date createTime; private Date updateTime;}注数据库中的字段有时候是多个单词拼接数据库中使用_分割在Java中我们使用小驼峰的方式来进行命名
之后配置MyBatis连接信息, MyBaits连接数据库需要配置以下信息
MySQL驱动类数据库连接的URL用户名密码
如果是配置文件是application.properties代码如下
#驱动类名称
spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver
#数据库连接的url
spring.datasource.urljdbc:mysql://localhost:3306/mybatis_test // 刚刚创建的表
#连接数据库的用户名
spring.datasource.usernameroot // 填写自己的用户名
#连接数据库的密码
spring.datasource.password123456 // 填写自己的密码如果配置文件是application.yml代码如下
# 数据库连接配置 每个空格都不能省略yml严格要求格式正确
spring:datasource:url: jdbc:mysql://127.0.0.1:3306/mybatis_testusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driver最后创建接口并通过测试类进行测试(测试类Spring在创建时已经给我们配置好在test目录下我们只需要根据需求编写相关代码即可或者右击mapper接口-Generate-Test, 勾选相应mapper方法然后编写需求代码即可)
package org.example.blog_mybatis.mapper;import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.example.blog_mybatis.pojo.UserInfo;import java.util.List;Mapper
public interface UserInfoMapper {Select(select id, username, password, age, gender, phone, delete_flag, create_time, update_time from userinfo)public ListUserInfo queryAllUser();}这样我们就能通过Mybatis来获取数据库中的信息了
小技巧当我们在进行sql语句编写时有时候字段多起来后我们自己手写可能会出现问题对此我们可以将数据库引入IDEA这样当我们编写sql语句时它就会自动为我们提示字段信息
按以下步骤配置 这样就配置好了同时它也能自动为我们获取字段信息了 3. MyBatis 注解基础操作
对于MyBatis操作数据库的方式有两种一种的通过注解的方式进行调用一种是通过xml文件进行调用对于使用那个没有固定的标准根据情况选择使用(比较简单的数据库操作可以使用注解较复杂的操作可以使用xml文件进行动态SQL操作)。xml后面会再开一篇文章进行讲解本文使用注解的方式进行操作
3.1 日志输出
上面我们通过println函数打印输出信息来观察数据库操作状态这样的方式观察起来效果不是很好对此我们可以使用MyBatis提供的日志输出来进行观察
在application.properties配置如下
#配置mybatis的日志, 指定输出到控制台
mybatis.configuration.log-implorg.apache.ibatis.logging.stdout.StdOutImpl3.2 Insert 操作
当我们要给数据库中的userinfot表添加一条数据时它的SQL语句是
insert into mybatis_test(username, password, age, gender, phone) values (zhangsan, 111, 18, 1, 123);将SQL中的常量替换为动态的参数在Mapper接口中它的注解为
Insert(insert into userinfo(username, password, age, gender, phone) values (#{username}, #{password}, #{age}, #{gender}, #{phone}))
void InsertUser(UserInfo userInfo);在注解中为了使我们的sql语句的操作条件不为固定值我们通过方法形参获取需求参数在注解中我们接收参数的方式是 #{}在括号里传入对应的方法参数
注
括号中填入的参数要与方法参数的名称相同如果传入的方法参数是对象的话填入括号的参数名称与对象的属性相同
编写测试类代码这里使用自己构建的测试类
package org.example.blog_mybatis.mapper;import org.example.blog_mybatis.pojo.UserInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;SpringBootTest
class UserInfoMapperTest {Autowiredprivate UserInfoMapper userInfoMapper;Testvoid insertUser() {UserInfo userInfo new UserInfo();userInfo.setUsername(zhangsan);userInfo.setPassword(111);userInfo.setAge(18);userInfo.setGender(1);userInfo.setPhone(123);userInfoMapper.InsertUser(userInfo);}
}同时我们也可以通过Param注解给方法参数设置别名则注解在接收参数时#{}中的内容需要与Param中的参数一样如果参数是对象则需要为参数.属性
Insert(insert into userinfo(username, password, age, gender, phone) values (#{userInfo.username}, #{userInfo.password}, #{userInfo.age}, #{userInfo.gender}, #{userInfo.phone}))
void InsertUser(Param(userInfo) UserInfo userInfo);拓展:
Insert语句默认返回的是受影响的行数但在有些情况下我们需要获取新插入数据的ID这时如果我们想要拿到这个自增ID需要在Mapper接口的方法上添加一个Options注解
Options(useGeneratedKeys true, keyProperty id)
Insert(insert into userinfo(username, password, age, gender, phone) values (#{username}, #{password}, #{age}, #{gender}, #{phone}))
void InsertUser(UserInfo userInfo);useGeneratedKeys这会令MyBatis使用JDBC的getGeneratedKeys方法来取出数据库内部生成的主键默认为falsekeyProperty指定能够唯⼀识别对象的属性MyBatis 使用 getGeneratedKeys 的返回值或 insert 语句的 selectKey子元素设置它的值默认值未设置(unset)
编写测试代码
Test
void insertUser() {UserInfo userInfo new UserInfo();userInfo.setUsername(XiaoMa);userInfo.setPassword(111);userInfo.setAge(18);userInfo.setGender(1);userInfo.setPhone(10086);Integer count userInfoMapper.InsertUser(userInfo);System.out.println(添加数据条数: count 数据ID: userInfo.getId());
}3.3 Delete 操作
当我们要删除数据库中的userinfo表中的一条数据时它的SQL语句是
delete from maybatis_test where id 3;将SQL中的常量替换为动态的参数在Mapper接口中它的注解为
Delete(delete from userinfo where id #{id})
void DeleteUser(Integer id);编写测试类代码
Test
void deleteUser() {userInfoMapper.DeleteUser(3);
}3.4 Update 操作
当我们要修改数据库中的userinfo表中的数据时它的SQL语句是
update userinfo set username zhaoliu, age 36 where id 5;将SQL中的常量替换为动态的参数在Mapper接口中它的注解为
Update(update userinfo set username #{username}, age #{age} where id #{id})
Integer UpdateUser(UserInfo userInfo);编写测试类代码
Test
void updateUser() {UserInfo userInfo new UserInfo();userInfo.setId(5);userInfo.setUsername(zhaoliu);userInfo.setAge(36);userInfoMapper.UpdateUser(userInfo);
}3.5 Select 操作
当我们要查询数据库中的userinfo表中的数据时它的SQL语句是
select id, username, password, age, gender, phone, delete_flag, create_time, update_time from userinfo where id 5;在Mapper接口中它的注解为
Select(select id, username, password, age, gender, phone, delete_flag, create_time, update_time from userinfo where id #{id})
UserInfo queryUser(Integer id);编写测试类代码
Test
void queryUser() {UserInfo userInfo userInfoMapper.queryUser(5);System.out.println(userInfo);
}MyBatis会根据方法返回的结果进行赋值 若方法用对象UserInfo接收返回结果MySQL查询出来的数据为一条就会自动赋值给对象 若方法用集合List接收返回结果MySQL查询出来的数据若为多条就会自动赋值集合List 但若MySQL返回的结果为多条数据却用UserInfo去接收多条数据则MyBatis就会报错 通过上述查询语句数据能够被显示出来但却存在着一些问题后面三个数的值为null
原因就在于在返回结果时MyBaits会自动获取结果中返回的列名并在Java类中查找相同名字的属性若名称相同则会将结果数据赋值给该属性但是我们的UserInfo中的属性名称为deleteFlag而数据库中对应的字段名称为delete_flag两者从名称上看并不相同则UserInfo中的deleteFlag属性无法被赋到值 那怎么解决上面这个问题呢这里我们开启驼峰命名解决(还有其它方法如起别名结果映射等这里使用驼峰命名只要配置好就能直接使用方便高效) 通常数据库中使用蛇形命名法进行命名(下划线分割各个单词)而Java属性一般遵循驼峰命名法约定。为了在这两种命名方式之间启用自动映射需要将map-underscore-to-camel-case设置为true 在配置文件application.properties中加入
#开启mybatis的驼峰命名自动映射开关 a_column ------ aCloumn
mybatis.configuration.map-underscore-to-camel-casetrue这样缺失的数据也能查询到了
总结
以上就是本文对MyBatis框架学习注解方式的介绍与使用了接下来也会更新MyBatis框架学习XML的方式的文章敬请期待