Spring Boot 整合mybatis

按照之前的文章先整合Druid

增加mybatis依赖:


    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.0.1

增加配置信息:

mybatis:
  mapper-locations: classpath*:/mapper/*.xml
  type-aliases-package: cn.ishangit.**.bean

扫描Dao接口和开启声明式事务:

在主类中增加扫描注解@MapperScan("cn.ishangit.**.dao")及事务管理@EnableTransactionManagement

测试

创建Dao包,创建MemberDao类如下:

package cn.ishangit.hellospringboot2.dao;

import cn.ishangit.hellospringboot2.bean.Member;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @description:
 * @author: Chen
 * @create: 2019-06-24 20:17
 **/
@Repository
public interface MemberDao {

    @Select("select * from t_member")
    List findAll();
}

创建MemberService接口,MemberServiceImpl实现类:

package cn.ishangit.hellospringboot2.service;

import cn.ishangit.hellospringboot2.bean.Member;

import java.util.List;

/**
 * @description:
 * @author: Chen
 * @create: 2019-06-24 20:19
 **/
public interface MemberService {

    List findAll();
}
package cn.ishangit.hellospringboot2.service.impl;

import cn.ishangit.hellospringboot2.bean.Member;
import cn.ishangit.hellospringboot2.dao.MemberDao;
import cn.ishangit.hellospringboot2.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description:
 * @author: Chen
 * @create: 2019-06-24 20:20
 **/
@Service
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public List findAll() {
        return memberDao.findAll();
    }
}

创建MemberController:

package cn.ishangit.hellospringboot2.controller;

import cn.ishangit.hellospringboot2.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @description:
 * @author: Chen
 * @create: 2019-06-24 20:22
 **/
@Controller
public class MemberController {

    @Autowired
    private MemberService memberService;

    @ResponseBody
    @RequestMapping(value = "findAll")
    public List getAll(){
        return memberService.findAll();
    }
}

访问http://localhost:8080测试

你可能感兴趣的:(Spring Boot 整合mybatis)