MP教程-入门
Posted deepminer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MP教程-入门相关的知识,希望对你有一定的参考价值。
一、数据库
CREATE TABLE `tb_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
二、创建SpringBoot项目
2.1 Maven
<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.2.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.will.learn</groupId>
<artifactId>SpringBootMybatisPlus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootMybatisPlus</name>
<description>Demo project for Spring Boot</description>
?
<properties>
<java.version>1.8</java.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>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
</dependencies>
?
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
?
</project>
2.2 配置
-
application.properties
spring.application.name=mybatisplustest
spring.datasource.url=jdbc:mysql://192.168.0.108:3306/testdb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
?
logging.config=classpath:logback.xml
-
logback.xml
<configuration>
<!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,%i索引【从数字0开始递增】,,, -->
<!-- appender是configuration的子节点,是负责写日志的组件。 -->
<!-- ConsoleAppender:把日志输出到控制台 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %p (%file:%line)- %m%n</pattern>
<!-- 控制台也要使用UTF-8,不要使用GBK,否则会中文乱码 -->
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- RollingFileAppender:滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件 -->
<!-- 以下的大概意思是:1.先按日期存日志,日期变了,将前一天的日志文件名重命名为XXX%日期%索引,新的日志仍然是sys.log -->
<!-- 2.如果日期没有发生变化,但是当前日志的文件大小超过1KB时,对当前日志进行分割 重命名-->
<appender name="syslog"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>log/sys.log</File>
<!-- rollingPolicy:当发生滚动时,决定 RollingFileAppender 的行为,涉及文件移动和重命名。 -->
<!-- TimeBasedRollingPolicy: 最常用的滚动策略,它根据时间来制定滚动策略,既负责滚动也负责出发滚动 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 活动文件的名字会根据fileNamePattern的值,每隔一段时间改变一次 -->
<!-- 文件名:log/sys.2017-12-05.0.log -->
<fileNamePattern>log/sys.%d.%i.log</fileNamePattern>
<!-- 每产生一个日志文件,该日志文件的保存期限为30天 -->
<maxHistory>30</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- maxFileSize:这是活动文件的大小,默认值是10MB,本篇设置为1KB,只是为了演示 -->
<maxFileSize>1KB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<!-- pattern节点,用来设置日志的输入格式 -->
<pattern>
%d %p (%file:%line)- %m%n
</pattern>
<!-- 记录日志的编码 -->
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
</encoder>
</appender>
<!-- 控制台输出日志级别 -->
<root level="info">
<appender-ref ref="STDOUT" />
</root>
<!-- 指定项目中某个包,当有日志操作行为时的日志记录级别 -->
<!-- com.appley为根包,也就是只要是发生在这个根包下面的所有日志操作行为的权限都是DEBUG -->
<!-- 级别依次为【从高到低】:FATAL > ERROR > WARN > INFO > DEBUG > TRACE -->
<logger name="com.will.learn.springboot" level="DEBUG">
<appender-ref ref="syslog" />
</logger>
</configuration>
2.3 POJO
package com.will.learn.springboot.pojo;
?
import org.springframework.beans.factory.annotation.Autowired;
?
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.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
?
//设置数据库表名
"tb_user") (
public class User {
//设置主键,生成ID类型为数据库自增长
type = IdType.AUTO) (
private Integer id;
//设置列名
"user_name") (
private String name;
private String password;
private Integer age;
}
2.4 Mapper
package com.will.learn.springboot.mapper;
?
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.will.learn.springboot.pojo.User;
?
//继承BaseMapper
public interface UserMapper extends BaseMapper<User>{
?
}
?
2.5 设置Mapper扫描
package com.will.learn.springboot;
?
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
?
//设置mapper扫描路径
"com.will.learn.springboot.mapper") (
public class SpringBootMybatisPlusApplication {
?
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisPlusApplication.class, args);
}
?
}
?
三、测试
package com.will.learn.springboot;
?
import java.util.List;
?
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
?
import com.will.learn.springboot.mapper.UserMapper;
import com.will.learn.springboot.pojo.User;
?
import lombok.val;
?
class SpringBootMybatisPlusApplicationTests {
UserMapper userMapper;
void contextLoads() {
}
public void UserMapperTest() {
List<User> userList = userMapper.selectList(null);
for(val user:userList){
System.out.println(user.toString());
}
}
public void UserInsertTest() {
User user = User.builder().name("曹操").password("123fdkfjk").age(33).build();
int insert = userMapper.insert(user);
System.out.println(insert);
System.out.println(user);
}
}
?
20200314 18:32:54,024 INFO (StartupInfoLogger.java:61) Started SpringBootMybatisPlusApplicationTests in 5.053 seconds (JVM running for 7.025)
20200314 18:32:54,536 INFO (HikariDataSource.java:110) HikariPool1 Starting...
20200314 18:32:55,814 INFO (HikariDataSource.java:123) HikariPool1 Start completed.
20200314 18:32:55,830 DEBUG (BaseJdbcLogger.java:143) > Preparing: INSERT INTO tb_user ( user_name, password, age ) VALUES ( , , )
20200314 18:32:55,887 DEBUG (BaseJdbcLogger.java:143) > Parameters: 曹操(String), 123fdkfjk(String), 33(Integer)
20200314 18:32:55,906 DEBUG (BaseJdbcLogger.java:143) < Updates: 1
1
User(id3, name 曹操, password123fdkfjk, age33)
一、数据库
CREATE TABLE `tb_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
二、创建SpringBoot项目
2.1 Maven
<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.2.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.will.learn</groupId>
<artifactId>SpringBootMybatisPlus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootMybatisPlus</name>
<description>Demo project for Spring Boot</description>
?
<properties>
<java.version>1.8</java.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>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
</dependencies>
?
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
?
</project>
2.2 配置
-
application.properties
spring.application.name=mybatisplustest
spring.datasource.url=jdbc:mysql://192.168.0.108:3306/testdb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
?
logging.config=classpath:logback.xml
-
logback.xml
<configuration>
<!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,%i索引【从数字0开始递增】,,, -->
<!-- appender是configuration的子节点,是负责写日志的组件。 -->
<!-- ConsoleAppender:把日志输出到控制台 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %p (%file:%line)- %m%n</pattern>
<!-- 控制台也要使用UTF-8,不要使用GBK,否则会中文乱码 -->
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- RollingFileAppender:滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件 -->
<!-- 以下的大概意思是:1.先按日期存日志,日期变了,将前一天的日志文件名重命名为XXX%日期%索引,新的日志仍然是sys.log -->
<!-- 2.如果日期没有发生变化,但是当前日志的文件大小超过1KB时,对当前日志进行分割 重命名-->
<appender name="syslog"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>log/sys.log</File>
<!-- rollingPolicy:当发生滚动时,决定 RollingFileAppender 的行为,涉及文件移动和重命名。 -->
<!-- TimeBasedRollingPolicy: 最常用的滚动策略,它根据时间来制定滚动策略,既负责滚动也负责出发滚动 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 活动文件的名字会根据fileNamePattern的值,每隔一段时间改变一次 -->
<!-- 文件名:log/sys.2017-12-05.0.log -->
<fileNamePattern>log/sys.%d.%i.log</fileNamePattern>
<!-- 每产生一个日志文件,该日志文件的保存期限为30天 -->
<maxHistory>30</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- maxFileSize:这是活动文件的大小,默认值是10MB,本篇设置为1KB,只是为了演示 -->
<maxFileSize>1KB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<!-- pattern节点,用来设置日志的输入格式 -->
<pattern>
%d %p (%file:%line)- %m%n
</pattern>
<!-- 记录日志的编码 -->
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
</encoder>
</appender>
<!-- 控制台输出日志级别 -->
<root level="info">
<appender-ref ref="STDOUT" />
</root>
<!-- 指定项目中某个包,当有日志操作行为时的日志记录级别 -->
<!-- com.appley为根包,也就是只要是发生在这个根包下面的所有日志操作行为的权限都是DEBUG -->
<!-- 级别依次为【从高到低】:FATAL > ERROR > WARN > INFO > DEBUG > TRACE -->
<logger name="com.will.learn.springboot" level="DEBUG">
<appender-ref ref="syslog" />
</logger>
</configuration>
2.3 POJO
package com.will.learn.springboot.pojo;
?
import org.springframework.beans.factory.annotation.Autowired;
?
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.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
?
//设置数据库表名
"tb_user") (
public class User {
//设置主键,生成ID类型为数据库自增长
type = IdType.AUTO) (
private Integer id;
//设置列名
"user_name") (
private String name;
private String password;
private Integer age;
}
2.4 Mapper
package com.will.learn.springboot.mapper;
?
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.will.learn.springboot.pojo.User;
?
//继承BaseMapper
public interface UserMapper extends BaseMapper<User>{
?
}
?
2.5 设置Mapper扫描
package com.will.learn.springboot;
?
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
?
//设置mapper扫描路径
"com.will.learn.springboot.mapper") (
public class SpringBootMybatisPlusApplication {
?
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisPlusApplication.class, args);
}
?
}
?
三、测试
package com.will.learn.springboot;
?
import java.util.List;
?
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
?
import com.will.learn.springboot.mapper.UserMapper;
import com.will.learn.springboot.pojo.User;
?
import lombok.val;
?
class SpringBootMybatisPlusApplicationTests {
UserMapper userMapper;
void contextLoads() {
}
public void UserMapperTest() {
List<User> userList = userMapper.selectList(null);
for(val user:userList){
System.out.println(user.toString());
}
}
public void UserInsertTest() {
User user = User.builder().name("曹操").password("123fdkfjk").age(33).build();
int insert = userMapper.insert(user);
System.out.println(insert);
System.out.println(user);
}
}
?
20200314 18:32:54,024 INFO (StartupInfoLogger.java:61) Started SpringBootMybatisPlusApplicationTests in 5.053 seconds (JVM running for 7.025)
20200314 18:32:54,536 INFO (HikariDataSource.java:110) HikariPool1 Starting...
20200314 18:32:55,814 INFO (HikariDataSource.java:123) HikariPool1 Start completed.
20200314 18:32:55,830 DEBUG (BaseJdbcLogger.java:143) > Preparing: INSERT INTO tb_user ( user_name, password, age ) VALUES ( , , )
20200314 18:32:55,887 DEBUG (BaseJdbcLogger.java:143) > Parameters: 曹操(String), 123fdkfjk(String), 33(Integer)
20200314 18:32:55,906 DEBUG (BaseJdbcLogger.java:143) < Updates: 1
1
User(id3, name 曹操, password123fdkfjk, age33)
以上是关于MP教程-入门的主要内容,如果未能解决你的问题,请参考以下文章
2019入门物联网教程 物联网开发视频教程 物联网零基础教程 物联网模块教程 物联网技术教程