概述
用于记录,mybatis-plus只是mybatis的一个增强版。
创建user表
导入依赖
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<groupId>com.lzp</groupId>
<artifactId>mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--Mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<!--运行阶段:容器已有jar包,则无需此依赖-->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置yml
spring:
application:
name: test
datasource:
username: root
password: a
url: jdbc:mysql://localhost:3306/tb_test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
#mybatis-plus
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml
编写实体类与方法
User
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ToString
@NoArgsConstructor
@AllArgsConstructor
@TableName("user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.ASSIGN_ID)
private Integer id;
private String uname;
private String upwd;
}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lzp.mybatis.mapper.UserMapper">
<select id="findAll" resultType="com.lzp.mybatis.entity.User">
SELECT
*
FROM
user
WHERE
1=1
</select>
</mapper>
UserMapper
package com.lzp.mybatis.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lzp.mybatis.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper extends BaseMapper<User> {
List<User> findAll();
}
IUserService
package com.lzp.mybatis.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.lzp.mybatis.entity.User;
import java.util.List;
public interface IUserService extends IService<User> {
List<User> findAll();
}
UserServiceImpl
package com.lzp.mybatis.service.Impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzp.mybatis.entity.User;
import com.lzp.mybatis.mapper.UserMapper;
import com.lzp.mybatis.service.IUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
@Resource
private UserMapper zgqUsersMapper;
@Override
public List<User> findAll() {
//获取所有user
List<User> list=this.list();
//根据条件查询
List<User> list2=this.list(Wrappers.<User>lambdaQuery().eq(User::getId,1).eq(User::getUname,"a"));
//获取list中 User的id属性 转为集合
List<Integer> list3=list.stream().map(User::getId).collect(Collectors.toList());
List<User> list5 = list.stream().filter(i -> i.getUname().contains("a")).collect(Collectors.toList());
List<User> list4=baseMapper.selectList(Wrappers.<User>lambdaQuery().in(User::getId,list3));
User user=new User();
user.setUname("d");
user.setUpwd("2");
boolean i=this.update(user,Wrappers.<User>lambdaUpdate().eq(User::getId,4));
List<User> list1=zgqUsersMapper.findAll();
return list;
}
}
UserControl
package com.lzp.mybatis.controller;
import com.lzp.mybatis.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
IUserService iUserService;
@GetMapping("/test")
public String test(){
iUserService.findAll();
return "1";
}
}
详细说明:
mybatis-plus超详细讲解_zdsg45的博客-CSDN博客_mybatis-plus
最后
以上就是直率鱼为你收集整理的springboot集成mybatis-plus的全部内容,希望文章能够帮你解决springboot集成mybatis-plus所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复