开发工具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());
}
}