作者 丁雄超

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

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="studySpringBoot01" />
</profile>
</annotationProcessing>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="studySpringBoot01" options="-parameters" />
</option>
</component>
</project>
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="http://maven.aliyun.com/nexus/content/groups/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="5baa27a9-5c44-4760-a914-7f154035883c" name="变更" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Interface" />
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="MavenImportPreferences">
<option name="generalSettings">
<MavenGeneralSettings>
<option name="mavenHome" value="D:/AppGallery/apache-maven-3.6.1" />
<option name="useMavenConfig" value="true" />
<option name="userSettingsFile" value="D:\AppGallery\apache-maven-3.6.1\conf\settings.xml" />
</MavenGeneralSettings>
</option>
</component>
<component name="ProjectId" id="2NZrfK42mofGAeRd6Jxw9GYLlq7" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="showLibraryContents" value="true" />
<option name="showMembers" value="true" />
<option name="showVisibilityIcons" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RequestMappingsPanelOrder0": "0",
"RequestMappingsPanelOrder1": "1",
"RequestMappingsPanelWidth0": "75",
"RequestMappingsPanelWidth1": "75",
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"WebServerToolWindowFactoryState": "false",
"last_opened_file_path": "C:/Users/Ding/IdeaProjects/studySpringBoot01",
"project.structure.last.edited": "项目",
"project.structure.proportion": "0.0",
"project.structure.side.proportion": "0.2",
"settings.editor.selected.configurable": "MavenSettings"
}
}]]></component>
<component name="RunManager" selected="JUnit.StudySpringBoot01ApplicationTests.save">
<configuration name="StudySpringBoot01ApplicationTests.deleteByIds" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="studySpringBoot01" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.example.studyspringboot01.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="com.example.studyspringboot01" />
<option name="MAIN_CLASS_NAME" value="com.example.studyspringboot01.StudySpringBoot01ApplicationTests" />
<option name="METHOD_NAME" value="deleteByIds" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="StudySpringBoot01ApplicationTests.page" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="studySpringBoot01" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.example.studyspringboot01.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="com.example.studyspringboot01" />
<option name="MAIN_CLASS_NAME" value="com.example.studyspringboot01.StudySpringBoot01ApplicationTests" />
<option name="METHOD_NAME" value="page" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="StudySpringBoot01ApplicationTests.save" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="studySpringBoot01" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.example.studyspringboot01.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="com.example.studyspringboot01" />
<option name="MAIN_CLASS_NAME" value="com.example.studyspringboot01.StudySpringBoot01ApplicationTests" />
<option name="METHOD_NAME" value="save" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="StudySpringBoot01ApplicationTests.selectById" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="studySpringBoot01" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.example.studyspringboot01.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="com.example.studyspringboot01" />
<option name="MAIN_CLASS_NAME" value="com.example.studyspringboot01.StudySpringBoot01ApplicationTests" />
<option name="METHOD_NAME" value="selectById" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="StudySpringBoot01ApplicationTests.update" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="studySpringBoot01" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.example.studyspringboot01.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="com.example.studyspringboot01" />
<option name="MAIN_CLASS_NAME" value="com.example.studyspringboot01.StudySpringBoot01ApplicationTests" />
<option name="METHOD_NAME" value="update" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="StudySpringBoot01Application" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot">
<module name="studySpringBoot01" />
<option name="SPRING_BOOT_MAIN_CLASS" value="com.example.studyspringboot01.StudySpringBoot01Application" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="JUnit.StudySpringBoot01ApplicationTests.save" />
<item itemvalue="JUnit.StudySpringBoot01ApplicationTests.deleteByIds" />
<item itemvalue="JUnit.StudySpringBoot01ApplicationTests.page" />
<item itemvalue="JUnit.StudySpringBoot01ApplicationTests.selectById" />
<item itemvalue="JUnit.StudySpringBoot01ApplicationTests.update" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="应用程序级" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="默认任务">
<changelist id="5baa27a9-5c44-4760-a914-7f154035883c" name="变更" comment="" />
<created>1679882549307</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1679882549307</updated>
<workItem from="1679882551647" duration="18261000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
</project>
\ No newline at end of file
... ...
<?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.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>studySpringBoot01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>studySpringBoot01</name>
<description>studySpringBoot01</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
... ...
package com.example.studyspringboot01;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StudySpringBoot01Application {
public static void main(String[] args) {
SpringApplication.run(StudySpringBoot01Application.class, args);
}
}
... ...
package com.example.studyspringboot01.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
//定义mybatisplus拦截器
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//添加具体的分页拦截器
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
//添加乐观锁拦截器
mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return mybatisPlusInterceptor;
}
}
... ...
package com.example.studyspringboot01.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println("id= " + id);
return "hello,springBoot程序!";
}
}
... ...
package com.example.studyspringboot01.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.studyspringboot01.domain.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserDao extends BaseMapper<User> {
}
... ...
package com.example.studyspringboot01.domain;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@TableName("tb_user")
@Data
public class User {
public Integer id;
public String username;
public String password;
//逻辑删除字段,标记当前字段是否被删除
//@TableLogic(value = "0",delval = "1") //0表示未删除,1表示已删除
public Integer deleted;
public Integer version;
}
... ...
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db1?serverTimezone=UTC
username: root
password: root
mybatis-plus:
global-config:
db-config:
logic-delete-field: deleted #??????????
logic-not-delete-value: 0 #?????0
logic-delete-value: 1 #??????1
... ...
package com.example.studyspringboot01;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.studyspringboot01.dao.UserDao;
import com.example.studyspringboot01.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class StudySpringBoot01ApplicationTests {
@Autowired
private UserDao userDao;
//查询所有
@Test
public void selectAllTest(){
List<User> userList = userDao.selectList(null);
userList.forEach(s -> System.out.println(s));
}
//新增
@Test
public void save(){
User user = new User();
user.id = 1;
user.username = "dingxiongchao";
user.password = "223344";
userDao.insert(user);
}
//删除
@Test
public void delete(){
userDao.deleteById(123);
}
//修改
@Test
public void update(){
User user = new User();
user.id = 13;
user.username = "1333";
user.password = "1343";
userDao.updateById(user);
}
//查询单个
@Test
public void selectById(){
userDao.selectById(122);
}
//分页查询
@Test
public void page(){
Page page = new Page(1,2);
Page page1 = userDao.selectPage(page, null);
System.out.println("分页查询,当前是多少页:"+ page1.getCurrent());
System.out.println("分页查询,一页能查询几条数据:"+ page1.getSize());
System.out.println("分页查询,一共多少页:"+ page1.getPages());
System.out.println("分页查询,一共多少条数据:"+ page1.getTotal());
System.out.println("分页查询,当前页显示数据:" + page1.getRecords());
}
//多选删除
@Test
public void deleteByIds(){
List<Integer> list = new ArrayList<>();
list.add(4);
list.add(5);
list.add(9);
userDao.deleteBatchIds(list);
}
}
... ...
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db1?serverTimezone=UTC
username: root
password: root
mybatis-plus:
global-config:
db-config:
logic-delete-field: deleted #??????????
logic-not-delete-value: 0 #?????0
logic-delete-value: 1 #??????1
... ...