Mybatis
mybatis是一款优秀的持久层框架,用于简化JDBC的开发。(控制层 controller)(业务层service)(持久层dao)(数据库)
快速创建springboot-mybatis工程
jdk选11 ,依赖Mybatis Framework 和 MySql driver---- 删除乱七八糟的文件夹 只留下src和pom.xml
创建数据库表
登录mysql,创建mybatis数据库
create database mybatis;
create table user(id int unsigned primary key auto_increment,name varchar(100),age tinyint unsigned,gender tinyint unsigned,phone varchar(11));//创建个user表 随表插几条数据
insert into user (name, age, gender, phone) VALUES ('朱元璋',22,1,'12345678900');
insert into user (name, age, gender, phone) VALUES ('马皇后',18,2,'12345678901');
项目生成user类
package com.example.springbootmybatis.pojo;public class User {private Integer id;private String name;private Short age;private Short gender;private String phone;public User() {}public User(Integer id, String name, Short age, Short gender, String phone) {this.id = id;this.name = name;this.age = age;this.gender = gender;this.phone = phone;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Short getAge() {return age;}public void setAge(Short age) {this.age = age;}public Short getGender() {return gender;}public void setGender(Short gender) {this.gender = gender;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", gender=" + gender +", phone='" + phone + '\'' +'}';}
}
引入Mybatis的相关依赖,配置mybatis数据库连接信息
application.properties
#配置数据库连接信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=1234
UserMapper接口
@Mapper //运行时。会自动生成该接口的实现对象(代理对象),并且将对象交给IOC容器管理
public interface UserMapper {//定义方法//查询所哟@Select("select * from user")public List<User> list();
}
基本测试
@Autowired
private UserMapper userMapper;
查询中文乱码
show variables like'%basedir%';//检查Mysql ini文件
检查IDEA配置
检查properties配置
配置SQL提示
JDBC快速入门
@Testpublic void jdbcTest() throws Exception {//1.注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//2.获取连接对象String url ="jdbc:mysql://localhost:3306/mybatis";String userName = "root";String password = "1234";Connection connection = DriverManager.getConnection(url, userName, password);//3.获取statement对象执行sqlString sql = "select * from user";Statement statement = connection.createStatement();ResultSet resultSet = statement.executeQuery(sql);//4.封装结果数据List<User> list = new ArrayList<>();while (resultSet.next()){Integer id = resultSet.getInt("id");String name = resultSet.getString("name");Short age = resultSet.getShort("age");Short gender = resultSet.getShort("gender");String phone = resultSet.getString("phone");User user = new User(id, name, age, gender, phone);list.add(user);}//5.释放资源statement.cancel();connection.close();list.stream().forEach(user -> {System.out.println(user);});}
数据库连接池
数据库连接池是个容器,负责分配、管理数据库连接(connection)。
允许应用程序使用同一个现有的数据库连接,而不需要新建立。
释放空间时间超过最大空闲时间的连接,来避免因没有释放连接而引起的数据库连接遗漏。
优势:资源重用、提升系统响应速度、避免数据库连接遗漏。
Druid连接池
pom.xml
<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.8</version></dependency>
lombok工具包
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>