第一步新建数据库mystudent 表 student
/*
Navicat MySQL Data Transfer
Source Server : biyesheji
Source Server Version : 50728
Source Host : 127.0.0.1:3306
Source Database : mystudent
Target Server Type : MYSQL
Target Server Version : 50728
File Encoding : 65001
Date: 2022-12-26 12:29:30
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`studentId` int(11) NOT NULL AUTO_INCREMENT,
`studentName` varchar(255) DEFAULT NULL,
PRIMARY KEY (`studentId`)
) ENGINE=InnoDB AUTO_INCREMENT=389 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('377', 'a');
INSERT INTO `student` VALUES ('378', 'b');
INSERT INTO `student` VALUES ('379', 'c');
INSERT INTO `student` VALUES ('380', 'd');
INSERT INTO `student` VALUES ('381', 'e');
INSERT INTO `student` VALUES ('382', 'f');
INSERT INTO `student` VALUES ('383', 'g');
INSERT INTO `student` VALUES ('384', 'h');
INSERT INTO `student` VALUES ('385', 'i');
INSERT INTO `student` VALUES ('386', 'g');
INSERT INTO `student` VALUES ('387', 'k');
INSERT INTO `student` VALUES ('388', '张三');
第二步创建springboot项目,导入相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--lombok用来简化实体类:需要安装lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!-- mysql 驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mybatis-plus依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!--mybatis-plus 代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
</dependencies>
第三步创建MyStudnt实体类
注解说明
@Data 生成 get set 方法
@TableName(“student”) 当实体名和数据库表名不一致的时候使用
@TableId(value = “studentId”, type = IdType.AUTO)指定 id和主键策略为自增
@TableField(“studentName”) 指定 列名
package com.front.spring_mycrud.entriy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
* @description:
* @author: 刘瑞_LiuRui
* @data: 2022/12/26
* @Version 1.0
**/
@Data
@TableName("student")
public class MyStudent {
@TableId(value = "studentId", type = IdType.AUTO)
private Integer studentId;
@TableField("studentName")
private String studentName;
}
第四步创建mapper
package com.front.spring_mycrud.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.front.spring_mycrud.entriy.MyStudent;
public interface MyStudentMapper extends BaseMapper<MyStudent> {
}
第五步创建 application.properties 和config 注册和扫描 组件
package com.front.spring_mycrud.config;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.stereotype.Component;
/**
* @description:
* @author: 刘瑞_LiuRui
* @data: 2022/12/26
* @Version 1.0
**/
@Component
@MapperScan("com.front.spring_mycrud.mapper")
public class MyConfig {
}
application.properties
server.port=8800
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mystudent?serverTimezone=GMT%2B8&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.mapper-locations= classpath:xml/*.xml
第六步测试
package com.front.spring_mycrud;
import com.front.spring_mycrud.entriy.MyStudent;
import com.front.spring_mycrud.mapper.MyStudentMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.List;
@SpringBootTest
class SpringMycrudApplicationTests {
// 注册MyStudentMapper
@Resource
private MyStudentMapper studentMapper;
@Test
public void selectAllTest() {
List<MyStudent> list = studentMapper.selectList(null);
list.forEach(System.out::println);
}
}