wget https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm yum install mysql80-community-release-el8-1.noarch.rpm
yum install mysql-community-server
/bin/systemctl start mysqld.service service mysqld status
grep 'temporary password' /var/log/mysqld.log
mysql -u root -p 随机密码
先更改成满足它的密码策略
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Root_21root';
修改密码长度
set global validate_password.length=1;
修改密码等级
set global validate_password.policy=0;
设置成自己想要的密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
create user 'root'@'%' identified by 'root123'; //1、先创建权限记录 grant all privileges on *.* to 'root'@'%' with grant option; //2、授权
使用Navicat Premium 15进行连接
官方软件下载地址:http://www.navicat.com.cn/download/navicat-premium
打开软件,点击连接选择mysql,输入主机ip和密码点击确认即可连接
这样mysql就安装成功了
官方地址
MyBatisPlus
特性
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window) 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
任何能使用 mybatis 进行 crud, 并且支持标准 sql 的数据库
Gitee|Github
拥有 Java 开发环境以及相应 IDE,这里使用IDEA
整体框架使用 Spring Boot
项目构建使用 Maven
数据库使用MySQL
准备一张表
代码如下
创建user表
CREATE TABLE user ( id BIGINT(20) AUTO_INCREMENT NOT NULL COMMENT '主键ID', name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名', age INT(11) NULL DEFAULT NULL COMMENT '年龄', email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱', PRIMARY KEY (id) ) default charset utf8;
写入数据
INSERT INTO user (name, age, email) VALUES ('Tom', 18, 'tom@dowhere.com'), ('Jerry', 20, 'jerry@dowhere.com'), ('Aaron', 28, 'aaron@dowhere.com'), ('Jack', 21, 'jack@dowhere.com'), ('Rose', 24, 'rose@dowhere.com');
点击下一步
这里添加两个依赖Lombok和Mysql Driver,点击完成
引入 Spring Boot Starter 父工程:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.2</version> <relativePath/> </parent>
以下是全部代码
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>mybatisplus</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mybatisplus</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
在application.properties配置文件中添加mysql数据库的相关配置:
spring.datasource.driver-lass-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test1 spring.datasource.username=root spring.datasource.password=123456
test1是自己的数据库名字
在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:
package com.example.mybatisplus; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.mybatisplus.mapper") public class MyBatisPlusApplication { public static void main(String[] args) { SpringApplication.run(MyBatisPlusApplication.class, args); } }
编写实体类 User.java(此处使用了 Lombok (opens new window) 简化代码)
package com.example.mybatisplus; import lombok.Data; /** * @author lsk * @date 2021/7/15 - 22:48 */ @Data public class User { private Long id; private String name; private Integer age; private String email; }
编写Mapper类 UserMapper.java
package com.example.mybatisplus.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.mybatisplus.User; /** * @author lsk * @date 2021/7/15 - 22:49 */ public interface UserMapper extends BaseMapper<User> { }
package com.example.mybatisplus; import com.example.mybatisplus.mapper.UserMapper; import org.junit.Assert; 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 class MyBatisPlusApplicationTests { @Autowired(required = false) private UserMapper userMapper; @Test void contextLoads() { System.out.println(("----- selectAll method test ------")); List<User> userList = userMapper.selectList(null); Assert.assertEquals(5, userList.size()); userList.forEach(System.out::println); } }