Spring Boot 菜鸟教程 3 MyBatis

MyBatis

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。

  • 集成spring boot 的时候必须在mapper接口上面标注@Mapper注解

项目图片

Spring Boot 菜鸟教程 3 MyBatis_第1张图片
这里写图片描述

pom.xml

-只需要在pom.xml引入需要的数据库配置,就会自动访问此数据库,如果需要配置其他数据库,可以在application.properties进行添加
-默认使用org.apache.tomcat.jdbc.pool.DataSource创建连接池


    4.0.0

    com.jege.spring.boot
    spring-boot-mybatis
    0.0.1-SNAPSHOT
    jar

    spring-boot-mybatis
    http://maven.apache.org

    
    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.1.RELEASE
        
    

    
        UTF-8
        1.8
    

    

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        

        
        
            com.h2database
            h2
            runtime
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

    

    
        spring-boot-mybatis
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    ${java.version}
                    ${java.version}
                
            
        
    


模型对象User

package com.jege.spring.boot.mybatis.entity;

/**
 * @author JE哥
 * @email [email protected]
 * @description:模型对象
 */
public class User {
  private Long id;
  private String name;
  private Integer age;

  public User() {
  }

  public User(String name, Integer age) {
    this.name = name;
    this.age = age;
  }
}

持久层UserMapper

package com.jege.spring.boot.mybatis.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import com.jege.spring.boot.mybatis.entity.User;

/**
 * @author JE哥
 * @email [email protected]
 * @description:持久层接口,由spring自动生成其实现
 */
@Mapper
public interface UserMapper {
  @Delete("drop table t_user if exists")
  void dropTable();

  @Insert("create table t_user (id bigint generated by default as identity, age integer, name varchar(255), primary key (id))")
  void createTable();

  @Insert("insert into t_user(name,age) values(#{name},#{age})")
  void insert(User user);

  @Select("select id,name,age from t_user")
  List findAll();

  @Select("select id,name,age from t_user where name like #{name}")
  List findByNameLike(String name);

  @Delete("delete from t_user")
  void deleteAll();

}


不需要application.properties

测试类UserMapperTest

package com.jege.spring.boot.mybatis;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.After;
import org.junit.Before;
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.test.context.junit4.SpringJUnit4ClassRunner;

import com.jege.spring.boot.mybatis.entity.User;
import com.jege.spring.boot.mybatis.mapper.UserMapper;

/**
 * @author JE哥
 * @email [email protected]
 * @description:
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest()
public class UserMapperTest {
  @Autowired
  UserMapper userMapper;

  // 每次执行Test之前先删除表,创建表
  @Before
  public void before() throws Exception {
    userMapper.dropTable();
    userMapper.createTable();
  }

  // 打印出class com.sun.proxy.$Proxy66表示spring注入通过jdk动态代理获取接口的子类
  @Test
  public void proxy() throws Exception {
    System.out.println(userMapper.getClass());
  }

  @Test
  public void save() throws Exception {
    for (int i = 0; i < 10; i++) {
      User user = new User("jege" + i, 25 + i);
      userMapper.insert(user);
    }
  }

  @Test
  public void all() throws Exception {
    save();
    assertThat(userMapper.findAll()).hasSize(10);
  }

  @Test
  public void findByName() throws Exception {
    save();
    assertThat(userMapper.findByNameLike("jege%")).hasSize(10);
  }

  // 每次执行Test之后清空数据
  @After
  public void destroy() throws Exception {
    userMapper.deleteAll();
  }

}


源码地址

https://github.com/je-ge/spring-boot

如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。
**您的支持将鼓励我继续创作!谢谢! **

微信打赏
微信打赏

支付宝打赏
支付宝打赏

你可能感兴趣的:(Spring Boot 菜鸟教程 3 MyBatis)