Springboot连接Sqlserver数据库整合mybaitsplus[数据库教程]

database

  1. 依赖下载:登录网址 https://mvnrepository.com/

所有依赖包都可以搜索直接复制依赖

  1. Yml 文件数据库配置连接串

mybatis-plus:

mapper-locations: classpath*:/mapper/**Mapper.xml

# global-config:

# db-config:

# id-type: auto

# table-underline: true

# logic-not-delete-value: 0

# logic-delete-value: 1

spring:

datasource:

driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver

url: jdbc:sqlserver://ip:30513;databaseName=dy

username: abc

password: 123

application:

name: microservice-provider

server:

port: 8002

  1. 完整pom如下

<?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.2.5.RELEASE</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<groupId>com.jwt</groupId>

<artifactId>jwt</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>jwt</name>

<description>Demo project for Spring Boot</description>

<properties>

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

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!-- Jwt 依赖-->

<dependency>

<groupId>com.auth0</groupId>

<artifactId>java-jwt</artifactId>

<version>3.14.0</version>

</dependency>

<!-- Mybatis Plus-->

<!--mp-->

<dependency>

<groupId>com.baomidou</groupId>

<artifactId>mybatis-plus-boot-starter</artifactId>

<version>3.2.0</version>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-freemarker</artifactId>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<scope>runtime</scope>

</dependency>

<!--mp代码生成器-->

<dependency>

<groupId>com.baomidou</groupId>

<artifactId>mybatis-plus-generator</artifactId>

<version>3.2.0</version>

</dependency>

<!-- <dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>5.2.3.RELEASE</version>

</dependency>-->

<!-- lombook -->

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<version>1.18.12</version>

</dependency>

<!-- druid-->

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid</artifactId>

<version>1.1.24</version>

</dependency>

<!-- sqlserver-->

<dependency>

<groupId>com.microsoft.sqlserver</groupId>

<artifactId>sqljdbc4</artifactId>

<version>4.0.0</version>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<!-- <scope>test</scope>-->

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>

  1. 自动生成entiy controller service 帮助类

package com.jwt.jwt;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;

import com.baomidou.mybatisplus.core.toolkit.StringPool;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import com.baomidou.mybatisplus.generator.AutoGenerator;

import com.baomidou.mybatisplus.generator.InjectionConfig;

import com.baomidou.mybatisplus.generator.config.*;

import com.baomidou.mybatisplus.generator.config.po.TableInfo;

import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

/**

*

*

* 执行 main 方法控制台输入模块表名回车自动生成对应项目目录中

*/

public class CodeGenerator {

/**

* <p>

* 读取控制台内容

* </p>

*/

public static String scanner(String tip) {

Scanner scanner = new Scanner(System.in);

StringBuilder help = new StringBuilder();

help.append("请输入" + tip + ":");

System.out.println(help.toString());

if (scanner.hasNext()) {

String ipt = scanner.next();

if (StringUtils.isNotEmpty(ipt)) {

return ipt;

}

}

throw new MybatisPlusException("请输入正确的" + tip + "!");

}

public static void main(String[] args) {

// 代码生成器

AutoGenerator mpg = new AutoGenerator();

// 全局配置

GlobalConfig gc = new GlobalConfig();

final String projectPath = System.getProperty("user.dir");

gc.setOutputDir(projectPath + "/src/main/java");

// gc.setOutputDir("D: est");

gc.setAuthor("ken");

gc.setOpen(false);

// gc.setSwagger2(true); 实体属性 Swagger2 注解

gc.setServiceName("%sService");

mpg.setGlobalConfig(gc);

// 数据源配置

DataSourceConfig dsc = new DataSourceConfig();

dsc.setUrl("jdbc:sqlserver://ip:30513;databaseName=dy");

// dsc.setSchemaName("public");

dsc.setDriverName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

dsc.setUsername("xxx");

dsc.setPassword("xxx");

mpg.setDataSource(dsc);

// 包配置

PackageConfig pc = new PackageConfig();

pc.setModuleName(null);

pc.setParent("com.jwt.jwt");

mpg.setPackageInfo(pc);

// 自定义配置

InjectionConfig cfg = new InjectionConfig() {

@Override

public void initMap() {

// to do nothing

}

};

// 如果模板引擎是 freemarker

String templatePath = "/templates/mapper.xml.ftl";

// 如果模板引擎是 velocity

// String templatePath = "/templates/mapper.xml.vm";

// 自定义输出配置

List<FileOutConfig> focList = new ArrayList<>();

// 自定义配置会被优先输出

focList.add(new FileOutConfig(templatePath) {

@Override

public String outputFile(TableInfo tableInfo) {

// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!

return projectPath + "/src/main/resources/mapper/"

+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;

}

});

cfg.setFileOutConfigList(focList);

mpg.setCfg(cfg);

// 配置模板

TemplateConfig templateConfig = new TemplateConfig();

templateConfig.setXml(null);

mpg.setTemplate(templateConfig);

// 策略配置

StrategyConfig strategy = new StrategyConfig();

strategy.setNaming(NamingStrategy.underline_to_camel);

strategy.setColumnNaming(NamingStrategy.underline_to_camel);

strategy.setEntityLombokModel(true);

strategy.setRestControllerStyle(true);

strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));

strategy.setControllerMappingHyphenStyle(true);

strategy.setTablePrefix("m_");

mpg.setStrategy(strategy);

mpg.setTemplateEngine(new FreemarkerTemplateEngine());

mpg.execute();

}

}

Springboot 连接Sqlserver 数据库 整合mybaits plus

原文:https://www.cnblogs.com/Ken2018/p/15241359.html

以上是 Springboot连接Sqlserver数据库整合mybaitsplus[数据库教程] 的全部内容, 来源链接: utcz.com/z/535918.html

回到顶部