第一步引入依赖:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>springbootmybatisplus</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
第二步:设置数据库信息:
spring.datasource.username=root spring.datasource.password=123456 spring.datasource.url=jdbc:mysql://localhost:3306/mysql8?characterEncoding=utf8 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
数据库信息
第三步:创建实体类:
package org.example.Entity; import com.baomidou.mybatisplus.annotation.TableName; @TableName("users") //不一致时,需要映射 public class MyUser { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "MyUser{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
第四步:常见mapper文件:
package org.example.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; import org.example.Entity.MyUser; @Mapper public interface UserMapper extends BaseMapper<MyUser> { //所有的CRUD都已经完成 //不需要像以前一样配置一大堆文件:pojo-dao(连接mybatis,配置mapper.xml文件)==>service-controller }
第五步:创建业务层以及实现接口:
package org.example.service; import org.example.Entity.MyUser; import java.util.List; public interface MyUserService { // 查询所有用户 List<MyUser> findAll(); }
package org.example.service.Impl; import org.example.Entity.MyUser; import org.example.mapper.UserMapper; import org.example.service.MyUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class MyUserServiceImpl implements MyUserService { //继承了BaseMapper所有的方法,可以编写自己的扩展方法 @Autowired private UserMapper userMapper; @Override public List<MyUser> findAll() { return userMapper.selectList(null); } }
第六步:控制器接口:
package org.example.controller; import org.example.Entity.MyUser; import org.example.service.MyUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController public class MyUserController { @Autowired private MyUserService myUserService; @GetMapping(value = "/1") public List<MyUser> home1() { // 查询所有用户信息 List<MyUser> users = myUserService.findAll(); for (MyUser user : users) { System.out.println(user.getId()+" "+user.getName()+" "+user.getAge()); } return users; } }
第七步:运行主程序:
package org.example; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("org.example.mapper") public class HelloWorldApplication { public static void main(String[] args) { SpringApplication.run(HelloWorldApplication.class, args); } }
实现如下:
标签:spring,boot,example,plus,org,import,public From: https://www.cnblogs.com/xiaobaibailongma/p/17091309.html