spring-boot集成mybatis

spring-boot集成mybatis

前言:

基于前面讲解的基础上进行集成,包结构有添加 下图:
数据库配置 和 jpa 一致不需要修改.
在原来基础是添加 mapper 包

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());
       }

    }



}

结束

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值