准备工作
数据表emp
/*
Navicat Premium Data Transfer
Source Server : mysql8
Source Server Type : MySQL
Source Server Version : 80036 (8.0.36)
Source Host : localhost:3306
Source Schema : emp
Target Server Type : MySQL
Target Server Version : 80036 (8.0.36)
File Encoding : 65001
Date: 26/03/2024 21:04:50
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for emp
-- ----------------------------
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`passwoed` varchar(32) NOT NULL DEFAULT '123456',
`name` varchar(10) NOT NULL,
`gender` tinyint unsigned NOT NULL,
`image` varchar(255) DEFAULT NULL,
`jop` tinyint unsigned DEFAULT NULL,
`entrydate` date DEFAULT NULL,
`dept_id` int unsigned DEFAULT NULL,
`create_time` datetime NOT NULL,
`update_time` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
-- ----------------------------
-- Records of emp
-- ----------------------------
BEGIN;
INSERT INTO `emp` (`id`, `username`, `passwoed`, `name`, `gender`, `image`, `jop`, `entrydate`, `dept_id`, `create_time`, `update_time`) VALUES (1, 'user1', '123456', '姓名1', 1, '1.jpg', 4, '2000-01-01', 2, '2022-11-02 00:00:00', '2022-11-11 00:00:00');
INSERT INTO `emp` (`id`, `username`, `passwoed`, `name`, `gender`, `image`, `jop`, `entrydate`, `dept_id`, `create_time`, `update_time`) VALUES (2, 'user2', '123456', '姓名2', 1, '2.jpg', 4, '2001-02-02', 2, '2022-11-03 00:00:00', '2022-11-12 00:00:00');
INSERT INTO `emp` (`id`, `username`, `passwoed`, `name`, `gender`, `image`, `jop`, `entrydate`, `dept_id`, `create_time`, `update_time`) VALUES (3, 'user3', '123456', '姓名3', 0, '3.jpg', 3, '2002-03-03', 3, '2022-11-04 00:00:00', '2022-11-13 00:00:00');
INSERT INTO `emp` (`id`, `username`, `passwoed`, `name`, `gender`, `image`, `jop`, `entrydate`, `dept_id`, `create_time`, `update_time`) VALUES (4, 'user4', '123456', '姓名4', 1, '4.jpg', 5, '2003-04-04', 1, '2022-11-05 00:00:00', '2022-11-14 00:00:00');
INSERT INTO `emp` (`id`, `username`, `passwoed`, `name`, `gender`, `image`, `jop`, `entrydate`, `dept_id`, `create_time`, `update_time`) VALUES (5, 'user5', '123456', '姓名5', 0, '5.jpg', 2, '2004-05-05', 2, '2022-11-06 00:00:00', '2022-11-15 00:00:00');
INSERT INTO `emp` (`id`, `username`, `passwoed`, `name`, `gender`, `image`, `jop`, `entrydate`, `dept_id`, `create_time`, `update_time`) VALUES (6, 'user6', '123456', '姓名6', 1, '6.jpg', 4, '2005-06-06', 3, '2022-11-07 00:00:00', '2022-11-16 00:00:00');
INSERT INTO `emp` (`id`, `username`, `passwoed`, `name`, `gender`, `image`, `jop`, `entrydate`, `dept_id`, `create_time`, `update_time`) VALUES (7, 'user7', '123456', '姓名7', 0, '7.jpg', 5, '2006-07-07', 1, '2022-11-08 00:00:00', '2022-11-17 00:00:00');
INSERT INTO `emp` (`id`, `username`, `passwoed`, `name`, `gender`, `image`, `jop`, `entrydate`, `dept_id`, `create_time`, `update_time`) VALUES (8, 'user8', '123456', '姓名8', 1, '8.jpg', 3, '2007-08-08', 2, '2022-11-09 00:00:00', '2022-11-18 00:00:00');
INSERT INTO `emp` (`id`, `username`, `passwoed`, `name`, `gender`, `image`, `jop`, `entrydate`, `dept_id`, `create_time`, `update_time`) VALUES (9, 'user9', '123456', '姓名9', 0, '9.jpg', 2, '2008-09-09', 3, '2022-11-10 00:00:00', '2022-11-19 00:00:00');
INSERT INTO `emp` (`id`, `username`, `passwoed`, `name`, `gender`, `image`, `jop`, `entrydate`, `dept_id`, `create_time`, `update_time`) VALUES (10, 'user10', '123456', '姓名10', 1, '10.jpg', 5, '2009-10-10', 1, '2022-11-11 00:00:00', '2022-11-20 00:00:00');
INSERT INTO `emp` (`id`, `username`, `passwoed`, `name`, `gender`, `image`, `jop`, `entrydate`, `dept_id`, `create_time`, `update_time`) VALUES (11, 'user11', '123456', '姓名11', 0, '11.jpg', 4, '2010-11-11', 2, '2022-11-12 00:00:00', '2022-11-21 00:00:00');
INSERT INTO `emp` (`id`, `username`, `passwoed`, `name`, `gender`, `image`, `jop`, `entrydate`, `dept_id`, `create_time`, `update_time`) VALUES (12, 'user12', '123456', '姓名12', 1, '12.jpg', 3, '2011-12-12', 3, '2022-11-13 00:00:00', '2022-11-22 00:00:00');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
创建springboot工程,并在application.properties引入数据库连接信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.NonRegisteringDriver
spring.datasource.url=jdbc:mysql://localhost:3306/emp
spring.datasource.username=root
spring.datasource.password=12345678
pom.xml添加依赖
<?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>
<groupId>com.itheima</groupId>
<artifactId>springboot-itheima</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-itheima</name>
<description>springboot-itheima</description>
<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.13</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.itheima.SpringbootItheimaApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
创建实体类Emp
package com.itheima.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Integer id;
private String username;
private String name;
private Short gender;
private String image;
private Short job;
private LocalDate entrydate;
private Integer deptId;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
LocalDate和LocalDateTime报错,是因为idea配置了低版本,但语法需要更高版本的
需要注意两个地方
在File->settings->Build,Execution,Deployment-->Compiler-->中更改Target bytecode version版本
在File-->Project Structure中更改Language level 版本
准备Mapper接口Emapper
package com.itheima.mapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface EmpMapper {
}