因爲一直使用的是mybatis,並且以前使用的是一個叫tk.mapper的框架,而如今mybatis-plus這個框架比較火,就來學習一下;html
個人環境java
這是使用的mybatis-plus官網的數據結構及腳本mysql
DROP TABLE IF EXISTS user;
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主鍵ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年齡',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '郵箱',
PRIMARY KEY (id)
);
複製代碼
DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
複製代碼
pom文件導包,這裏我使用了druid;也能夠不用;注意,不用導入mybatis的包,和mybatis-spring的包;git
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<!-- druid的starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
複製代碼
MybatisPageConfig
代碼: 這是mybatis的配置類,這裏主要寫了mybatis-plus的分頁配置;若是不配置分頁,能夠將mapperscan註解寫到項目的啓動類上面去,將該類刪除;這裏爲了方便閱讀,就寫在一塊兒了;@Configuration
@MapperScan("com.example.demo.mapper")
public class MybatisPageConfig {
/** * mybatis-plus分頁插件<br> */
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}
複製代碼
UserMapper
代碼:public interface UserMapper extends BaseMapper<User> {
}
複製代碼
User
代碼:@TableName("user")
public class User implements Serializable {
private static final long serialVersionUID = 8268426372054408603L;
@TableId("id")
private Long id;
@TableField("name")
private String name;
@TableField("age")
private Integer age;
@TableField("email")
private String email;
}
複製代碼
配置文件,一下是個人基本配置,其中mybatis-plus還有不少配置,這裏就不貼出來了;github
其餘的說明一下:spring
我把配置中的驅動給註釋了,由於我在pom中沒有指定數據庫鏈接的版本,因此引入的是最新的8.0.13版本;而這個最新的版本結構圖以下:sql
再來一張5.0版本的結構圖:數據庫
若是引入的是高版本的MySQL鏈接,而配置中也寫了驅動,會報這麼一個提示:springboot
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.把驅動的配置註釋就好了;由於新版本會自動幫咱們選擇哪個驅動bash
spring:
datasource:
druid:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: ***
password: ****
# driver-class-name: com.mysql.jdbc.Driver
initial-size: 5
max-active: 30
min-idle: 2
max-wait: 1234
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 5
# driver-class-name: com.mysql.jdbc.Driver
mybatis-plus:
# config-location: classpath:/mapper/*.xml
type-aliases-package: com.example.demo.model
複製代碼
這裏我把config-location屬性也註釋掉了,由於個人項目在該路徑下邊沒有xml文件,就註釋了,否則會報錯,找不到xml文件;
DemoApplicationTests
代碼:
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Resource
private UserMapper userMapper;
@Test
public void contextLoads() {
User user=new User();
user.setName("jack");
//條件查詢
System.out.println("條件查詢");
List<User> userList = userMapper.selectList(new QueryWrapper<>(user));
userList.forEach(one-> System.out.println(one.toString()));
//分頁
Page page=new Page(3,2);
IPage iPage = userMapper.selectPage(page, null);
List records = iPage.getRecords();
System.out.println("分頁");
records.forEach(one-> System.out.println(one.toString()));
}
}
複製代碼
結果:
條件查詢
User{id=2, name='Jack', age=20, email='test2@baomidou.com'}
分頁
User{id=5, name='Billie', age=24, email='test5@baomidou.com'}
複製代碼
本篇文章主要寫入門配置,後續主要寫一篇mybatis-plus的一些其餘的屬性的配置的文章