spring boot+mybatis-plus

最近几天公司接到一个外包项目指定让我来搭建后台和数据库,当时自己就虚了,心里想着自己一直都是在项目里面直接敲代码的还从来没有自己来搭建项目环境。让我有点难搞。
心想之前自己对springboot+mybatis-plus比较熟悉,就尝试在网上查资料准备开始搭建的时候,我发现网上的资料太多又很复杂,光是内容一眼看下去就不想在看了。

花了半天的时间给大家 整理的最简单的springboot整合mybatis-plus

这个整合非常的简单在此大家可以借鉴一下,并清楚其原理。如何还有更加复杂的业务完全可以在此代码扩展或更改!

有的人可能在pom文件里具体需要哪些可能jar可能不清楚这里我给大家贴出来我的,大家也可以借鉴一下

<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.shop</groupId>
	<artifactId>shop</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>shop</name>
	<description>xxxxx</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<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>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		
		
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- mybatisPlus 核心库 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- 引入阿里数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>
        
      		
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

我的项目结构
spring boot+mybatis-plus_第1张图片

springboot在集成mybatis-plus是非常简单的其中最主要的部分就是下面这块

spring:
  #数据库配置
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/dyhxx?useUnicode=true&useSSL=false&characterEncoding=utf-8&serverTimezone=UTC
    username: 
    password: 
    # 使用druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    
#mybatis-plusl的配置
mybatis-plus:
  configuration:
  #用来映射数据库字段与java实体类的驼峰是否启用的(如果启用类似	  #ss_as对应ssAs)
    map-underscore-to-camel-case: false
    auto-mapping-behavior: full
    #用来输出sql语句执行过程打印到控制台
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

剩下其他的部分就是业务逻辑代码的实现
实体类(自己实现以下属性的get和set的方法)

@TableName("system_new")
public class SystemNew implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@TableId
	private int sn_id;//
	private Date sn_time;//
	private String sn_content;//
	private int sn_aid;//
	private String sn_aname;//
	private String sn_aphone;//
	private int sn_mid;//
	private int sn_type;//

mapper
继承BaseMapper并对应相应的实体类会继承基础的mybatis-plus的CRUD(意思就是基本的增删改查)如果比较复杂自行添加mapper的xml文件对应上就可以

public interface SystemNewMapper extends BaseMapper<SystemNew>{

}

service
在mybatis-plus里原本是一个 interface的service和一个class的serviceImpl的。我个人比较懒就直接忽略那一层了

@Service
public class SystemNewService extends ServiceImpl<SystemNewMapper, SystemNew>{

	
	
	public SystemNew getxhjs() {
		// TODO Auto-generated method stub
		return baseMapper.selectById(13);
	}

}

controller
这里面有好几个查询结果我自己是用来测试用的。

@RestController
public class SystemNewController {
	
	@Autowired
	private SystemNewService systemnewservice;
	
	@RequestMapping("/list")
	public List djw() {
		int i = systemnewservice.count();
		List list = systemnewservice.list();
		SystemNew systemnew = systemnewservice.getxhjs();
		System.out.println(systemnew+"==============");
		return list ;
		
	}

}

你以为这就完了吧还差一步呢!!!哈哈

因为我用的是注解方式的来写的项目所有需要项目在启动时扫包具体创建bean

@SpringBootApplication
@ComponentScan(basePackages = {"com.shop"})
@MapperScan("com.shop.mapper")
public class ShopApplication {

	public static void main(String[] args) {
		SpringApplication.run(ShopApplication.class, args);
	}

}

在此就全部结束看完是不是觉得很简单呢!
这个配置数据源的(我有点不是很明白是不是只要配置上来我就不用再去手写数据库连接池了呢???)
type: com.alibaba.druid.pool.DruidDataSource
我看了很多网上的搜索说的都不是很清楚个人感觉比较浅显

因为时间比较短,我有的部分还没有理解希望大家多多指点一下哪里有问题!!非常感谢大家!

可以加入群里交流

spring boot+mybatis-plus_第2张图片

你可能感兴趣的:(mysql,数据库,eclipse,mybatis)