快速搭建springboot工程


一、创建SpringBoot的三种方式

  1. 在线创建: https://start.spring.io
  2. 在IntelliJ IDEA中选择Spring Initializr创建;或者在Eclipse中选择 New -> Spring Starter Project创建(本质于第一种创建方式相同)
  3. 通过Maven创建,选择maven-archetype-quickstart骨架并在pom文件见中添加如下依赖:

    org.springframework.boot
    spring-boot-starter-parent
    2.5.4
     
  


    
      org.springframework.boot
      spring-boot-starter-web
    

二、配置启动类

 配置App 的启动类,如下:

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

这就可以启动一个SpringBoot工程了。

三、整合MyBatis

依次添加mysql数据库、druid数据库连接池、mybatis依赖


      mysql
      mysql-connector-java
      8.0.16
    
    
    
      com.alibaba
      druid
      1.1.13
    
    
    
      org.mybatis.spring.boot
      mybatis-spring-boot-starter
      1.3.2
    

配置mybatis逆向工程插件


          org.mybatis.generator
          mybatis-generator-maven-plugin
          1.3.7
          
          
            
              org.mybatis.generator
              mybatis-generator-core
              1.3.7
            
            
            
              mysql
              mysql-connector-java
              8.0.16
            
          
          
          
          
            
            
              
              mybatis-generator
              
              package
              
              
                generate
              
            
          
          
          
            
            true
            
            true
            
            src/main/resources/mybatis-generator.xml
          
        

编写mybatis-generator.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">


        
    "DB2Tables" targetRuntime="MyBatis3">
        
            
            "suppressAllComments" value="true" />
        
        
        "com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/Ecommerce_spike?serverTimezone=Asia/Shanghai"
                        userId="root"
                        password="root" />

        
        
            "forceBigDecimals" value="false" />
        

        
        <javaModelGenerator
                targetPackage="com.miaoshaProject.Entity" targetProject="src/main/java">
            
            "enableSubPackages" value="false" />
            
            "trimStrings" value="true" />
        

        
        "mapper"
                         targetProject="src/main/resources">
            
            "enableSubPackages" value="false" />
        

        
        "XMLMAPPER"
                             targetPackage="com.miaoshaProject.Dao" targetProject="src/main/java">
            
            "enableSubPackages" value="false" />
        

        
        "user_info" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false">
"user_password" domainObjectName="Password" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false">

运行该配置文件Run->Edit Configurations...,创建一个Maven,重点是配置Command line 要与上面pox文件中

标签中的内容对应。
mybatis-generator:generate对应标签中的

 运行即可生成相应的实体类、mapper文件等。

再配置application.yml文件

server:
  port: 8080
spring:
  application:
    name: E-commerce_spike
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/Ecommerce_spike?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: root
    #配置druid数据连接池
    type: com.alibaba.druid.pool.DruidDataSource

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

注意:mapper.xml文件放在了resource目录下。

若和mapper接口放在一起,maven工程在打包时会将xml文件忽略,需要在pom文件中添加以下配置:


    
      
        src/main/java
        
          **/*.xml
        
      
      
        src/main/resources
      
    

 四、测试

@SpringBootApplication
//@EnableAutoConfiguration   表示开启自动化配置,与上面的注解异曲同工的功能
public class App 
{
    public static void main(String[] args) {SpringApplication.run(App. class,args);}

    @Resource
    private UserMapper userMapper;

    @GetMapping("/hello")
    public String Hello(){
        User user = userMapper.selectByPrimaryKey(1);
        if (user == null){
            return "对象不存在";
        }else{
            System.out.println(user.getName());
            return "yes";
        }
    }
}

访问localhost:8080/hello查看返回结果。