1. 程式人生 > >dubbo+springboot+mybatis入門案例

dubbo+springboot+mybatis入門案例

此案例在 http://blog.csdn.net/qq_35641192/article/details/78132168 上面改動 
dubbo參考文件:https://www.gitbook.com/@dubbo

一、springboot-dubbo-provider的改動

1.pom加入mybatis與mysql的依賴

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
2.properties中加入資料庫配置

#配置資料來源
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root

3.增加mapper對映檔案(就把xml中的sql語句寫在註解裡面,偷懶)

package dubbo.test.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import dubbo.test.entity.Student;

@Mapper
public interface StudentMapper {
    @Insert("insert into student values(null,#{name},#{age})")
    public Integer add(Student student);

    @Delete("delete from student where id=#{id}")
    public Integer deleteById(Integer id);

    @Update("update student set name=#{name},age=#{age} where id=${id}")
    public Integer update(Student student);

    @Select("select * from student where id=#{id}")
    public Student queryById(Integer id);

    @Select("select * from student order by id")
    public List<Student> queryStudentList();
}
4.介面TestService改動(此處偷懶了,沒有將介面打成 jar 包後匯入,而是直接把介面檔案新增到專案下,強烈不建議此種做法)

package dubbo.test.remote;

import java.util.List;

import dubbo.test.entity.Student;

public interface TestService {
    String sayHello(String name);

    public Integer addStu(Student student);

    public Integer deleteById(Integer id);

    public Integer updateStu(Student student);

    public Student findStuById(Integer id);

    public List<Student> findAllStu();
}
5.實現類改動

package dubbo.test.remote.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import dubbo.test.entity.Student;
import dubbo.test.mapper.StudentMapper;
import dubbo.test.remote.TestService;

public class TestServiceImpl implements TestService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public String sayHello(String name) {
        return "Hello " + name + "! Springboot-Dubbo test success!";
    }

    public Integer addStu(Student student) {
        return studentMapper.add(student);
    }

    public Integer deleteById(Integer id) {
        return studentMapper.deleteById(id);
    }

    public Integer updateStu(Student student) {
        return studentMapper.update(student);
    }

    public Student findStuById(Integer id) {
        return studentMapper.queryById(id);
    }

    public List<Student> findAllStu() {
        return studentMapper.queryStudentList();
    }

}

6.加入student與response的實體類(體類其實也要抽離出來寫在common中,再次偷懶),注意實體類必須實現可序列化介面Serializable,不然會報錯!

二、springboot-dubbo-consumer的改動

1.增加實體類(偷懶。。)

2.修改介面(同上,偷懶)

3.修改控制器TestController

package dubbo.test.handler;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import dubbo.test.entity.Response;
import dubbo.test.entity.Student;
import dubbo.test.remote.TestService;

/**
 * 測試用的 Controller 類;
 */
@RestController
public class TestController {
    @Autowired
    TestService testService;

    /**
     * 測試 JSON 介面;
     */
    @ResponseBody
    @RequestMapping("/test/{name}")
    public String testJson(@PathVariable("name") String name) {
        String testStr = testService.sayHello(name);
        return testStr;
    }

    @PostMapping("/stu/add")
    public Object add(Student student) {
        Integer res = testService.addStu(student);
        return res == 1 ? new Response("200", "ok") : new Response("500", "fail");
    }

    @DeleteMapping("/stu/{id}")
    public Object delete(@PathVariable("id") Integer id) {
        Integer res = testService.deleteById(id);
        return res == 1 ? new Response("200", "ok") : new Response("500", "fail");
    }

    @PutMapping("/stu/update")
    public Object put(Student student) {
        Integer res = testService.updateStu(student);
        return res == 1 ? new Response("200", "ok") : new Response("500", "fail");
    }

    @GetMapping("/stu/{id}")
    public Object get(@PathVariable("id") Integer id) {
        Student student = testService.findStuById(id);
        return new Response("200", "ok", student);
    }

    @GetMapping("/stu")
    public Object list() {
        List<Student> studentList = testService.findAllStu();
        return new Response("200", "ok", studentList);
    }
}

三、資料庫增加表

四、測試

1.先啟動provider再啟動consumer

2.檢視dubbo admin是否註冊了

3.由於沒寫介面,所以用postman這個外掛測試

總結:個人認為dubbo將一個普通專案拆分4部分,服務層抽取介面做API,服務層的實現類與持久層一起作為provider,控制層做consumer,實體類,靜態資源什麼的抽取出來作為common公共方法