作者 丁雄超

搭建springboot工程项目,基于mybatisplus做了增删改查,多删,逻辑删除,分页条件查询

  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project version="4">
  3 + <component name="CompilerConfiguration">
  4 + <annotationProcessing>
  5 + <profile name="Maven default annotation processors profile" enabled="true">
  6 + <sourceOutputDir name="target/generated-sources/annotations" />
  7 + <sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
  8 + <outputRelativeToContentRoot value="true" />
  9 + <module name="studySpringBoot01" />
  10 + </profile>
  11 + </annotationProcessing>
  12 + </component>
  13 + <component name="JavacSettings">
  14 + <option name="ADDITIONAL_OPTIONS_OVERRIDE">
  15 + <module name="studySpringBoot01" options="-parameters" />
  16 + </option>
  17 + </component>
  18 +</project>
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project version="4">
  3 + <component name="Encoding">
  4 + <file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
  5 + </component>
  6 +</project>
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project version="4">
  3 + <component name="RemoteRepositoriesConfiguration">
  4 + <remote-repository>
  5 + <option name="id" value="central" />
  6 + <option name="name" value="Central Repository" />
  7 + <option name="url" value="http://maven.aliyun.com/nexus/content/groups/public/" />
  8 + </remote-repository>
  9 + <remote-repository>
  10 + <option name="id" value="central" />
  11 + <option name="name" value="Maven Central repository" />
  12 + <option name="url" value="https://repo1.maven.org/maven2" />
  13 + </remote-repository>
  14 + <remote-repository>
  15 + <option name="id" value="jboss.community" />
  16 + <option name="name" value="JBoss Community repository" />
  17 + <option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
  18 + </remote-repository>
  19 + </component>
  20 +</project>
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project version="4">
  3 + <component name="ExternalStorageConfigurationManager" enabled="true" />
  4 + <component name="MavenProjectsManager">
  5 + <option name="originalFiles">
  6 + <list>
  7 + <option value="$PROJECT_DIR$/pom.xml" />
  8 + </list>
  9 + </option>
  10 + </component>
  11 + <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
  12 + <output url="file://$PROJECT_DIR$/out" />
  13 + </component>
  14 +</project>
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project version="4">
  3 + <component name="VcsDirectoryMappings">
  4 + <mapping directory="$PROJECT_DIR$" vcs="Git" />
  5 + </component>
  6 +</project>
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project version="4">
  3 + <component name="AutoImportSettings">
  4 + <option name="autoReloadType" value="SELECTIVE" />
  5 + </component>
  6 + <component name="ChangeListManager">
  7 + <list default="true" id="5baa27a9-5c44-4760-a914-7f154035883c" name="变更" comment="" />
  8 + <option name="SHOW_DIALOG" value="false" />
  9 + <option name="HIGHLIGHT_CONFLICTS" value="true" />
  10 + <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
  11 + <option name="LAST_RESOLUTION" value="IGNORE" />
  12 + </component>
  13 + <component name="FileTemplateManagerImpl">
  14 + <option name="RECENT_TEMPLATES">
  15 + <list>
  16 + <option value="Interface" />
  17 + <option value="Class" />
  18 + </list>
  19 + </option>
  20 + </component>
  21 + <component name="Git.Settings">
  22 + <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
  23 + </component>
  24 + <component name="MarkdownSettingsMigration">
  25 + <option name="stateVersion" value="1" />
  26 + </component>
  27 + <component name="MavenImportPreferences">
  28 + <option name="generalSettings">
  29 + <MavenGeneralSettings>
  30 + <option name="mavenHome" value="D:/AppGallery/apache-maven-3.6.1" />
  31 + <option name="useMavenConfig" value="true" />
  32 + <option name="userSettingsFile" value="D:\AppGallery\apache-maven-3.6.1\conf\settings.xml" />
  33 + </MavenGeneralSettings>
  34 + </option>
  35 + </component>
  36 + <component name="ProjectId" id="2NZrfK42mofGAeRd6Jxw9GYLlq7" />
  37 + <component name="ProjectLevelVcsManager" settingsEditedManually="true" />
  38 + <component name="ProjectViewState">
  39 + <option name="showLibraryContents" value="true" />
  40 + <option name="showMembers" value="true" />
  41 + <option name="showVisibilityIcons" value="true" />
  42 + </component>
  43 + <component name="PropertiesComponent"><![CDATA[{
  44 + "keyToString": {
  45 + "RequestMappingsPanelOrder0": "0",
  46 + "RequestMappingsPanelOrder1": "1",
  47 + "RequestMappingsPanelWidth0": "75",
  48 + "RequestMappingsPanelWidth1": "75",
  49 + "RunOnceActivity.OpenProjectViewOnStart": "true",
  50 + "RunOnceActivity.ShowReadmeOnStart": "true",
  51 + "WebServerToolWindowFactoryState": "false",
  52 + "last_opened_file_path": "C:/Users/Ding/IdeaProjects/studySpringBoot01",
  53 + "project.structure.last.edited": "项目",
  54 + "project.structure.proportion": "0.0",
  55 + "project.structure.side.proportion": "0.2",
  56 + "settings.editor.selected.configurable": "MavenSettings"
  57 + }
  58 +}]]></component>
  59 + <component name="RunManager" selected="JUnit.StudySpringBoot01ApplicationTests.save">
  60 + <configuration name="StudySpringBoot01ApplicationTests.deleteByIds" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
  61 + <module name="studySpringBoot01" />
  62 + <extension name="coverage">
  63 + <pattern>
  64 + <option name="PATTERN" value="com.example.studyspringboot01.*" />
  65 + <option name="ENABLED" value="true" />
  66 + </pattern>
  67 + </extension>
  68 + <option name="PACKAGE_NAME" value="com.example.studyspringboot01" />
  69 + <option name="MAIN_CLASS_NAME" value="com.example.studyspringboot01.StudySpringBoot01ApplicationTests" />
  70 + <option name="METHOD_NAME" value="deleteByIds" />
  71 + <option name="TEST_OBJECT" value="method" />
  72 + <method v="2">
  73 + <option name="Make" enabled="true" />
  74 + </method>
  75 + </configuration>
  76 + <configuration name="StudySpringBoot01ApplicationTests.page" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
  77 + <module name="studySpringBoot01" />
  78 + <extension name="coverage">
  79 + <pattern>
  80 + <option name="PATTERN" value="com.example.studyspringboot01.*" />
  81 + <option name="ENABLED" value="true" />
  82 + </pattern>
  83 + </extension>
  84 + <option name="PACKAGE_NAME" value="com.example.studyspringboot01" />
  85 + <option name="MAIN_CLASS_NAME" value="com.example.studyspringboot01.StudySpringBoot01ApplicationTests" />
  86 + <option name="METHOD_NAME" value="page" />
  87 + <option name="TEST_OBJECT" value="method" />
  88 + <method v="2">
  89 + <option name="Make" enabled="true" />
  90 + </method>
  91 + </configuration>
  92 + <configuration name="StudySpringBoot01ApplicationTests.save" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
  93 + <module name="studySpringBoot01" />
  94 + <extension name="coverage">
  95 + <pattern>
  96 + <option name="PATTERN" value="com.example.studyspringboot01.*" />
  97 + <option name="ENABLED" value="true" />
  98 + </pattern>
  99 + </extension>
  100 + <option name="PACKAGE_NAME" value="com.example.studyspringboot01" />
  101 + <option name="MAIN_CLASS_NAME" value="com.example.studyspringboot01.StudySpringBoot01ApplicationTests" />
  102 + <option name="METHOD_NAME" value="save" />
  103 + <option name="TEST_OBJECT" value="method" />
  104 + <method v="2">
  105 + <option name="Make" enabled="true" />
  106 + </method>
  107 + </configuration>
  108 + <configuration name="StudySpringBoot01ApplicationTests.selectById" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
  109 + <module name="studySpringBoot01" />
  110 + <extension name="coverage">
  111 + <pattern>
  112 + <option name="PATTERN" value="com.example.studyspringboot01.*" />
  113 + <option name="ENABLED" value="true" />
  114 + </pattern>
  115 + </extension>
  116 + <option name="PACKAGE_NAME" value="com.example.studyspringboot01" />
  117 + <option name="MAIN_CLASS_NAME" value="com.example.studyspringboot01.StudySpringBoot01ApplicationTests" />
  118 + <option name="METHOD_NAME" value="selectById" />
  119 + <option name="TEST_OBJECT" value="method" />
  120 + <method v="2">
  121 + <option name="Make" enabled="true" />
  122 + </method>
  123 + </configuration>
  124 + <configuration name="StudySpringBoot01ApplicationTests.update" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
  125 + <module name="studySpringBoot01" />
  126 + <extension name="coverage">
  127 + <pattern>
  128 + <option name="PATTERN" value="com.example.studyspringboot01.*" />
  129 + <option name="ENABLED" value="true" />
  130 + </pattern>
  131 + </extension>
  132 + <option name="PACKAGE_NAME" value="com.example.studyspringboot01" />
  133 + <option name="MAIN_CLASS_NAME" value="com.example.studyspringboot01.StudySpringBoot01ApplicationTests" />
  134 + <option name="METHOD_NAME" value="update" />
  135 + <option name="TEST_OBJECT" value="method" />
  136 + <method v="2">
  137 + <option name="Make" enabled="true" />
  138 + </method>
  139 + </configuration>
  140 + <configuration name="StudySpringBoot01Application" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot">
  141 + <module name="studySpringBoot01" />
  142 + <option name="SPRING_BOOT_MAIN_CLASS" value="com.example.studyspringboot01.StudySpringBoot01Application" />
  143 + <method v="2">
  144 + <option name="Make" enabled="true" />
  145 + </method>
  146 + </configuration>
  147 + <recent_temporary>
  148 + <list>
  149 + <item itemvalue="JUnit.StudySpringBoot01ApplicationTests.save" />
  150 + <item itemvalue="JUnit.StudySpringBoot01ApplicationTests.deleteByIds" />
  151 + <item itemvalue="JUnit.StudySpringBoot01ApplicationTests.page" />
  152 + <item itemvalue="JUnit.StudySpringBoot01ApplicationTests.selectById" />
  153 + <item itemvalue="JUnit.StudySpringBoot01ApplicationTests.update" />
  154 + </list>
  155 + </recent_temporary>
  156 + </component>
  157 + <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="应用程序级" UseSingleDictionary="true" transferred="true" />
  158 + <component name="TaskManager">
  159 + <task active="true" id="Default" summary="默认任务">
  160 + <changelist id="5baa27a9-5c44-4760-a914-7f154035883c" name="变更" comment="" />
  161 + <created>1679882549307</created>
  162 + <option name="number" value="Default" />
  163 + <option name="presentableId" value="Default" />
  164 + <updated>1679882549307</updated>
  165 + <workItem from="1679882551647" duration="18261000" />
  166 + </task>
  167 + <servers />
  168 + </component>
  169 + <component name="TypeScriptGeneratedFilesManager">
  170 + <option name="version" value="3" />
  171 + </component>
  172 + <component name="Vcs.Log.Tabs.Properties">
  173 + <option name="TAB_STATES">
  174 + <map>
  175 + <entry key="MAIN">
  176 + <value>
  177 + <State />
  178 + </value>
  179 + </entry>
  180 + </map>
  181 + </option>
  182 + </component>
  183 +</project>
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4 + <modelVersion>4.0.0</modelVersion>
  5 + <parent>
  6 + <groupId>org.springframework.boot</groupId>
  7 + <artifactId>spring-boot-starter-parent</artifactId>
  8 + <version>2.5.0</version>
  9 + <relativePath/> <!-- lookup parent from repository -->
  10 + </parent>
  11 + <groupId>com.example</groupId>
  12 + <artifactId>studySpringBoot01</artifactId>
  13 + <version>0.0.1-SNAPSHOT</version>
  14 + <name>studySpringBoot01</name>
  15 + <description>studySpringBoot01</description>
  16 + <properties>
  17 + <java.version>1.8</java.version>
  18 + </properties>
  19 +
  20 + <dependencies>
  21 +
  22 + <dependency>
  23 + <groupId>com.alibaba</groupId>
  24 + <artifactId>druid</artifactId>
  25 + <version>1.1.12</version>
  26 + </dependency>
  27 + <dependency>
  28 + <groupId>org.springframework.boot</groupId>
  29 + <artifactId>spring-boot-starter-web</artifactId>
  30 + </dependency>
  31 +
  32 + <dependency>
  33 + <groupId>org.springframework.boot</groupId>
  34 + <artifactId>spring-boot-starter-test</artifactId>
  35 + <scope>test</scope>
  36 + </dependency>
  37 +
  38 + <dependency>
  39 + <groupId>mysql</groupId>
  40 + <artifactId>mysql-connector-java</artifactId>
  41 + </dependency>
  42 +
  43 + <dependency>
  44 + <groupId>com.baomidou</groupId>
  45 + <artifactId>mybatis-plus-boot-starter</artifactId>
  46 + <version>3.4.0</version>
  47 + </dependency>
  48 +
  49 + <dependency>
  50 + <groupId>org.projectlombok</groupId>
  51 + <artifactId>lombok</artifactId>
  52 + </dependency>
  53 + </dependencies>
  54 +
  55 + <build>
  56 + <plugins>
  57 + <plugin>
  58 + <groupId>org.springframework.boot</groupId>
  59 + <artifactId>spring-boot-maven-plugin</artifactId>
  60 + </plugin>
  61 + </plugins>
  62 + </build>
  63 +
  64 +</project>
  1 +package com.example.studyspringboot01;
  2 +
  3 +import org.springframework.boot.SpringApplication;
  4 +import org.springframework.boot.autoconfigure.SpringBootApplication;
  5 +
  6 +@SpringBootApplication
  7 +public class StudySpringBoot01Application {
  8 +
  9 + public static void main(String[] args) {
  10 + SpringApplication.run(StudySpringBoot01Application.class, args);
  11 + }
  12 +
  13 +}
  1 +package com.example.studyspringboot01.config;
  2 +
  3 +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
  4 +import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
  5 +import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
  6 +import org.springframework.context.annotation.Bean;
  7 +import org.springframework.context.annotation.Configuration;
  8 +
  9 +@Configuration
  10 +public class MyBatisPlusConfig {
  11 +
  12 + @Bean
  13 + public MybatisPlusInterceptor mybatisPlusInterceptor(){
  14 + //定义mybatisplus拦截器
  15 + MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
  16 + //添加具体的分页拦截器
  17 + mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
  18 + //添加乐观锁拦截器
  19 + mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
  20 + return mybatisPlusInterceptor;
  21 + }
  22 +}
  1 +package com.example.studyspringboot01.controller;
  2 +
  3 +import org.springframework.web.bind.annotation.GetMapping;
  4 +import org.springframework.web.bind.annotation.PathVariable;
  5 +import org.springframework.web.bind.annotation.RequestMapping;
  6 +import org.springframework.web.bind.annotation.RestController;
  7 +
  8 +@RestController
  9 +@RequestMapping("/books")
  10 +public class BookController {
  11 +
  12 + @GetMapping("/{id}")
  13 + public String getById(@PathVariable Integer id){
  14 + System.out.println("id= " + id);
  15 + return "hello,springBoot程序!";
  16 + }
  17 +}
  1 +package com.example.studyspringboot01.dao;
  2 +
  3 +import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  4 +import com.example.studyspringboot01.domain.User;
  5 +import org.apache.ibatis.annotations.Mapper;
  6 +
  7 +@Mapper
  8 +public interface UserDao extends BaseMapper<User> {
  9 +}
  1 +package com.example.studyspringboot01.domain;
  2 +
  3 +import com.baomidou.mybatisplus.annotation.TableLogic;
  4 +import com.baomidou.mybatisplus.annotation.TableName;
  5 +import lombok.Data;
  6 +
  7 +@TableName("tb_user")
  8 +@Data
  9 +public class User {
  10 +
  11 + public Integer id;
  12 + public String username;
  13 + public String password;
  14 +
  15 + //逻辑删除字段,标记当前字段是否被删除
  16 + //@TableLogic(value = "0",delval = "1") //0表示未删除,1表示已删除
  17 + public Integer deleted;
  18 +
  19 + public Integer version;
  20 +
  21 +
  22 +}
  1 +spring:
  2 + datasource:
  3 + type: com.alibaba.druid.pool.DruidDataSource
  4 + driver-class-name: com.mysql.cj.jdbc.Driver
  5 + url: jdbc:mysql://localhost:3306/db1?serverTimezone=UTC
  6 + username: root
  7 + password: root
  8 +
  9 +
  10 +mybatis-plus:
  11 + global-config:
  12 + db-config:
  13 + logic-delete-field: deleted #??????????
  14 + logic-not-delete-value: 0 #?????0
  15 + logic-delete-value: 1 #??????1
  1 +package com.example.studyspringboot01;
  2 +
  3 +import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4 +import com.example.studyspringboot01.dao.UserDao;
  5 +import com.example.studyspringboot01.domain.User;
  6 +import org.junit.jupiter.api.Test;
  7 +import org.springframework.beans.factory.annotation.Autowired;
  8 +import org.springframework.boot.test.context.SpringBootTest;
  9 +
  10 +import java.util.ArrayList;
  11 +import java.util.List;
  12 +
  13 +@SpringBootTest
  14 +class StudySpringBoot01ApplicationTests {
  15 +
  16 + @Autowired
  17 + private UserDao userDao;
  18 +
  19 + //查询所有
  20 + @Test
  21 + public void selectAllTest(){
  22 + List<User> userList = userDao.selectList(null);
  23 + userList.forEach(s -> System.out.println(s));
  24 + }
  25 +
  26 + //新增
  27 + @Test
  28 + public void save(){
  29 + User user = new User();
  30 + user.id = 1;
  31 + user.username = "dingxiongchao";
  32 + user.password = "223344";
  33 + userDao.insert(user);
  34 + }
  35 +
  36 + //删除
  37 + @Test
  38 + public void delete(){
  39 + userDao.deleteById(123);
  40 + }
  41 +
  42 + //修改
  43 + @Test
  44 + public void update(){
  45 + User user = new User();
  46 + user.id = 13;
  47 + user.username = "1333";
  48 + user.password = "1343";
  49 + userDao.updateById(user);
  50 + }
  51 +
  52 + //查询单个
  53 + @Test
  54 + public void selectById(){
  55 + userDao.selectById(122);
  56 + }
  57 +
  58 + //分页查询
  59 + @Test
  60 + public void page(){
  61 + Page page = new Page(1,2);
  62 + Page page1 = userDao.selectPage(page, null);
  63 + System.out.println("分页查询,当前是多少页:"+ page1.getCurrent());
  64 + System.out.println("分页查询,一页能查询几条数据:"+ page1.getSize());
  65 + System.out.println("分页查询,一共多少页:"+ page1.getPages());
  66 + System.out.println("分页查询,一共多少条数据:"+ page1.getTotal());
  67 + System.out.println("分页查询,当前页显示数据:" + page1.getRecords());
  68 + }
  69 +
  70 + //多选删除
  71 + @Test
  72 + public void deleteByIds(){
  73 + List<Integer> list = new ArrayList<>();
  74 + list.add(4);
  75 + list.add(5);
  76 + list.add(9);
  77 + userDao.deleteBatchIds(list);
  78 + }
  79 +
  80 +
  81 +
  82 +
  83 +
  84 +
  85 +}
  1 +spring:
  2 + datasource:
  3 + type: com.alibaba.druid.pool.DruidDataSource
  4 + driver-class-name: com.mysql.cj.jdbc.Driver
  5 + url: jdbc:mysql://localhost:3306/db1?serverTimezone=UTC
  6 + username: root
  7 + password: root
  8 +
  9 +
  10 +mybatis-plus:
  11 + global-config:
  12 + db-config:
  13 + logic-delete-field: deleted #??????????
  14 + logic-not-delete-value: 0 #?????0
  15 + logic-delete-value: 1 #??????1