springboot整合mybatis

188 阅读1分钟

第一步:在pom.yml文件中配置

  <parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.2.2.RELEASE</version>
  </parent>
  
<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     <!--
     mybatis 集成
    -->
     <dependency>
       <groupId>org.mybatis.spring.boot</groupId>
       <artifactId>mybatis-spring-boot-starter</artifactId>
       <version>2.1.1</version>
     </dependency>
     <!-- springboot分页插件 -->
     <dependency>
       <groupId>com.github.pagehelper</groupId>
       <artifactId>pagehelper-spring-boot-starter</artifactId>
       <version>1.2.13</version>
     </dependency>

    <!--mysql 驱动-->
     <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>

     </dependency>
     <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
     <dependency>
       <groupId>com.mchange</groupId>
       <artifactId>c3p0</artifactId>
       <version>0.9.5.5</version>
     </dependency>
  </dependencies>

添加spring-boot-maven插件
    <plugins>
       <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
       </plugin>
     </plugins>

第二步:配置application.yml

## 端口号
server:
  port: 9999
## 数据源配置
spring:
  datasource:
    type: com.mchange.v2.c3p0.ComboPooledDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springboot_mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
    username: root
    password: root
## mybatis 配置
mybatis:
  ## 这里设置了mybatis的sql存放的位置
  mapper-locations: classpath:/mappers/*.xml
  type-aliases-package: com.xxxx.springboot.vo
  configuration:
   ## 下划线转驼峰配置
   map-underscore-to-camel-case: true
## pageHelper
pagehelper:
  helper-dialect: mysql

#显示dao 执行sql语句
logging:
  level:
    com:
      zks:
        springboot:
          dao: debug

第三步:
添加controller、dao、service层

第四步:
在resources目录下添加mappers包,包里面写mybatis的动态sql

第五步:配置程序入口

@SpringBootApplication
@MapperScan("com.zks.springboot.dao")
public class Starter {
    public static void main(String[] args) {
        SpringApplication.run(Starter.class);
    }
}