spring-boot集成mybatis
前言:
基于前面讲解的基础上进行集成,包结构有添加 下图:
数据库配置 和 jpa 一致不需要修改.
pom 文件修改
<!-- mybatis begin 在原来基础上 新引入 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mybatis end -->
写mapper 类下:
package com.jade.mapper;
import com.jade.domian.User;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.type.JdbcType;
/**
*
*@author jade
*@date 2016年7月16日上午10:54:50
*@version 1.0
*/
@Mapper
public interface UserMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbggenerated Sat Jul 16 10:41:22 CST 2016
*/
@Delete({
"delete from user",
"where id = #{id,jdbcType=BIGINT}"
})
int deleteByPrimaryKey(Long id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbggenerated Sat Jul 16 10:41:22 CST 2016
*/
@Select({
"select",
"id, age, email_address, username",
"from user",
"where id = #{id,jdbcType=BIGINT}"
})
@Results({
@Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
@Result(column="age", property="age", jdbcType=JdbcType.INTEGER),
@Result(column="email_address", property="emailAddress", jdbcType=JdbcType.VARCHAR),
@Result(column="username", property="username", jdbcType=JdbcType.VARCHAR)
})
User selectByPrimaryKey(Long id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbggenerated Sat Jul 16 10:41:22 CST 2016
*/
@Select({
"select",
"id, age, email_address, username",
"from user"
})
@Results({
@Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
@Result(column="age", property="age", jdbcType=JdbcType.INTEGER),
@Result(column="email_address", property="emailAddress", jdbcType=JdbcType.VARCHAR),
@Result(column="username", property="username", jdbcType=JdbcType.VARCHAR)
})
List<User> selectAll();
}
搭建完成.. 是不是太简单……
测试:
package main.java;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.jade.Application;
import com.jade.domian.User;
import com.jade.mapper.UserMapper;
import com.jade.repository.UserRepository;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
/**
* 测试
*@author panlunyou
*@date 2016年7月14日下午3:18:34
*@version 1.0
*/
public class testJpa {
@Autowired
UserRepository res;
@Autowired
UserMapper userMapper;
@Test
public void testGetForJpa(){
List<User> luesr=res.findAll();
for(User user :luesr){
System.out.println(user.getId() + " "+user.getUsername());
}
}
@Test
public void testGetForMybatis(){
List<User> luesr=userMapper.selectAll();
for(User user :luesr){
System.out.println(user.getId() + " "+user.getUsername());
}
}
}