Spring boot整合Mybatis

Spring boot整合mybatis(最小配置)

  1. 创建maven工程、导入坐标
    <!--springboot工程需要继承的父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
    </parent>
    <dependencies>
        <!--web开发的起步依赖-->
        <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.0</version>
        </dependency>
        <!--mysql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!--lombok:用于自动生成get\set-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--测试类依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!--redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
    </dependencies>
  1. 配置属性:连接mysql、redis、mybatis
spring:
#  数据库连接
  datasource:
    url: jdbc:mysql://192.168.5.128/db1
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
#  redis连接
  redis:
    host: 192.168.5.128 #redis的主机ip
    port: 6379          #端口号
    password: 123456
# mybatis  
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-packge: com.yx.domain
  configuration: #sql打印日志输出
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  1. 编码:启动类、三层架构、测试类
  • 启动类
package com.yx;

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

/**
 * @author scx
 */
@SpringBootApplication
public class MybatisApp {
    public static void main(String[] args) {
        SpringApplication.run(MybatisApp.class,args);
    }
}

  • pojo
/**
 * @author scx
 */
@Data
public class Stu {
    private Integer id;
    private String name;
    private Integer age;
}
  • mapper
package com.yx.mapper;

import com.yx.domain.Stu;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author scx
 */
@Mapper
@Repository
public interface StuMapper {
    /**
     * 查询所有
     * @return
     */
    @Select("select * from stu")
    public List<Stu> findAll();
}

  • service(省略,用测试类做测试)
  • test
package com.yx;

import com.yx.domain.Stu;
import com.yx.mapper.StuMapper;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MybatisApp.class)
public class StuTest {
    @Autowired
    private StuMapper mapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    public void testAll(){
        List<Stu> all = mapper.findAll();
        System.out.println(all);
        // 测试redis地址
        System.out.println(redisTemplate);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值