亲宝软件园·资讯

展开

springboot+mybatis-plue内置CRUD 详解springboot+mybatis-plue实现内置的CRUD使用详情

Dlei东 人气:0

mybatis-plus的特性

首先搭建好springboot项目,引入pom文件

<!-- mybatis plus 依赖 -->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.1.0</version>
		</dependency>
		<!-- mybatis 依赖 -->

		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.0.1</version>
		</dependency>

		<!-- mybatis 根据实体生成数据库表依赖 -->
		<dependency>
			<groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
			<artifactId>mybatis-enhance-actable</artifactId>
			<version>1.1.1.RELEASE</version>
		</dependency>

连接数据库这一步很关键,这里使用druid连接mysql数据库

<!-- mysql数据库-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!-- druid连接数据库 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.10</version>
		</dependency>

application-prod.yml数据源配置文件,这里使用com.mysql.cj.jdbc.Driver的druid数据源,你也可以直接写在application.yml配置文件中

spring:
 datasource:
 url: jdbc:mysql://127.0.0.1:3306/lianxicrud?useUnicode=true&useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
 username: root
 password: 123456
 # 使用druid数据源
 type: com.alibaba.druid.pool.DruidDataSource
 driver-class-name: com.mysql.cj.jdbc.Driver
 
server:
	port: 8191 

新建mapper的空文件

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.psd.domain.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends BaseMapper<User>{
}

新建service及impl文件
Service文件继续IService
Impl继承ServiceImpl<UserMapper,User>实现service抽象接口
注ServiceImpl查询是直接this.list(),插入则使用userMapper.insert(user)
修改数据删除数据在单独在文章内容后文补充

import com.baomidou.mybatisplus.extension.service.IService;
import com.psd.domain.User;

import java.util.List;


public interface IUserService extends IService<User>{

 /**
  * 查询所有
  * @return
  */
 List<User> listAll();

	/**
  * id条件查询
  * @return
  */
 User listOne(Long id);

 /***
  * 保存
  * @param user
  * @return
  */
 int insert1(User user);

}
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.psd.domain.User;
import com.psd.mapper.UserMapper;
import com.psd.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements IUserService{

 @Autowired
 private UserMapper userMapper;

 //查询所有
 @Override
 public List<User> listAll() {
  List<User> users = this.list();
  return users;
 }

 //条件查询
 public User listOne(Long id) {
  User user = this.getById(id);
  return user;
 }

 //保存
 public int insert1(User user){
  return userMapper.insert(user);

 }

}

user实体

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;


@TableName("user")
@Data
public class User{

 private long id;
 private String name;
 private int age;
 private String email;
	//..使用lombok插件省列get set方法

}

控制器应用层类

import com.psd.domain.User;
import com.psd.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class UserController {

 @Autowired
 private IUserService userService;

 @GetMapping("/listAll")
 public List<User> listAll(){
  List<User> users = userService.listAll();
  return users;
 }

 @GetMapping("/listOne")
 public User listOne(){
  return userService.listOne(1L);
 }


 //保存
 @GetMapping("/save")
 public void baocun1(int age,String name,String email){
  User user1 = new User();
  user1.setAge(age);
  user1.setName(name);
  user1.setEmail(email);
  userService.insert1(user1);
 }
}

mybatis-plus修改数据操作

@Test
 public void update() {
 
  //修改值
  User user = new User();
  user.setAge(“20”);
  user.setName("李四");
 
  //修改条件意思就是修改值为user.setAge(“20”);user.setName("李四");像setEmail为null的视为不变,然后条件值为id=1
  UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
  userUpdateWrapper.eq("id", "1");
  boolean update = mapper.update(user, userUpdateWrapper);
 
  System.out.println(update);
 }

mybatis-plus删除数据操作

@Test
 public void delete() {
  QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  //条件意思就是删除id=1的并且isNotNull指"id"字段不为空的
  queryWrapper
    .ge("id", 1)
    .isNotNull("id");
  int delete = mapper.delete(queryWrapper);
 }

直接浏览器请求,那么整个过程就完成了
http://localhost:8191/save?age=18&name=张磊&email=739595798@qq.com
http://localhost:8191/user/listOne


到此这篇关于详解springboot+mybatis-plue实现内置的CRUD使用详情的文章就介绍到这了,更多相关springboot+mybatis-plue内置CRUD内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持! 

加载全部内容

相关教程
猜你喜欢
用户评论