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公共方法

猜你喜欢

转载自blog.csdn.net/zalan01408980/article/details/84325031