springboot 整合 mybatis

开发工具idea

1新建工程



SQL中选择MyBatis、MySQL、JDBC,下一步点击完成。

添加mapper、mapping、model包,最终的项目结构为:

package com.example.controller;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.mapper")
public class SpringMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringMybatisApplication.class, args);
    }
}
mapper

package com.example.mapper;

import com.example.model.User;

public interface UserMapper {

    User selectByPrimaryKey(String id);

}
user

package com.example.model;

import java.util.Date;

public class User {
    private String id;
    private String email;
    private Date createtime;
    private String password;
    private String type;
    private String nickname;
    private String avatar;
    private String status;

   //set get 略
}
usermapping

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.mapper.UserMapper" >
  <resultMap id="BaseResultMap" type="com.example.model.User" >
    <id column="id" property="id" jdbcType="CHAR" />
    <result column="email" property="email" jdbcType="VARCHAR" />
    <result column="createtime" property="createtime" jdbcType="TIMESTAMP" />
    <result column="password" property="password" jdbcType="CHAR" />
    <result column="type" property="type" jdbcType="VARCHAR" />
    <result column="nickname" property="nickname" jdbcType="VARCHAR" />
    <result column="avatar" property="avatar" jdbcType="VARCHAR" />
    <result column="status" property="status" jdbcType="CHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, email, createtime, password, type, nickname, avatar, status
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=CHAR}
  </select>
</mapper>
applicationproperties

mybatis.type-aliases-package=com.example.model
mybatis.mapper-locations=classpath:mapping/*.xml

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/whtest?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root
测试类

package com.example.demo;

import com.example.controller.SpringMybatisApplication;
import com.example.mapper.UserMapper;
import com.example.model.User;
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.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration(classes =SpringMybatisApplication.class )
@SpringBootTest
public class SpringMybatisApplicationTests {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void contextLoads() {
        User user = userMapper.selectByPrimaryKey("1");
        System.err.println("user.getCreatetime()" +user.getCreatetime());
    }
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值