spring-boot集成mybatis-plus简单例子

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);
    }

}

运行结果

测试成功!
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章