SpringBoot整合连接池、mybatis、tkMybatis

      • 1. SpringBoot整合连接池
        • 1.1 在pom.xml中引人jdbc连接池依赖
        • 1.2 在application.properties配置数据库连接信息
      • 2 SpringBoot整合mybatis
        • 2.1 概述
        • 2.2 在pom.xml引入mybatis依赖
        • 2.3 表结构与数据
        • 2.4 实体类
        • 2.5 业务接口与实现类
        • 2.6 控制器类
        • 2.7 XML方式
          • 2.7.1 Mapper接口
          • 2.7.2 xml映射文件
          • 2.7.3 application.properties配置
          • 2.7.4 SpringBoot启动类添加扫描接口的注解
        • 2.8 注解方式
      • 3. SpringBoot集成tkMybatis
        • 3.1 在pom.xml引入依赖
        • 3.2 Mapper接口继承通用的Mapper接口
        • 3.3 SpringBoot启动类修改成tk包的@MapperScan
        • 3.4 tk的Mapper的方法

1. SpringBoot整合连接池

1.1 在pom.xml中引人jdbc连接池依赖


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-jdbcartifactId>
dependency>


<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
dependency>

1.2 在application.properties配置数据库连接信息

# 数据源连接信息
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver

# hikari连接池
spring.datasource.hikari.idle-timeout=60000
spring.datasource.hikari.maximum-pool-size=30
spring.datasource.hikari.minimum-idle=10

2 SpringBoot整合mybatis

2.1 概述

  • SpringBoot没有提供整合mybatis的包,但mybatis官方提供了向SpringBoot整合的包,参考官网链接:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

2.2 在pom.xml引入mybatis依赖



<dependency>
    <groupId>org.mybatis.spring.bootgroupId>
    <artifactId>mybatis-spring-boot-starterartifactId>
    <version>1.3.2version>
dependency>

2.3 表结构与数据

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `birthday` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

INSERT INTO `user` VALUES ('1', '张三', '13', '2013-03-03');
INSERT INTO `user` VALUES ('2', '李四', '14', '2014-04-04');
INSERT INTO `user` VALUES ('3', '王五', '15', '2015-05-05');

2.4 实体类

package com.domain;
import java.util.Date;
public class User {
    private Integer id;
    private String username;
    private Integer age;
    private Date birthday;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Date getBirthday() {return birthday;}
    public void setBirthday(Date birthday) {this.birthday = birthday; }
    @Override
    public String toString() {
        return "User{" + "id=" + id + ", username='" + username + '\'' + ", age=" + age + ", birthday=" + birthday + '}';
    }
}

2.5 业务接口与实现类

package com.service;
import com.domain.User;
import java.util.List;

public interface IUserService {
    List<User> findAll();
}
package com.service.impl;

import com.service.IUserService;
import com.domain.User;
import com.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service  //自动创建bean
public class UserServiceImpl implements IUserService {
    @Autowired  //自动根据类型装配注入属性值
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }
}

2.6 控制器类

package com.controller;

import com.domain.User;
import com.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController  // @ResponseBody与@Controller结合
@RequestMapping("user")
public class UserController {
    @Autowired  //自动根据类型注入属性值
    private IUserService userService;
    
    @RequestMapping("findAll")
    public List<User> findAll(){
        System.out.println("findAll");
        List<User> userList=userService.findAll();
        System.out.println(userList);
        return  userList;
    }
}

2.7 XML方式

2.7.1 Mapper接口
package com.mapper;
import com.domain.User;
import java.util.List;

public interface UserMapper {
    List<User> findAll();
}
2.7.2 xml映射文件




<mapper namespace="com.mapper.UserMapper">
    <select id="findAll" resultType="com.domain.User">
        select * from user;
    select>
mapper>
2.7.3 application.properties配置
# mybatis 实体类型别名扫描,配合映射文件XXXMapper.xml使用
# 如果用注解方式,该设置可以省略
mybatis.type-aliases-package=com.domain
# mapper.xml映射文件位置,如果接口与xml在同一个包中(包括java中的包与resource中的包同包),该设置可以省略
# 如果用注解方式,该设置可以省略
mybatis.mapper-locations=classpath:com/mapper/*.xml
2.7.4 SpringBoot启动类添加扫描接口的注解
@SpringBootApplication
@MapperScan("com.mapper")  //扫描com.mapper所有Mapper接口
public class SpringBootStarter {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootStarter.class);
    }
}

2.8 注解方式

  • 2.7.2与2.7.3步骤直接省略掉,不用做
  • 2.7.4代码不变,依然要添加@MapperScan注解
  • Mapper接口中@Mapper注解只对当前接口有效
  • SpringBoot启动类的@MapperScan对指定包下所有接口都有效
package com.mapper;
import com.domain.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;

//为了省事,直接在SpringBoot启动类中一次性扫描整个包,不再单独再Mapper接口上使用Mapper注解
public interface UserMapper {
    @Select("select * from user")
    List<User> findAll();
}
@SpringBootApplication
@MapperScan("com.mapper")  //扫描com.mapper所有Mapper接口
public class SpringBootStarter {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootStarter.class);
    }
}

3. SpringBoot集成tkMybatis

3.1 在pom.xml引入依赖

<dependency>
    <groupId>tk.mybatisgroupId>
    <artifactId>mapper-spring-boot-starterartifactId>
    <version>2.0.2version>
dependency>

3.2 Mapper接口继承通用的Mapper接口

package com.mapper;
import com.domain.User;
import tk.mybatis.mapper.common.Mapper;

public interface UserMapper extends Mapper<User> {
    //继承tk的Mapper接口有很多默认的方法
}

3.3 SpringBoot启动类修改成tk包的@MapperScan

package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//注意注意注意:这里引入的是tk的注解,不用springboot的
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan("com.mapper")   //扫描com.mapper包
public class SpringBootStarter {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootStarter.class);
    }
}

3.4 tk的Mapper的方法

  • 这里不详细说明
    SpringBoot整合连接池、mybatis、tkMybatis_第1张图片

你可能感兴趣的:(#,SpringBoot)