mybatis-plus的特性
首先搭建好springboot项目,引入pom文件
com.baomidou mybatis-plus-boot-starter 3.1.0 org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.1 com.gitee.sunchenbin.mybatis.actable mybatis-enhance-actable 1.1.1.RELEASE
连接数据库这一步很关键,这里使用druid连接mysql数据库
mysql mysql-connector-java com.alibaba druid-spring-boot-starter 1.1.10
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{ }
新建service及impl文件
Service文件继续IService
Impl继承ServiceImpl
注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{ /** * 查询所有 * @return */ List 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 ServiceImplimplements IUserService{ @Autowired private UserMapper userMapper; //查询所有 @Override public List listAll() { List 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 ListlistAll(){ List 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 UpdateWrapperuserUpdateWrapper = new UpdateWrapper<>(); userUpdateWrapper.eq("id", "1"); boolean update = mapper.update(user, userUpdateWrapper); System.out.println(update); }
mybatis-plus删除数据操作
@Test public void delete() { QueryWrapperqueryWrapper = 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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!