Spring Boot教程(15)-集成mybatis

Spring Boot教程(1)创建Spring项目

Spring Boot教程(2) – 运行第一个项目

Spring Boot教程(3)-配置文件-多环境配置

Spring Boot教程(4)-日志配置-logback和使用。

Spring Boot教程(5)-web应用开发-模板引擎Thymeleaf

Spring Boot教程(6)-web应用开发-错误处理

Spring Boot教程(7)-文件上传

Spring Boot教程(8)-使用SQL关系型数据库-JdbcTemplate

Spring Boot教程(9)-Spring-data-jpa

Spring Boot教程(10)-使用SQL关系型数据库-h2嵌入式数据库的使用

Spring Boot教程(11)-redis

Spring Boot教程(12)-redis缓存设置,幂等性防重复提交

Spring Boot教程(13)-使用异步消息服务-JMS(ActiveMQ)

Spring Boot教程(14)-使用异步消息服务-AMQP(RabbitMQ)

Spring Boot教程(15)-集成mybatis

Spring Boot教程(16)-集成Druid阿里数据库连接池

Spring Boot教程(17)-Spring Boot集成Swagger

 

一、添加依赖

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

二、配置文件

spring.datasource.url=jdbc:mysql://localhost/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
Encoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

三、复用之前写的WsjUser

package com.wsj.springbootdemo.bean;

import java.util.Date;

/**
 * 项目名称:WsjUser;
 * 类 名 称:WsjUser;
 * 类 描 述:TODO ;
 * 创 建 人:Angus;
 * 创建时间:2020/5/6 23:31;
 *
 * @version:1.0
 **/
public class WsjUser {
    private int id;
    private String name;
    private Date createTime;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    @Override
    public String toString() {
        return "RoncooUser [id=" + id + ", name=" + name + ", createTime=" + createTime
                + "]";
    }
}

四、编写mapper接口

package com.wsj.springbootdemo.mapper;

import com.wsj.springbootdemo.bean.WsjUser;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import static java.sql.JDBCType.TIMESTAMP;
import static org.apache.ibatis.type.JdbcType.VARCHAR;

/**
 * 项目名称:WsjUserMapper;
 * 类 名 称:WsjUserMapper;
 * 类 描 述:TODO ;
 * 创 建 人:Angus;
 * 创建时间:2020/5/7 20:34;
 *
 * @version:1.0
 **/
@Mapper
public interface WsjUserMapper {
    @Insert(value = "insert into wsj_user (name, create_time) values (#{name,jdbcType=VARCHAR},#{createTime,jdbcType=TIMESTAMP})")
    int insert(WsjUser record);

    @Select(value = "select id, name, create_time from wsj_user where id = #{id,jdbcType=INTEGER}")
    @Results(value = { @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP) })
    WsjUser selectByPrimaryKey(Integer id);

}

五、测试

package com.wsj.springbootdemo.mapper;

import com.wsj.springbootdemo.bean.WsjUser;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Date;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class WsjUserMapperTest {

    @Autowired
    private WsjUserMapper mapper;

    @Test
    void insert() {
        WsjUser wsjUser = new WsjUser();
        wsjUser.setName("测试22");
        wsjUser.setCreateTime(new Date());
        int result = mapper.insert(wsjUser);
        System.out.println(result);
    }

    @Test
    void selectByPrimaryKey() {
        WsjUser result = mapper.selectByPrimaryKey(2);
        System.out.println(result);
    }
}

运行。。。

Spring Boot教程(15)-集成mybatis_第1张图片

github地址:https://github.com/itwsj/springbootdemo

 

 

 

你可能感兴趣的:(SpringBoot)