其他分享
首页 > 其他分享> > Spring boot整合Mybatis

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          #端口号
# 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);
    }
}

/**
 * @author scx
 */
@Data
public class Stu {
    private Integer id;
    private String name;
    private Integer age;
}
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();
}

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);
    }

}

标签:Spring,boot,springframework,yx,org,Mybatis,import,com
来源: https://blog.csdn.net/weixin_49992390/article/details/115454458