文章目录
- Mybatis plus快速使用
- 1.ORM
- 2.mybatis plus介绍
- 3.mybatis plus使用
- 1.添加依赖
- 2.配置信息
- 3.启动类加入 @MapperScan(“填入mapper包的位置”)
- 4.创建user接口,在mapper中加入UserMapper接口
- 5.mybatis-plus crud注解
- 启动springboot项目
- mapper使用继承basemapper
Mybatis plus快速使用
1.ORM
2.mybatis plus介绍
3.mybatis plus使用
1.添加依赖
<!-- MyBatis Plus 核心依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!-- MySQL 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- Druid 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
2.配置信息
spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
这里注意数据库名称和密码都需要自己的
3.启动类加入 @MapperScan(“填入mapper包的位置”)
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}
4.创建user接口,在mapper中加入UserMapper接口
package com.example.demo.controller;
import com.example.demo.mapper.pojo.User;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;
@RestController
public class UserController {
// @Operation(summary = "获取用户信息")
// @GetMapping("/user/{id}")
// public String getUserById(@PathVariable int id) {
// return "根据ID获取用户";
// }
@GetMapping("/user/")
public String getUser() {
return "根据ID获取用户";
}
@PostMapping("/user")
public String save(User user) {
return "添加用户";
}
@PutMapping("/user")
public String update(User user) {
return "更新用户";
}
@DeleteMapping("/user/{id}")
public String deleteById(@PathVariable int id) {
return "根据ID删除用户";
}
}
package com.example.demo.mapper;
import com.example.demo.mapper.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("select * from user")
public List<User> find();
}
5.mybatis-plus crud注解
启动springboot项目
mapper使用继承basemapper
package com.example.demo.mapper.pojo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper extends BaseMapper<User> {
// @Select("select * from user")
// public List<User> find();
}
修改成以上之后就不用我们写代码了