欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot整合mybatis

程序员文章站 2022-07-13 09:56:14
...

正题

本项目使用的环境:

  • 开发工具:Intellij IDEA 
  • jdk:1.8
  • maven

额外功能

  • PageHelper 分页插件

步骤: 

  • 1.创建一个springboot项目: 
    springboot整合mybatis
    2.创建项目的文件结构以及jdk的版本 
    springboot整合mybatis 
    3.选择项目所需要的依赖 
    springboot整合mybatis

    然后点击finish
  • 4查看pom并添加需要的项目依赖(分页及数据库连接池Druid)
  • <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>
    
       <groupId>com.example</groupId>
       <artifactId>demo222</artifactId>
       <version>0.0.1-SNAPSHOT</version>
       <packaging>jar</packaging>
    
       <name>demo222</name>
       <description>Demo project for Spring Boot</description>
    
       <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>2.0.3.RELEASE</version>
          <relativePath/> <!-- lookup parent from repository -->
       </parent>
    
       <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
          <java.version>1.8</java.version>
       </properties>
    
       <dependencies>
          <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
          <dependency>
             <groupId>org.mybatis.spring.boot</groupId>
             <artifactId>mybatis-spring-boot-starter</artifactId>
             <version>1.3.2</version>
          </dependency>
    
          <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <scope>runtime</scope>
          </dependency>
          <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
          </dependency>
    
          <!-- 分页插件 -->
          <dependency>
             <groupId>com.github.pagehelper</groupId>
             <artifactId>pagehelper-spring-boot-starter</artifactId>
             <version>1.1.2</version>
          </dependency>
          <!-- alibaba的druid数据库连接池 -->
          <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>druid-spring-boot-starter</artifactId>
             <version>1.1.0</version>
          </dependency>
       </dependencies>
    
       <build>
          <plugins>
             <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
          </plugins>
       </build>
    
    
    </project>
    

    
    

    5.项目不使用application.properties文件 而使用更加简洁的application.yml文件: 
    将原有的resource文件夹下的application.properties文件删除,创建一个新的application.yml配置文件, 
    文件的内容如下:

    
    server:
      port: 8080
    
    spring:
        datasource:
            name: test
            url: jdbc:mysql://127.0.0.1:3306/depot
            username: root
            password: root
            # 使用druid数据源
            type: com.alibaba.druid.pool.DruidDataSource
            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:
      mapper-locations: classpath:mapping/*.xml
      type-aliases-package: com.winter.model
    
    #pagehelper分页插件
    pagehelper:
        helperDialect: mysql
        reasonable: true
        supportMethodsArguments: true
        params: count=countSql

    6.创建数据库,编写相应的实体类entity dao层 service controller

    UserController.java

    package com.winter.Controller;
    
    import com.winter.model.User;
    import com.winter.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    @RequestMapping(value = "/user")
    public class UserController {
    
        @Autowired
        private UserService userService;
    
        
    @ResponseBody
       @RequestMapping(value = "/all/{pageNum}/{pageSize}")
       public Object findAllUser(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize){
    
            return userService.findAllUser(pageNum,pageSize);
        }

    UserService.java

    package com.winter.service;
    
    import com.winter.model.User;
    
    import java.util.List;
    
    /**
     * Created by Administrator on 2017/8/16.
     */
    public interface UserService {
    
        List<User> findAllUser(int pageNum, int pageSize);
    }

    UserServiceImpl.java

    package com.winter.service.impl;
    
    import com.github.pagehelper.PageHelper;
    import com.winter.mapper.UserMapper;
    import com.winter.model.User;
    import com.winter.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service(value = "userService")
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UserMapper userMapper;//这里会报错,但是并不会影响
    
    
        /*
        * 这个方法中用到了我们开头配置依赖的分页插件pagehelper
        * 很简单,只需要在service层传入参数,然后将参数传递给一个插件的一个静态方法即可;
        * pageNum 当前页
        * pageSize 每页显示的数据条数
        * */
        @Override
        public List<User> findAllUser(int pageNum, int pageSize) {
            //将参数传给这个方法就可以实现物理分页了,非常简单。
            PageHelper.startPage(pageNum, pageSize);
            return userMapper.selectAllUser();
        }
    }