1.lombok的使用
2.Spring-mybatis-增删改查准备工作
This commit is contained in:
33
springboot-mybatis-crud/.gitignore
vendored
Normal file
33
springboot-mybatis-crud/.gitignore
vendored
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
HELP.md
|
||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
83
springboot-mybatis-crud/pom.xml
Normal file
83
springboot-mybatis-crud/pom.xml
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
<?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.inmind</groupId>
|
||||||
|
<artifactId>springboot-mybatis-crud</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>springboot-mybatis-crud</name>
|
||||||
|
<description>springboot-mybatis-crud</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.7.6</spring-boot.version>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
<version>2.3.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</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.inmind.SpringbootMybatisCrudApplication</mainClass>
|
||||||
|
<skip>true</skip>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>repackage</id>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@@ -0,0 +1,13 @@
|
|||||||
|
package com.inmind;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SpringbootMybatisCrudApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringbootMybatisCrudApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,8 @@
|
|||||||
|
package com.inmind.mapper;
|
||||||
|
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface EmpMapper {
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,26 @@
|
|||||||
|
package com.inmind.pojo;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Emp {
|
||||||
|
private Integer id;//ID
|
||||||
|
private String username;//用户名
|
||||||
|
private String password;//密码
|
||||||
|
private String name;//姓名
|
||||||
|
private Short gender;//性别 1:男 2:女
|
||||||
|
private String image; //头像URL
|
||||||
|
private String job; // 职位 1:班主任 2:讲师 3:学工主管 4:教研主管 5:咨询师
|
||||||
|
private LocalDate entrydate; // 入职日期
|
||||||
|
private Integer deptId; // 部门ID
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
private LocalDateTime updateTime;
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,14 @@
|
|||||||
|
#下面这些内容是为了让MyBatis映射
|
||||||
|
#指定Mybatis的Mapper文件
|
||||||
|
mybatis.mapper-locations=classpath:mappers/*xml
|
||||||
|
#指定Mybatis的实体目录
|
||||||
|
mybatis.type-aliases-package=com.inmind.mybatis.entity
|
||||||
|
|
||||||
|
#驱动类名称
|
||||||
|
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||||
|
#数据库连接的url
|
||||||
|
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
|
||||||
|
#连接数据库的用户名
|
||||||
|
spring.datasource.username=root
|
||||||
|
#连接数据库的密码
|
||||||
|
spring.datasource.password=1234
|
@@ -0,0 +1,13 @@
|
|||||||
|
package com.inmind;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class SpringbootMybatisCrudApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -44,6 +44,18 @@
|
|||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--阿里巴巴的DRUID依赖-->
|
||||||
|
<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>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<!--统一springboot依赖管理版本-->
|
<!--统一springboot依赖管理版本-->
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
@@ -14,6 +14,7 @@ public interface UserMapper {
|
|||||||
/*
|
/*
|
||||||
查询所有用户数据
|
查询所有用户数据
|
||||||
*/
|
*/
|
||||||
@Select("select * from user;")
|
// @Select("select * from user;")
|
||||||
|
@Select("select * from user")
|
||||||
public List<User> list();
|
public List<User> list();
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,14 @@
|
|||||||
package com.inmind.pojo;
|
package com.inmind.pojo;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
/*@Getter
|
||||||
|
@Setter
|
||||||
|
@ToString
|
||||||
|
@EqualsAndHashCode*/
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor //生成无参构造
|
||||||
|
@AllArgsConstructor//生成满参构造方法
|
||||||
public class User {
|
public class User {
|
||||||
private Integer id;
|
private Integer id;
|
||||||
private String name;
|
private String name;
|
||||||
@@ -7,65 +16,4 @@ public class User {
|
|||||||
private Short gender;
|
private Short gender;
|
||||||
private String phone;
|
private String phone;
|
||||||
|
|
||||||
public User() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public User(Integer id, String name, Integer age, Short gender, String phone) {
|
|
||||||
this.id = id;
|
|
||||||
this.name = name;
|
|
||||||
this.age = age;
|
|
||||||
this.gender = gender;
|
|
||||||
this.phone = phone;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "User{" +
|
|
||||||
"id=" + id +
|
|
||||||
", name='" + name + '\'' +
|
|
||||||
", age=" + age +
|
|
||||||
", gender=" + gender +
|
|
||||||
", 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 Integer getAge() {
|
|
||||||
return age;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAge(Integer 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -6,21 +6,61 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest //该注解的作用,启动spring环境,所以也就有了IOC容器
|
||||||
class SpringbootMybatisQuickstartApplicationTests {
|
class SpringbootMybatisQuickstartApplicationTests {
|
||||||
|
|
||||||
@Autowired//直接从IOC容器中获取UserMapper类型的对象
|
@Autowired//直接从IOC容器中获取UserMapper类型的对象
|
||||||
private UserMapper userMapper;
|
private UserMapper userMapper;//接口开发,多态
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void contextLoads() {
|
void contextLoads() {
|
||||||
//使用mybatis框架查询所有用户的数据
|
//使用mybatis框架查询所有用户的数据
|
||||||
List<User> list = userMapper.list();
|
List<User> list = userMapper.list();
|
||||||
list.stream().forEach(user -> {
|
list.stream().forEach(user -> {
|
||||||
|
System.out.println(user.getId());
|
||||||
System.out.println(user);
|
System.out.println(user);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testJDBC() throws ClassNotFoundException, SQLException {
|
||||||
|
//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. 获取执行SQL的对象Statement,执行SQL,返回结果
|
||||||
|
String sql = "select * from user";
|
||||||
|
Statement statement = connection.createStatement();
|
||||||
|
ResultSet resultSet = statement.executeQuery(sql);
|
||||||
|
|
||||||
|
//4. 封装结果数据
|
||||||
|
List<User> userList = new ArrayList<>();
|
||||||
|
while (resultSet.next()){
|
||||||
|
int id = resultSet.getInt("id");
|
||||||
|
String name = resultSet.getString("name");
|
||||||
|
int age = resultSet.getInt("age");
|
||||||
|
short gender = resultSet.getShort("gender");
|
||||||
|
String phone = resultSet.getString("phone");
|
||||||
|
|
||||||
|
User user = new User(id,name,age,gender,phone);
|
||||||
|
userList.add(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
//5. 释放资源
|
||||||
|
statement.close();
|
||||||
|
connection.close();
|
||||||
|
|
||||||
|
|
||||||
|
userList.forEach(System.out::println);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user