Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。这是官方给的定义,关于mybatis-plus的更多介绍及特性,可以参考mybatis-plus官网。那么它是怎么增强的呢?其实就是它已经封装好了一些crud方法,我们不需要再写xml了,直接调用这些方法就行,就类似于JPA。
create database if not exists javaweb;
CREATE TABLE `account` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id主键,自增',
`name` varchar(32) DEFAULT NULL,
`password` varchar(50) NOT NULL COMMENT '密码',
`createTime` datetime DEFAULT CURRENT_TIMESTAMP,
`status` int(11) DEFAULT '0',
`type` int(11) DEFAULT NULL,
`hobby` varchar(128) DEFAULT NULL,
`signature` varchar(128) DEFAULT NULL,
`age` int(4) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8 COMMENT='用户表';
配置application.properties文件:
server.port=8080
#数据库连接的用户名
spring.datasource.username=root
#数据库连接的密码
spring.datasource.password=root
#数据库连接的URL
spring.datasource.url=jdbc:mysql://localhost:3306/javaweb?serverTimezone=UTC
#数据库连接的驱动 MySQL8使用com.mysql.cj.jdbc.Driver ,mysql5使用com.mysql.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#配置日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
package com.moyisuiying.booksystem.entity;
import lombok.*;
import org.springframework.stereotype.Repository;
/**
* Classname:Account
*
* @description:用户实体类
* @author: 陌意随影
* @Date: 2020-11-24 22:53
* @Version: 1.0
**/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Account {
private int id;
private String name;
private String password;
}
package com.moyisuiying.booksystem.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.moyisuiying.booksystem.entity.Account;
import org.springframework.stereotype.Repository;
/**
* Classname:AccountDao
*
* @description:
* @author: 陌意随影
* @Date: 2020-11-24 22:57
* @Version: 1.0
**/
@Repository
public interface AccountDao extends BaseMapper<Account> {
}
package com.moyisuiying.booksystem;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.moyisuiying.booksystem.dao")
public class BooksystemApplication {
public static void main(String[] args) {
SpringApplication.run(BooksystemApplication.class, args);
}
}
package com.moyisuiying.booksystem;
import com.moyisuiying.booksystem.dao.AccountDao;
import com.moyisuiying.booksystem.entity.Account;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class BooksystemApplicationTests {
@Autowired
AccountDao accountDao;
@Test
public void testFindAll(){
List<Account> accountList = accountDao.selectList(null);
accountList.forEach(System.out::println);
}
}
可见测试访问查找所有成功。