由于日常需要,这里给大家写了一个MybatisPlus的Demo案例,用于学习用。话不多说,直接上代码:
第一步,创建一个SpringBoot的基础项目,项目结构如下:
第二步,编写application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: yourpassword
main:
# 解决升级Spring Boot2.6后,因依赖循环引用导致启动时报错的问题
allow-circular-references: true
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 默认日志
mapper-locations: classpath:/mapper/**/*.xml
第三步,编写实体类:
package com.example.mybatisplusdemo.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@TableName("person")
public class Person {
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
@TableField(value = "personName")
private String personName;
@TableField(value = "personEmail")
private String personEmail;
@TableField(value = "personPhone")
private String personPhone;
@TableField(value = "contactText")
private String contactText;
}
第四步,编写Mapper接口:
package com.example.mybatisplusdemo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.mybatisplusdemo.pojo.Person;
import org.springframework.stereotype.Repository;
@Repository
public interface PersonMapper extends BaseMapper<Person> {
}
第五步,编写业务逻辑层接口及实现类:
1)接口:
package com.example.mybatisplusdemo.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.mybatisplusdemo.pojo.Person;
public interface PersonService extends IService<Person> {
}
2)实现类:
package com.example.mybatisplusdemo.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatisplusdemo.mapper.PersonMapper;
import com.example.mybatisplusdemo.pojo.Person;
import com.example.mybatisplusdemo.service.PersonService;
import org.springframework.stereotype.Service;
@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements PersonService {
}
第六步,编写控制层:
package com.example.mybatisplusdemo.controller;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.mybatisplusdemo.mapper.PersonMapper;
import com.example.mybatisplusdemo.pojo.Person;
import com.example.mybatisplusdemo.service.PersonService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/")
public class MyTest {
//这里的userMapper飘红了,我们可以在mapper.UserMapper添加注解(Repository)
@Autowired
private PersonService personService;
/**
* mybatisPlusDemo
*/
@RequestMapping("/person")
public void testSelect() {
// Page pageInfo = PageHelper.startPage(1, 2);
//添加
// Person person = new Person();
// person.setPersonEmail("123@123");
// person.setContactText("test");
// person.setPersonName("xingming");
// person.setPersonPhone("123456789");
// personService.save(person);
//修改
// Person person = new Person();
// person.setPersonEmail("456@456");
// UpdateWrapper<Person> updateWrapper = new UpdateWrapper();
// updateWrapper.eq("id",19);
// personService.update(person,updateWrapper);
//删除
// personService.removeById(19);
//查询
List<Person> userList = personService.list();
userList.forEach(System.out::println);
}
}
第七步,配置启动类:
package com.example.mybatisplusdemo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.mybatisplusdemo.mapper")
public class MybatisplusdemoApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisplusdemoApplication.class, args);
}
}
附,pom.xml配置文件:
<?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.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>mybatisplusdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatisplusdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<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>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mybatisplus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
<!-- 指向静态资源路径 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>