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)]

      

  • 相关阅读:
    bzoj1618 / P2918 [USACO08NOV]买干草Buying Hay(完全背包)
    bzoj1617 / P2904 [USACO08MAR]跨河River Crossing
    bzoj1615 / P2903 [USACO08MAR]麻烦的干草打包机The Loathesome Hay Baler
    bzoj1613 / P1353 [USACO08JAN]跑步Running
    bzoj1612 / P2419 [USACO08JAN]牛大赛Cow Contest(Floyd)
    bzoj1611 / P2895 [USACO08FEB]流星雨Meteor Shower
    bzoj1610 / P2665 [USACO08FEB]连线游戏Game of Lines
    bzoj1609 / P2896 [USACO08FEB]一起吃饭Eating Together(最长不降子序列)
    bzoj1606 / P2925 [USACO08DEC]干草出售Hay For Sale(01背包)
    [bzoj1041][HAOI2008]圆上的整点
  • 原文地址:https://www.cnblogs.com/nxzblogs/p/10482055.html
Copyright © 2011-2022 走看看