3.SpringBoot集成SpringMVC和MyBatis

第一步

1创建SpringBoot项目

3.SpringBoot集成SpringMVC和MyBatis_第1张图片
2,填写相关信息
3.SpringBoot集成SpringMVC和MyBatis_第2张图片
3,选择框架
3.SpringBoot集成SpringMVC和MyBatis_第3张图片
4:完成项目的创建
3.SpringBoot集成SpringMVC和MyBatis_第4张图片

第二步:使用Mybatis逆向工程

1.在pom文件中加入插件

    
        org.mybatis.generator
        mybatis-generator-maven-plugin
        1.3.2
        
            true
            true
        
    

3.SpringBoot集成SpringMVC和MyBatis_第5张图片
2.把逆向工程需要的配置文件放在resources目录下

3.SpringBoot集成SpringMVC和MyBatis_第6张图片
3.修改db.properties文件中数据库的相关配置信息
3.SpringBoot集成SpringMVC和MyBatis_第7张图片

4.修改generatorConfig.xml文件中生成实体类,mapper接口,mapper映射文件的路径,以及表的名称和实体类的名称,如果已经改好了可以不用改

5.运行逆向工程
3.SpringBoot集成SpringMVC和MyBatis_第8张图片
6.运行成功后的结果如下
3.SpringBoot集成SpringMVC和MyBatis_第9张图片

第三步,mybatis配置

1.我们使用druid数据库连接池,在pom文件中加入如下依赖

		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>

2.把springboot配置文件改成yml后缀,并删除逆向工程的配置文件,如下
3.SpringBoot集成SpringMVC和MyBatis_第10张图片
3.在application.yml中配置如下信息,注意数据库的名称和密码是否正确

server:
  port: 8081
  servlet:
    context-path: /MallProj

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/auth_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
    username: root
    password: ROOT
  druid:
    initialSize: 10
    minIdle: 5
    maxActive: 20
    maxWait: 10
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 30000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    filters: stat,wall,log4j
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

4.在application.yml文件中继续添加mybatis的配置

mybatis:
  mapper-locations: classpath:mappers/*Mapper.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

在这里插入图片描述

5.在SpringBoot启动类上加注解扫描mapper接口

@MapperScan("com.bajin.mapper")

3.SpringBoot集成SpringMVC和MyBatis_第11张图片
6.到此Mybatis配置完成,我跳过业务层,直接在表现层调用持久化层测试一下
3.SpringBoot集成SpringMVC和MyBatis_第12张图片
7.运行成功
3.SpringBoot集成SpringMVC和MyBatis_第13张图片

你可能感兴趣的:(SpringBoot)