spring boot+mybatis搭建项目

一.创建spring boot项目

1.File->New->Project


image.png

2.选择 Spring Initializr ,然后选择默认的 url 点击【Next】:


2.jpg

3.修改项目信息
3.jpg

4.选择项目的模板


spring boot+mybatis搭建项目_第1张图片
4.jpg

5.jpg

5、填写Project name 和项目的位置,然后点击Finish。
6.jpg

点击完成后生产的代码结构图:
spring boot+mybatis搭建项目_第2张图片
image.png
二:配置pom文件

因为需要集成Mybatis插入,所以需要在pom.xml中加上依赖包
所以最后的pom.xml文件内容如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.2.RELEASE
         
    
    com.example
    springbatis
    1.0.1
    springbatis
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.0
        

        
        
            com.alibaba
            druid
            1.1.11
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.0
        

        


        
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.5
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            

            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                
                    ${basedir}/src/main/resources/generator/generatorConfig.xml
                    true
                    true
                
            
        
    


其中

 
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                
                    ${basedir}/src/main/resources/generator/generatorConfig.xml
                    true
                    true
                
            

是mybatis generator 自动生成代码插件配置文件的地址;

三:配置mybatis

1.添加mybatis generator 自动生成代码插件配置文件
在resources文件加下添加generator文件夹,并创建generatorConfig.xml文件
内容如下:





    
    
    
        
            
            
            
        
        
        
        
        
            
        
        
        
            
            
        
        
        
            
        
        
        
            
        
        
        
四.配置application.yml
server:
  port: 8089

spring:
  datasource:
    mame:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/ygop_eps_commodity
    username: test
    password: test
    driver-class-name: com.mysql.jdbc.Driver
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20


#Mybatis配置
mybatis:
    mapper-locations: classpath:mybatis/mapper/**/*.xml
    type-aliases-package: com.example.springbatis.model.entities
五:设置build自动生成代码

1.Run->Edit Configurations
点击加好+


image.png

然后配置mybatis-generator信息


spring boot+mybatis搭建项目_第3张图片
8.jpg

店家Apply 和OK最后在主界面的右上角会有一个
image.png

2.自动生成代码


image.png

生成代码结构图:
image.png

到这里spring boot+mybatis搭建项目就已经完成了;

六:运行程序

1.添加注解
在SpringbatisApplication中添加注解
将mapper注入到Spring中
@MapperScan("com.example.springbatis.dto")


image.png

2.创建Controller


9.jpg

3.运行:


spring boot+mybatis搭建项目_第4张图片
image.png

你可能感兴趣的:(spring boot+mybatis搭建项目)