文章目錄
mybatis-plus
一、建庫建表
具體操作不詳細說,創建一個測試的T_USER表
二、引入依賴
<?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>hzsh-cost-account</groupId>
<artifactId>hzsh-cost-account</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hzsh-cost-account</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<mybatis-plus-boot-starter.version>3.0-RC3</mybatis-plus-boot-starter.version>
<HikariCP.version>3.2.0</HikariCP.version>
</properties>
<!-- spring boot starter 父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- mybatis-plus begin -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus-boot-starter.version}</version>
</dependency>
<!-- mybatis-plus end -->
<!-- 用於簡化代碼 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- Oracle數據依賴 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
<!-- 數據庫連接,spring-boot 2.X 默認使用 HikariCP 連接池 -->
<!-- mybatis-plus-boot-starter 中已存在可以不加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
-->
<!--web場景啓動器,包含 Tomcat 和 spring-mvc restful aop jackjson支持。 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.hzsh.HzshCostAccountApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
三、配置
創建application.yml文件
3.1 數據源配置
3.1.1 mysql
3.1.2 oracle
datasource:
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:@10.152.71.177:1521/dbname
username: root
password: root
注意:ORACLE的表名和字段名要全部大寫,否則會出現表名或字段名無效。
四、編碼
4.1 Entity
package com.hzsh.module.user.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.time.LocalDateTime;
//使用 Lombok 簡化代碼
@TableName("T_USER")
@Data
public class User {
//主鍵
private Long id;
//姓名
private String name;
//年齡
private Integer age;
//郵箱
private String email;
//創建時間
private LocalDateTime createTime;
}
4.2 Mapper
package com.hzsh.module.user.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hzsh.module.user.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
4.3 SpringBoot啓動類
package com.hzsh;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//掃描 Mapper 文件
@MapperScan("com.hzsh.module.*.mapper")
public class HzshCostAccountApplication {
public static void main(String[] args) {
SpringApplication.run(HzshCostAccountApplication.class, args);
}
}
五、測試
5.1 代碼
package com.hzsh.module.user;
import com.hzsh.module.user.mapper.UserMapper;
import com.hzsh.module.user.entity.User;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SimpleTest {
@Autowired
UserMapper userMapper;
@Test
public void test(){
List<User> users = userMapper.selectList(null);
Assert.assertEquals(2, users.size());
users.forEach(System.out::println);
}
}
運行結果
測試成功!