六、MybatisPlus快速上手 数据库操作
注意!注意!注意!springboot版本选择2.7.2
1.ORM介绍(对象关系映射)
既包含存储,又包含映射。将java类映射到数据库
2.MybatisPlus介绍
ORM框架
数据库操作来啦!注意欧
1.添加依赖到pom。xmlwen文件中
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
2.在properties中加入配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/Englishword?useSSL=false
spring.datasource.username=root
spring.datasource.password="youpassword"
3.在mysql中添加数据库和表单,我添加了Englisword database 和user table
有关MYSQL的具体操作大家可以参考
CREATE TABLE user (
id VARCHAR(50) PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
birth VARCHAR(50) NOT NULL
);
INSERT INTO user (id, username, password, birthday)
VALUES ('1', 'Alice', 'password123', '1990-01-01'),
('2', 'Bob', '123password', '1995-05-10');
4.在启动类里面添加@MapperScan(“mapper文件夹路径”)
5.package mapper 添加usermapper (记得导入user类)
package com.example.sql2.mapper;
import com.example.sql2.entity.user;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
@Mapper
public interface UserMapper {
//根据ID查找用户
/* @Select("select * from user where id=#{id})
public int findbyid(int id);*/
//查询所有用户
@Select("select * from user")
public List<user> find();
//增加用户
@Insert("insert into user values(#{id},#{username},#{password},#{birthday})")
public int insert(user user);
//更新用户
@Update("update user set username=#{username},password=#{password},#{birthday}) where id=#{id}")
public int update(user user);
}
6.package entity添加实体user、
package com.example.sql2.entity;
public class user {
private String id;
private String username;
private String password;
private String birthday;
@Override
public String toString() {
return "user{" +
"id='" + id + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
", birthday='" + birthday + '\'' +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public user(String id, String username, String password, String birthday) {
this.id = id;
this.username = username;
this.password = password;
this.birthday = birthday;
}
}
7.package controller添加控制器usercontroller
@RestController
public class usercontroller {
@Autowired
private UserMapper usermapper;
@GetMapping("/user")
public List query(){
List <user>list=usermapper.find();
System.out.println(list);
return list;
}
@PostMapping("/user")
public String save(user user ){
int i=usermapper.insert(user);
if (i == 0) {
return "创建失败";
}
else return "创建成功";
}
}