spring boot整合dubbo加入mybatis,mybatisplus(Client)
首先我們先用myeclipse建立一個maven專案(可直接去dubbo官網生成http://start.dubbo.io/)。
目錄結構:
接下來複制一下pom配置(根據自己所需)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>testClient</groupId> <artifactId>testClient</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>testClient</name> <description /> <properties> <webVersion>3.1</webVersion> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <mybatis-plus-boot-starter.version>3.0-RC1</mybatis-plus-boot-starter.version> <HikariCP.version>3.2.0</HikariCP.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.15.RELEASE</version> </parent>
<dependencies> <!-- 加上這個才能辨認到log4j2.yml檔案 --> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-yaml</artifactId> <version>2.9.3</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>${HikariCP.version}</version> </dependency>
<!-- mybatis-plus begin --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus-boot-starter.version}</version> </dependency> <!-- mybatis-plus end --> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.1.0</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.24.Final</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <finalName>Client</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <warSourceDirectory>${basedir}/WebRoot</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
</plugins> </build> </project>
然後building一下,下載需要的jar包。
完成之後,在com.example.demo包下建立第一個DemoApplication:
package com.example.demo;
import org.springframework.boot.SpringApplication; import com.alibaba.dubbo.config.annotation.Reference; import com.example.IAffairsService; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement @SpringBootApplication //因為springboot會自動掃描一下mian所在的包,我的service放在上級目錄所有不加入掃描。 @ComponentScan(basePackages = { "com.baomidou.springboot.controller"}) public class DemoApplication {
@Reference(version = "1.0.0") private IAffairsService affairsService;
public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } /* * 測試程式碼 * @PostConstruct public void init() { Affairs sayHello = affairsService.getById(8); System.err.println(sayHello.getTitle()); }*/ }
接下來在com.example包下建立service(可利用mybatisplus的一鍵生成工具直接生成):
package com.example;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.springboot.entity.Affairs;
/** * <p> * 服務類 * </p> * * @author FUNKYE * @since 2018-09-03 */ public interface IAffairsService extends IService<Affairs> { //因為使用了mybatisplus,目前沒寫專用的mapper,如果需要可以自己在下面寫入 } 之後在建立所需要的實體類:
package com.baomidou.springboot.entity;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
/** * <p> * * </p> * * @author FUNKYE * @since 2018-09-03 */ public class Affairs implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO) private Integer id; private String title; private Date date; private String content; @TableField("useradminId") private Integer useradminId;
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public Date getDate() { return date; }
public void setDate(Date date) { this.date = date; }
public String getContent() { return content; }
public void setContent(String content) { this.content = content; }
public Integer getUseradminId() { return useradminId; }
public void setUseradminId(Integer useradminId) { this.useradminId = useradminId; }
@Override public String toString() { return "Affairs{" + ", id=" + id + ", title=" + title + ", date=" + date + ", content=" + content + ", useradminId=" + useradminId + "}"; } } 之後再建立Controller:
package com.baomidou.springboot.controller;
import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.dubbo.config.annotation.Reference; import com.baomidou.springboot.entity.Affairs; import com.example.IAffairsService;
/** * <p> * 前端控制器 * </p> * * @author FUNKYE * @since 2018-09-03 */ @Controller public class AffairsController extends BaseController { @Reference(version = "1.0.0") private IAffairsService as;
@ResponseBody @RequestMapping("{token}/test") public Object test(@PathVariable String token,HttpSession session) throws Exception { MySessionContext myc= MySessionContext.getInstance(); session = myc.getSession(token); return renderSuccess(session.getAttribute("111")); } @ResponseBody @RequestMapping("test1") public Object test1(HttpSession session) throws Exception { session.setAttribute("111", "111"); return renderSuccess(session.getId()); } @ResponseBody @RequestMapping("findId") public Object findId(String id){ Affairs a=as.getById(id); return a; }
} 以及所需要的繼承的BaseController:
package com.baomidou.springboot.controller;
import com.baomidou.springboot.result.JsonResult;
/** * Author: D.Yang * Email: [email protected] * Date: 16/10/9 * Time: 下午1:37 * Describe: 基礎控制器 */ public class BaseController { /** * 渲染失敗資料 * * @return result */ protected JsonResult renderError() { JsonResult result = new JsonResult(); result.setSuccess(false); result.setStatus("500"); return result; }
/** * 渲染失敗資料(帶訊息) * * @param msg 需要返回的訊息 * @return result */ protected JsonResult renderError(String msg) { JsonResult result = renderError(); result.setMsg(msg); return result; }
/** * 渲染成功資料 * * @return result */ protected JsonResult renderSuccess() { JsonResult result = new JsonResult(); result.setSuccess(true); result.setStatus("200"); return result; }
/** * 渲染成功資料(帶資訊) * * @param msg 需要返回的資訊 * @return result */ protected JsonResult renderSuccess(String msg) { JsonResult result = renderSuccess(); result.setMsg(msg); return result; }
/** * 渲染成功資料(帶資料) * * @param obj 需要返回的物件 * @return result */ protected JsonResult renderSuccess(Object obj) { JsonResult result = renderSuccess(); result.setObj(obj); return result; } } 接下配置application.properties配置註冊中心:
# Dubbo Config properties ## ApplicationConfig Bean dubbo.application.name= dubbo-demo-client
## RegistryConfig Bean dubbo.registry.id = my-registry dubbo.registry.address = zookeeper://localhost:2181?client=curator
dubbo.application.qosEnable=false
好了,可以去DemoApplication裡啟動屬於你的springboot dubbo專案了!