mybatis-plus搭建
新建一个spring-boot项目
如果慢的话,可以更改custom为https://start.aliyun.com/
勾选这些选项
配置文件
pom.xml添加mybatis-plus依赖
切记别写错了!!!
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
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>
<groupId>com.example</groupId>
<artifactId>p3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>p3</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.example.p3.P3Application</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
目录架构如下
![](file://C:\Users\asus\AppData\Roaming\marktext\images\2021-01-20-01-04-38-image.png)
先创建student实体类
student.java
package com.example.p4.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Table;
import lombok.Data;
/**
* @TableName student
*/
@Table(name="student")
@Data
public class Student implements Serializable {
/**
*
*
* @mbg.generated Wed Jan 20 00:44:50 CST 2021
*/
@Column(name = "SId")
private String sid;
/**
*
*
* @mbg.generated Wed Jan 20 00:44:50 CST 2021
*/
@Column(name = "Sname")
private String sname;
/**
*
*
* @mbg.generated Wed Jan 20 00:44:50 CST 2021
*/
@Column(name = "Sage")
private Date sage;
/**
*
*
* @mbg.generated Wed Jan 20 00:44:50 CST 2021
*/
@Column(name = "Ssex")
private String ssex;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table student
*
* @mbg.generated Wed Jan 20 00:44:50 CST 2021
*/
private static final long serialVersionUID = 1L;
}
dao层
studentDao.java
package com.example.p4.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.p4.entity.Student;
import org.apache.ibatis.annotations.Mapper;
/**
* @Entity com.example.p4.entity.Student
*/
@Mapper
public interface StudentDao extends BaseMapper<Student> {
}
service层接口
StudentService.java
package com.example.p4.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.p4.entity.Student;
public interface StudentService extends IService<Student> {
}
实现层
StudentServiceImpl
package com.example.p4.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.p4.dao.StudentDao;
import com.example.p4.entity.Student;
import com.example.p4.service.StudentService;
import org.springframework.stereotype.Service;
@Service("StudentService")
public class StudentServiceImpl extends ServiceImpl<StudentDao, Student> implements StudentService {
}
mapper.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.example.p4.dao.StudentDao">
<resultMap id="BaseResultMap" type="com.example.p4.entity.Student">
<!--@mbg.generated-->
<result column="SId" jdbcType="VARCHAR" property="sid" />
<result column="Sname" jdbcType="VARCHAR" property="sname" />
<result column="Sage" jdbcType="TIMESTAMP" property="sage" />
<result column="Ssex" jdbcType="VARCHAR" property="ssex" />
</resultMap>
</mapper>
控制层
StudentController.java
package com.example.p4.controller;
import com.example.p4.entity.Student;
import com.example.p4.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class StudentController {
@Autowired
StudentService studentService;
@RequestMapping("/test")
public List<Student> test(){
return studentService.list();
}
}