美文网首页
二、springboot整合mybatis

二、springboot整合mybatis

作者: shmilylyp | 来源:发表于2019-06-16 15:59 被阅读0次

    1、新增repositoryModule

       (1)new -> new Module ->spring Initializr
       (2)修改pom文件
          <parent>
        <groupId>shmilylyp</groupId>
        <artifactId>shmilylyp</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.shmilylyp</groupId>
    <artifactId>repository</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>repository</name>
    <description>Demo project for Spring Boot</description>
    
    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>com.shmilylyp</groupId>
            <artifactId>bean</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    

    2、添加相应的依赖

      <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.12</version>
        </dependency>
        <!-- alibaba的druid数据库连接池 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
        </dependency>
    

    3、在application.yml文件中创建数据库和mybatis相关的配置

    (1)数据库的配置

    spring:
      datasource:
        name: mysql_set
        type: com.alibaba.druid.pool.DruidDataSource
        #druid相关配置
        druid:
          #监控统计拦截的filters
          filters: stat
          driver-class-name: com.mysql.cj.jdbc.Driver
          #基本属性
          url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
          username: root
          password: 123123
          #配置初始化大小/最小/最大
          initial-size: 1
          min-idle: 1
          max-active: 20
          #获取连接等待超时时间
          max-wait: 60000
          #间隔多久进行一次检测,检测需要关闭的空闲连接
          time-between-eviction-runs-millis: 60000
          #一个连接在池中最小生存的时间
          min-evictable-idle-time-millis: 300000
          validation-query: SELECT 'x'
          test-while-idle: true
          test-on-borrow: false
          test-on-return: false
          #打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
          pool-prepared-statements: false
          max-pool-prepared-statement-per-connection-size: 20
    

    (2)mybatis配置

    mybatis:
      mapper-locations: classpath:mapper/*.xml  #注意:一定要对应mapper映射xml文件的所在路径
      type-aliases-package: com.shmilylyp.model  # 注意:对应实体类的路径
    

    4、创建mapper和接口

    (1)mapper.xml

        <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.shmilylyp.repository.UserRepository">
      <resultMap id="userReturn" type="com.shmilylyp.bean.UserBean">
          <id column="id" property="id"></id>
          <result column="name" property="name"/>
          <result column="age" property="age"/>
      </resultMap>
      <sql id="Base_Column_List">
          id, name, age
      </sql>
      <select id="getUserList" resultMap="userReturn">
          select
          <include refid="Base_Column_List"/>
          from
          users
      </select>
    </mapper>
    

    (2) 创建对应的接口

        @Mapper
    public interface UserRepository {
        //  接口名称要和配置文件中的id相对应
        List<UserBean> getUserList();
    }
    

    相关文章

      网友评论

          本文标题:二、springboot整合mybatis

          本文链接:https://www.haomeiwen.com/subject/xadgfctx.html