SpringBoot构建微服务实战

1. 创建一个Maven项目,

目录结构:

pom.xml文件内容如下:

<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>com.windy.mall.product</groupId>
	<artifactId>ms-mall</artifactId>
	<version>1.0.0-SNAPSHOT</version>
	<packaging>jar</packaging>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.0.RELEASE</version>
	</parent>

	<name>ms-mall</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.1</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>
	</dependencies>
</project>

  

2. 创建数据库,sql如下:

create database db_products default charset utf8;

create table products (pid int not null primary key auto_increment, pname varchar(200), type varchar(50), price double, createTime timestamp)

  

3. 创建resouces folder: src/main/resources,并创建application.properties文件

spring.datasource.url=jdbc:mysql:///db_products?useSSL=false
spring.datasource.username=***
spring.datasource.password=***

  

4. 创建实体类package: bean,并创建实体类Product.java

package com.windy.mall.product.bean;

import java.sql.Timestamp;

public class Product {

	private Integer pid;
	private String pname;
	private String type;
	private Double price;
	private Timestamp createTime;

	public Integer getPid() {
		return pid;
	}
	public void setPid(Integer pid) {
		this.pid = pid;
	}
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	public Timestamp getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Timestamp createTime) {
		this.createTime = createTime;
	}

	public String toString() {
		return "Product [pid=" + pid + ", pname=" + pname + ", type=" + type + ", price=" + price + ", createTime="
				+ createTime + "]";
	}

}

  

5. 创建Mapper package:mapper,并创建一个Mapper接口:ProductMapper(整合Mybatis,基于注解的形式)

Mybatis会帮助我们自动创建实现类, 我们只需要声明接口即可.

问题: 如何使用Mybatis的逆向工程,自动生成实体类,并且Mapper接口都是以注解的形式存在,而并非XML形式?

package com.windy.mall.product.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 com.windy.mall.product.bean.Product;

@Mapper
public interface ProductMapper {

	@Insert("insert into products(pname,type,price)values(#{pname},#{type},#{price})")
	public Integer add(Product product);

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

	@Update("update products set pname=#{pname},type=#{type},price=#{price} where pid=#{id} ")
	public Integer update(Integer id);

	@Select("select * from products where pid=#{arg1}")
	public Product getById(Integer id);

	@Select("select * from products order by pid desc")
	public List<Product> queryByLists();

}

  

6. 针对Mapper的单元测试(待补充)

7. 创建一个响应类Response,把所有返回结果信息,封装在这个类中.

package com.windy.mall.product.utils;

public class Response {
	/*
	 * code: 标志状态码
	 * 200:表示成功
	 * 404: 表示资源不存在
	 * 500:表示失败
	 */

	private String code;
	private String msg;
	private Object data;

	public Response() {
		super();
	}

	public Response(String code, String msg) {
		super();
		this.code = code;
		this.msg = msg;
	}

	public Response(String code, String msg, Object data) {
		super();
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

}

 

8. 创建Controller类(由于只是简单的Demo,所以没有实现Service层)

package com.windy.mall.product.controller;

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.RestController;

import com.windy.mall.product.bean.Product;
import com.windy.mall.product.mapper.ProductMapper;
import com.windy.mall.product.utils.Response;

@RestController
public class ProductController {

	@Autowired
	private ProductMapper productMapper;

	@PostMapping("/ms/product/add")
	public Object add(Product product){
		Integer res = productMapper.add(product);
		if(res == null){
			return new Response("200", "Success!");
		}

			return new Response("500","Fail!");
	}

	@PutMapping("/ms/product/update")
	public Object update(Product product ){
		return new Response("200", "Success!", productMapper.update(product));
	}

	@GetMapping("/ms/product/{id}")
	public Object get(@PathVariable("id") Integer id ){
		return new Response("200", "Success!", productMapper.getById(id));
	}

	@GetMapping("/ms/products")
	public Object list(){
		return new Response("200","Success!", productMapper.queryByLists());
	}

	@DeleteMapping("/ms/product/{id}")
	public Object delete(@PathVariable("id") Integer id ){
		Integer res = productMapper.deleteById(id);
		return res==1? new Response("200","Success!") : new Response("500", "Fail!");

	}

}

  

 

原文地址:https://www.cnblogs.com/fengze/p/8326040.html

时间: 2024-08-02 17:15:18

SpringBoot构建微服务实战的相关文章

SpringBoot 快速构建微服务体系 知识点总结

可以通过http://start.spring.io/构建一个SpringBoot的脚手架项目 一.微服务 1.SpringBoot是一个可使用Java构建微服务的微框架. 2.微服务就是要倡导大家尽量将功能进行拆分,将服务粒度做小,使之可以独立承担对外服务的职责,沿着这个思路开发和交付的软件服务实体就叫做“微服务”. 3.微服务的好处 (1)独立,独立,还是独立.每一个微服务都是一个小王国,跳出了“大一统”(Monolith)王国的统治,开始从各个层面打造自己的独立能力,从而保障自己的小王国可

JAVA架构师之SpringBoot,SpringCloud构建微服务项目架构

springcloud微服务项目架构搭建第一天(一).项目简介1.准备工作:idea创建springboot模板 2.后台应该涉及的技术(后期可能会有删改) Spring Framework 容器SpringMVC MVC框架Apache Shiro 安全框架Spring session 分布式Session管理MyBatis ORM框架MyBatis Generator 代码生成PageHelper MyBatis物理分页插件Druid 数据库连接池FluentValidator 校验框架Th

pring Cloud构建微服务架构

SpringCloud-Learning 本项目内容为Spring Cloud教程的程序样例. 作者博客:http://blog.didispace.com Spring Cloud系列博文:http://blog.didispace.com/categories/Spring-Cloud/ Spring Cloud中文社区:http://bbs.springcloud.com.cn/ GitHub:https://github.com/dyc87112/SpringCloud-Learning

使用Spring Boot构建微服务(文末福利)

本文主要内容 学习微服务的关键特征 了解微服务是如何适应云架构的 将业务领域分解成一组微服务 使用Spring Boot实现简单的微服务 掌握基于微服务架构构建应用程序的视角 学习什么时候不应该使用微服务 软件开发的历史充斥着大型开发项目崩溃的故事,这些项目可能投资了数百万美元.集中了行业里众多的顶尖人才.消耗了开发人员成千上万的工时,但从未给客户交付任何有价值的东西,最终由于其复杂性和负担而轰然倒塌. 这些庞大的项目倾向于遵循大型传统的瀑布开发方法,坚持在项目开始时界定应用的所有需求和设计.这

构建微服务:如何优雅的使用mybatis

本文由www.29sl.com转载发布:这两天启动了一个新项目因为项目组成员一直都使用的是mybatis,虽然个人比较喜欢jpa这种极简的模式,但是为了项目保持统一性技术选型还是定了 mybatis.到网上找了一下关于spring boot和mybatis组合的相关资料,各种各样的形式都有,看的人心累,结合了mybatis的官方demo和文档终于找到了最简的两种模式,花了一天时间总结后分享出来. orm框架的本质是简化编程中操作数据库的编码,发展到现在基本上就剩两家了,一个是宣称可以不用写一句s

【架构】微服务实战:从发布到架构——上篇

微服务实战:从发布到架构——上篇  MaxLeap2016-03-23 10:42 “微服务”是当前软件架构领域非常热门的词汇,能找到很多关于微服务的定义.准则,以及如何从微服务中获益的文章,在企业的实践中去应用“微服务”的资源却很少.本篇文章中,会介绍微服务架构(Microservices Architecture)的基础概念,以及如何在实践中具体应用. 单体架构(Monolithic Architecture ) 企业级的应用一般都会面临各种各样的业务需求,而常见的方式是把大量功能堆积到同一

构建微服务:如何优雅的使用mybaits

*:first-child{margin-top: 0 !important}.markdown-body>*:last-child{margin-bottom: 0 !important}.markdown-body .absent{color: #c00}.markdown-body .anchor{position: absolute;top: 0;left: 0;display: block;padding-right: 6px;padding-left: 30px;margin-lef

构建微服务:Spring boot 入门篇

构建微服务:Spring boot 入门篇 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.用我的话来理解,就是spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架(不知道这样比喻是否合适). 使用spring boot有什

【架构】微服务实战:从发布到架构——下篇

 MaxLeap2016-03-25 13:53 上篇文章介绍了微服务和单体架构的区别.微服务的设计.消息.服务间通信.数据去中心化,本篇会继续深入微服务,介绍其它特性. 治理去中心化 通常“治理”的意思是构建方案,并且迫使人们通过努力达到组织的目标.SOA治理指导开发者开发可重用的服务,以及随着时间推移,服务应该怎么被设计和开发.治理建立了服务提供者和消费者之间对于服务的协定,告诉消费者能从服务提供获取到什么样的支持. SOA中有两种常见的治理: 设计时的治理-定义和控制服务的创建.设计和服务