实训学习收获满满,跟着老师学习了 Mybatis入门的有关知识,有幸能参加此次学习,希望文章也能帮助大家!!!
1Mybatis入门
1.1 介绍
以前我们是在图形化客户端工具中编写SQL查询代码,发送给数据库执行,数据库执行后返回操作结果。
现在使用Mybatis操作数据库,就是在Mybatis中编写SQL查询代码,发送给数据库执行,数据库执行后返回结果。
Mybatis会把数据库执行的查询结果,使用实体类封装起来(一行记录对应一个实体类对象)
Mybatis操作数据库的步骤:
- 准备工作(数据库表emp、创建springboot工程、实体类Emp)
- 引入Mybatis的相关依赖,配置Mybatis(数据库连接信息)
- 编写SQL语句(注解)
1.2 入门案例
【1】导入数据库
create database mybatis_db;
use mybatis_db;
CREATE TABLE `emp` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`age` int DEFAULT NULL,
`image` varchar(66) DEFAULT 'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/1.jpg',
`gender` varchar(33) DEFAULT '1',
`job` varchar(32) DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8mb3;
INSERT INTO `emp` VALUES (1,'李二虎',23,'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/3.jpg','1','1'),(2,'王婷婷',22,'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/2.jpg','1','1'),(3,'刘皇叔',21,'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/1.jpg','1','1'),(4,'李白',24,'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/1.jpg','1','1'),(5,'李名',26,'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/3.jpg','1','1'),(6,'王昭君',32,'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/2.jpg','1','1'),(7,'刘备',22,'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/4.jpg','1','1'),(8,'小二郎',12,'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/2.jpg','1','1'),(9,'小龙女',13,'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/4.jpg','1','1'),(10,'貂蝉',15,'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/2.jpg','1','1'),(11,'刘三',17,'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/4.jpg','1','1'),(12,'李逵',11,'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/2.jpg','1','1'),(13,'李楠',19,'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/3.jpg','1','1'),(14,'小白龙',34,'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/1.jpg','1','1'),(15,'西施',33,'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/4.jpg','1','1'),(16,'刘茹',37,'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/4.jpg','1','1');
【2】项目搭建
创建day04-mybatismaven项目,并导入坐标
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<!--数据层-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.23</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!--工具类-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
【3】配置文件
在resources目录下创建application.yml文件,并引入配置
spring:
datasource: # 数据源配置
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 1234
【5】启动类
【6】实体类
package com.itheima.domain;
public class Emp {
private Long id;
private String name;
private Integer age;
private String image;
private String gender; // 1 男 2 女
private String job; // 1 讲师 2 班主任
//省略构造器,getter,setter方法(执行生成)
}
【7】编写SQL语句
@Mapper注解:表示是mybatis中的Mapper接口
@Select注解:代表的就是select查询,用于书写select查询语句
@Mapper // 交给mybatis框架执行
public interface EmpMapper {
@Select("select * from emp") // 将sql语句执行结果封装到Emp类中
public List<Emp> findAll();
}
【8】单元测试
package com.itheima.mapper;
import com.itheima.MyBatisApp;
import com.itheima.domain.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest(classes = MyBatisApp.class)
public class EmpMapperTest {
//Spring 可以管理我们对象
@Autowired
private EmpMapper empMapper;
@Test
public void findAll() {
System.out.println("Hello World");
List<Emp> all = empMapper.findAll();
for (int i = 0; i < all.size(); i++) {
Emp emp = all.get(i);
System.out.println("emp = " + emp);
}
}
}
1.3 lombok
Lombok是一个实用的Java类库,可以通过简单的注解来简化和消除一些必须有但显得很臃肿的Java代码。
通过注解的形式自动生成构造器、getter/setter、equals、hashcode、toString等方法,并可以自动化生成日志变量,简化java开发、提高效率。
package com.itheima.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data //帮我生成 getter,setter,toString 等
@NoArgsConstructor// 无参构造器
@AllArgsConstructor// 满参构造器
public class Emp {
private Long id;
private String name;
private Integer age;
private String image;
private String gender; // 1 男 2 女
private String job; // 1 讲师 2 班主任
}