zoukankan      html  css  js  c++  java
  • springboot集成mybatis源码分析(一)

    本篇文章只是简单接受使用,具体源码解析请看后续文章

    1、新建springboot项目,并导入mybatis的pom配置

    配置数据库驱动和mybatis 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.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
    </dependency>
    application.yml配置

    spring :
    datasource :
    driverClassName : com.mysql.jdbc.Driver
    url : jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username : root
    password : 123456

    2、基础类(使用lombok自动生成get/set方法)

    package com.example.demo.domain;
    
    import lombok.Data;
    
    @Data
    public class User {
        private Integer id;
        private String username;
        private Integer age;
    }

    3、测试dao(mybatis使用注解开发)

    package com.example.demo.dao;
    
    import com.example.demo.domain.User;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Select;
    
    @Mapper
    public interface UserDao {
        
        @Select("SELECT * FROM USER")
        List<User> getUser();
    }

    4、测试service

    package com.example.demo.service;
    
    import com.example.demo.dao.UserDao;
    import com.example.demo.domain.User;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    @Slf4j
    public class UserService {
    
        @Autowired
        private UserDao userDao;
    
        public List<User> getUser(){
            List<User> userList = userDao.getUser();
            log.info("查询出来的用户信息,{}",userList.toString());
            return userList;
        }
    }

    5、service对应的test类(该测试类继承主测试类(主测试类直接在启动文件上goto test即可自动生成))

    package com.example.demo.service;
    
    import com.example.demo.DemoApplicationTests;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import static org.junit.Assert.*;
    
    public class UserServiceTest extends DemoApplicationTests {
    
        @Autowired
        private UserService userService;
    
        @Test
        public void getUser() {
            userService.getUser();
        }
    
    }
    package com.example.demo;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class DemoApplicationTests {
    
        @Test
        public void contextLoads() {
        }
    
    }

    6、运行测试类输出结果

      查询出来的用户信息,[User(id=1, username=test, age=11)]

      

  • 相关阅读:
    HDU4385Moving Bricks【状压DP】
    用位运算实现加减法
    hdu 1874(最短路 Dilkstra +优先队列优化+spfa)
    codeforces 782B The Meeting Place Cannot Be Changed+hdu 4355+hdu 2438 (三分)
    hdu 1542(线段树+扫描线 求矩形相交面积)
    hdu 2602(经典01背包)
    hdu 1698(线段树区间更新)
    hdu 1754(单点更新 ,区间最大值)
    NYOJ 寻找最大数
    hdu 2222(AC自动机模版题)
  • 原文地址:https://www.cnblogs.com/nxzblogs/p/10482055.html
Copyright © 2011-2022 走看看