基于springBoot,,springCloud,mybatis 框架简单 微服开发 ==CRUD

基本结构:父类工程。common工具类。provider提供者。消费者consumer (一般映射地址报错)

1...父类工程:需要配置pom.xml文件。

  手动指定pom  <packaging>pom</packaging>,

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6
 7     <groupId>com.wsc</groupId>
 8     <artifactId>spring-cloud-parent</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10     <modules>
11         <module>../../common</module>
12         <module>../../provider</module>
13         <module>../../consumer</module>
14     </modules>
15     <!--手动指定pom-->
16     <packaging>pom</packaging>
17     <!--springboot版本  2.0.7-->
18     <parent>
19         <groupId>org.springframework.boot</groupId>
20         <artifactId>spring-boot-starter-parent</artifactId>
21         <version>2.0.7.RELEASE</version>
22     </parent>
23     <properties>
24         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
25         <maven.compiler.source>1.8</maven.compiler.source>
26         <maven.compiler.target>1.8</maven.compiler.target>
27         <junit.version>4.12</junit.version>
28         <!--spring Cloud  最新的稳定版  Finchley SR2-->
29         <spring.cloud.version>Finchley.SR2</spring.cloud.version>
30     </properties>
31     <!--依赖声明-->
32     <dependencyManagement>
33         <dependencies>
34             <dependency>
35                 <groupId>org.springframework.cloud</groupId>
36                 <artifactId>spring-cloud-dependencies</artifactId>
37                 <version>${spring.cloud.version}</version>
38 <!--                不可或缺-->
39                 <scope>import</scope>
40                 <type>pom</type>
41             </dependency>
42             <dependency>
43                 <groupId>org.mybatis.spring.boot</groupId>
44                 <artifactId>mybatis-spring-boot-starter</artifactId>
45                 <version>1.3.2</version>
46             </dependency>
47             <!--数据源-->
48             <dependency>
49                 <groupId>com.alibaba</groupId>
50                 <artifactId>druid</artifactId>
51                 <version>1.1.12</version>
52             </dependency>
53 <!--            数据库  8.0.13-->
54             <dependency>
55                 <groupId>mysql</groupId>
56                 <artifactId>mysql-connector-java</artifactId>
57                 <version>8.0.13</version>
58             </dependency>
59             <!--测试单元-->
60             <dependency>
61                 <groupId>junit</groupId>
62                 <artifactId>junit</artifactId>
63                 <version>${junit.version}</version>
64                 <scope>test</scope>
65             </dependency>
66         </dependencies>
67     </dependencyManagement>
68 </project>

pom.xml

2.....common:

pom不需配置 引入父类即可,创建实体类pojo

 1 package com.wsc.core.pojo;
 2
 3 /**
 4  * @version 1.0
 5  * @ClassName Product
 6  * @Description TODO
 7  * @Author WSC
 8  * @Date 2019/8/26 14:53
 9  **/
10 public class Product {
11     private Long pid;
12     private String productName;
13     private  String dbSource;
14
15     public Product(String productName) {
16         this.productName = productName;
17     }
18
19     public Product(Long pid, String productName, String dbSource) {
20         this.pid = pid;
21         this.productName = productName;
22         this.dbSource = dbSource;
23     }
24     public Product() {
25     }
26
27     public Long getPid() {
28         return pid;
29     }
30
31     public void setPid(Long pid) {
32         this.pid = pid;
33     }
34
35     public String getProductName() {
36         return productName;
37     }
38
39     public void setProductName(String productName) {
40         this.productName = productName;
41     }
42
43     public String getDbSource() {
44         return dbSource;
45     }
46
47     public void setDbSource(String dbSource) {
48         this.dbSource = dbSource;
49     }
50 }

Product

3.....provider :

1...pom.xml    依赖common

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>spring-cloud-parent</artifactId>
 7         <groupId>com.wsc</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9         <relativePath>../springCloud/spring-cloud-parent/pom.xml</relativePath>
10     </parent>
11     <modelVersion>4.0.0</modelVersion>
12
13     <artifactId>provider</artifactId>
14
15
16     <properties>
17         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18         <maven.compiler.source>1.8</maven.compiler.source>
19         <maven.compiler.target>1.8</maven.compiler.target>
20     </properties>
21     <dependencies>
22         <dependency>
23             <groupId>com.wsc</groupId>
24             <artifactId>common</artifactId>
25             <version>${project.version}</version>
26         </dependency>
27         <dependency>
28             <groupId>org.springframework.boot</groupId>
29             <artifactId>spring-boot-starter-web</artifactId>
30         </dependency>
31         <dependency>
32             <groupId>mysql</groupId>
33             <artifactId>mysql-connector-java</artifactId>
34         </dependency>
35         <dependency>
36             <groupId>org.mybatis.spring.boot</groupId>
37             <artifactId>mybatis-spring-boot-starter</artifactId>
38         </dependency>
39         <dependency>
40             <groupId>junit</groupId>
41             <artifactId>junit</artifactId>
42         </dependency>
43         <dependency>
44             <groupId>com.alibaba</groupId>
45             <artifactId>druid</artifactId>
46         </dependency>
47         <dependency>
48             <groupId>org.springframework.boot</groupId>
49             <artifactId>spring-boot-starter-test</artifactId>
50         </dependency>
51     </dependencies>
52 </project>

pom.xml

2...resources/mybatis/mybatis.cfg.xml      驼峰命名规则

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <settings>
 7         <!--开启驼峰命名-->
 8         <setting name="mapUnderscoreToCamelCase" value="true"/>
 9     </settings>
10 </configuration>

3....resources/mybatis/mapper/ProductMapper   :    SQL语句

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 3         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 4 <mapper namespace="com.wsc.core.mapper.ProductMapper">
 5     <select id="findAll" resultType="Product">
 6         select * from product;
 7     </select>
 8
 9     <select id="findById" resultType="Product" parameterType="Long">
10         select * from product where pid=#{pid};
11     </select>
12
13     <insert id="add"    parameterType="Product" >
14         insert into product values (null,#{product_name},#{db_source});
15     </insert>
16 </mapper>

resources/application.xml        配置端口号 ,mybatis下的包的扫描 ,数据库的连接及数量 配置

 1 server:
 2   port: 8001
 3 mybatis:
 4   config-location: classpath:mybatis/mybatis.cfg.xml #mybatis 配置文件路径
 5   type-aliases-package: com.wsc.core.pojo # entity别名类所在包
 6   mapper-locations: mybatis/mapper/*.xml    # mapper映射文件
 7 spring:
 8   application:
 9     name: microserver-product #这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name
10   datasource:
11     type: com.alibaba.druid.pool.DruidDataSource
12     driver-class-name: com.mysql.cj.jdbc.Driver
13     url: jdbc:mysql://127.0.0.1:3306/springcloud_db01?serverTimezone=GMT%2B8
14     password: wsc
15     username: root
16     dbcp2:
17       min-idle: 5                         # 数据库连接池的最小维持连接数
18       initial-size: 5                     # 初始化连接数
19       max-total: 5                        # 最大连接数
20       max-wait-millis: 150                # 等待连接获取的最大超时时间

  4...mapper/ProductMapper       与mapper映射文件类名相同

 1 package com.wsc.core.mapper;
 2
 3 import com.wsc.core.pojo.Product;
 4
 5 import java.util.List;
 6
 7 public interface ProductMapper {
 8 //查询全部
 9     public List<Product> findAll();
10 //id查询
11     public Product findById(Long id);
12 //插入数据
13     public Boolean add(Product product);
14 }

5...service 接口

 1 package com.wsc.core.service;
 2
 3 import com.wsc.core.pojo.Product;
 4
 5 import java.util.List;
 6
 7 public interface ProductService {
 8     public List<Product> findAll();
 9
10     public Product findById(Long id);
11
12     public Boolean add(Product product);
13 }

6...impl/ProductServiceImpl        注入 ProductMapper

 1 package com.wsc.core.service.impl;
 2
 3 import com.wsc.core.mapper.ProductMapper;
 4 import com.wsc.core.pojo.Product;
 5 import com.wsc.core.service.ProductService;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Service;
 8
 9 import java.util.List;
10
11 /**
12  * @version 1.0
13  * @ClassName ProductServiceImpl
14  * @Description TODO
15  * @Author WSC
16  * @Date 2019/8/27 10:53
17  **/
18 @Service
19 public class ProductServiceImpl implements ProductService {
20     @Autowired
21     private ProductMapper productMapper;
22     @Override
23     public List<Product> findAll() {
24         return productMapper.findAll();
25     }
26
27     @Override
28     public Product findById(Long id) {
29         return productMapper.findById(id);
30     }
31
32     @Override
33     public Boolean add(Product product) {
34         return productMapper.add(product);
35     }
36 }

7...controller/ProductController   写入访问地址

 1 package com.wsc.core.controller;
 2
 3 import com.wsc.core.pojo.Product;
 4 import com.wsc.core.service.ProductService;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.web.bind.annotation.*;
 7
 8 import java.util.List;
 9
10 /**
11  * @version 1.0
12  * @ClassName ProductController
13  * @Description TODO
14  * @Author WSC
15  * @Date 2019/8/27 11:06
16  **/
17 @RestController //返回json数据
18 public class ProductController {
19     @Autowired
20     private ProductService productService;
21     //查询全部
22     @RequestMapping(value ="/product/get/list",method = RequestMethod.GET)
23     public List<Product> getAll(){
24         return productService.findAll();
25     }
26
27 //查询id
28     @RequestMapping(value = "/product/get/{pid}",method = RequestMethod.GET)
29     public Product getById(@PathVariable("pid")Long pid){
30         return productService.findById(pid);
31     }
32
33 //添加
34     @RequestMapping(value ="/product/get/add",method = RequestMethod.POST)
35     public boolean add(@RequestBody Product product){
36         return productService.add(product);
37     }
38 }

   8...start启动类

 1 package com.wsc.core;
 2
 3 import org.mybatis.spring.annotation.MapperScan;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 7
 8 /**
 9  * @version 1.0
10  * @ClassName Start
11  * @Description TODO
12  * @Author WSC
13  * @Date 2019/8/27 10:54
14  **/
15
16 @MapperScan("com.wsc.core.mapper") //mapper扫描包 类 ProductMapper
17 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) //数据库设置访问  可以不加 exclude
18 public class Start {
19     public static void main(String[] args) {
20         SpringApplication.run(Start.class,args);
21     }
22 }

4.....consumer

1...pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>spring-cloud-parent</artifactId>
 7         <groupId>com.wsc</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9         <relativePath>../springCloud/spring-cloud-parent/pom.xml</relativePath>
10     </parent>
11     <modelVersion>4.0.0</modelVersion>
12
13     <artifactId>consumer</artifactId>
14
15
16     <properties>
17         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18         <maven.compiler.source>1.8</maven.compiler.source>
19         <maven.compiler.target>1.8</maven.compiler.target>
20     </properties>
21     <dependencies>
22         <dependency>
23             <groupId>com.wsc</groupId>
24             <artifactId>common</artifactId>
25             <version>${project.version}</version>
26         </dependency>
27         <!--springboot web启动器-->
28         <dependency>
29             <groupId>org.springframework.boot</groupId>
30             <artifactId>spring-boot-starter-web</artifactId>
31         </dependency>
32
33     </dependencies>
34 </project>

  2...resources/application.xml  配置端口号

1 server:
2   port: 800

3....ConfigBean  向容器中添加RestTemplate 组件 直接该组件调用EREST接口

 1 package com.wsc.core.config;
 2
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.web.client.RestTemplate;
 6
 7 /**
 8  * @version 1.0
 9  * @ClassName ConfigBean
10  * @Description TODO
11  * @Author WSC
12  * @Date 2019/8/27 14:32
13  **/
14 @Configuration
15 public class ConfigBean {
16     // 向容器中添加RestTemplate 组件   直接该组件调用EREST接口
17     @Bean
18     public RestTemplate getRestTemplate(){
19         return new RestTemplate();
20     }
21 }

 4....ConfigBeanController 访问地址

 1 package com.wsc.core.controller;
 2
 3 import com.wsc.core.pojo.Product;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.web.bind.annotation.PathVariable;
 6 import org.springframework.web.bind.annotation.RequestBody;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RestController;
 9 import org.springframework.web.client.RestTemplate;
10
11 import java.util.List;
12
13 /**
14  * @version 1.0
15  * @ClassName ConfigBeanController
16  * @Description TODO
17  * @Author WSC
18  * @Date 2019/8/27 14:33
19  **/
20 @RestController
21 public class ConfigBeanController {
22     //provider的访问 端口
23     private static final String REST_URL_PREFIX="http://localhost:8001";
24     @Autowired
25     private RestTemplate restTemplate;
26     @RequestMapping("/consumer/get/add")
27     public boolean add(@RequestBody Product product){
28         return restTemplate.postForObject(REST_URL_PREFIX+"/product/get/add",product,boolean.class);
29     }
30
31     @RequestMapping("/consumer/get/findAll")
32     public List<Product> getAll(){
33        return restTemplate.getForObject(REST_URL_PREFIX+"/product/get/list",List.class);
34     }
35
36     @RequestMapping("/consumer/getById/{pid}")
37     public Product getById(@PathVariable("pid") Long pid){
38         return restTemplate.getForObject(REST_URL_PREFIX+"/product/get/"+pid,Product.class);
39     }
40 }

 5....启动类 

 1 package com.wsc.core;
 2
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5
 6 /**
 7  * @version 1.0
 8  * @ClassName Start_80
 9  * @Description TODO
10  * @Author WSC
11  * @Date 2019/8/27 14:31
12  **/
13 @SpringBootApplication
14 public class Start_80 {
15     public static void main(String[] args) {
16         SpringApplication.run(Start_80.class,args);
17     }
18 }

   

原文地址:https://www.cnblogs.com/wangshichang/p/11420081.html

时间: 2024-10-07 18:51:05

基于springBoot,,springCloud,mybatis 框架简单 微服开发 ==CRUD的相关文章

基于Springboot+SpringCloud的微信点餐系统开发视频教程

基于Springboot+SpringCloud的微信点餐系统开发视频教程课程分享链接:https://pan.baidu.com/s/1q7h9zn8sGf_e0k38pc69tw 密码:fk9w 随着互联网不断发展,大家的生活习惯也不断在改变,像美团,饿了么平台的外卖快速的发展起来,这就是我们所说的O2O,即线上和线下结合同时微信也给人们带来了沟通的便利,现在几乎每个年轻人都会有微信号,所以我们开发微信点餐系统,也是极大方便了大家的使用,只要在打开微信进入我们的点餐系统就可以方便的进行点餐.

Mybatis框架简单使用

Mybatis框架简单使用 环境搭建 新建一个JavaWeb项目,在web\WEB-INF\创建lib文件,并且在其下添加Mybatis的核心包以及依赖包,以及Mysql驱动包,junit4测试包等. 加入配置文件 在src路径下面,新建log4j.properties文件. # Global logging configuration log4j.rootLogger=DEBUG, stdout # Console output... log4j.appender.stdout=org.apa

SpringBoot+SpringCloud +Mybatis+Restful 企业分布式微服务

1.介绍 Commonservice-system是一个大型分布式.微服务.面向企业的JavaEE体系快速研发平台,基于模块化.服务化.原子化.热插拔的设计思想,使用成熟领先的无商业限制的主流开源技术构建.采用服务化的组件开发模式,可实现复杂的业务功能.提供驱动式开发模式,整合内置的代码生成器,将JavaEE开发效率提高5倍以上,减少50%的代码开发量,解决80%的重复工作,让开发者更关注业务逻辑.使用Maven进行项目的构建管理,采用Jenkins进行持续集成,主要定位于大型分布式企业系统或大

6.3 Springboot整合Mybatis框架

一.整合Mybatis框架 1.步骤: 在整合Druid的基础上继续(略) pom.xml中添加mybatis依赖 编写实体类 编写mapper层(dao层) 编写mapper映射文件 在application.yml文件中添加配置 编写控制器 2.pom.xml中添加mybatis依赖 <!-- 引入 myBatis,这是 MyBatis官方提供的适配 Spring Boot 的,而不是Spring Boot自己的--> <dependency> <groupId>o

基于Spring与Mybatis框架的网络购物系统设计与实现,免费分享

大家好,我是全微毕设团队的创始人,本团队擅长JAVA(SSM,SSH,SPRINGBOOT).PYTHON.PHP.C#.安卓等多项技术. 今天将为大家分析一个电脑配件网络购物(本设计采用JAVA+MySQL技术,设计了一种基于B/S模式的在线购物系统.后台可以发布商品信息.维护商品.管理订单.管理评论等.),该项目使用框架为SSM(MYECLIPSE),选用开发工具为MYECLIPSE.电脑配件网络购物为一个 后台项目. 为了完成该系统,我们首先需要对该系统进行需求分析.一个电脑配件网络购物应

MyBatis框架之注解开发

MyBatis注解开发 @Insert注解注解属性value:写入SQL语句 @Options注解实现添加新数据的主键封装注解属性useGeneratedKeys:使用生成的主键,配置为truekeyProperty:主键封装的pojo对象属性 @SelectKey注解实现添加新数据的主键封装注解属性statement:要执行的SQL语句before:在添加SQL语句之前还是之后进行,配置为false keyProperty:主键封装的pojo对象属性 注解实现添加数据 UserMapper接口

一套基于SpringBoot+Vue+Shiro 前后端分离 开发的代码生成器

一.前言 最近花了一个月时间完成了一套基于Spring Boot+Vue+Shiro前后端分离的代码生成器,目前项目代码已基本完成 止步传统CRUD,进阶代码优化: 该项目可根据数据库字段动态生成 controller.mapper.service.html.jsp.vue.php..py ... 等各种类型代码,采用 velocity 模板引擎在页面动态配置生成代码,前后端动态权限配置,前端权限精确到 按钮 级别,后端权限精确到 uri 上,QQ授权第三方单用户登录...等 基本环境: JDK

spring+springMvc+mybatis框架简单实例

web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/ja

Mybatis之使用注解开发CRUD

上一篇演示了如何使用XML来操作Mybatis实现CRUD,但是大量的XML配置文件的编写是非常烦人的.因此 Mybatis也提供了基于注解的配置方式,下面我们来演示一下使用接口加注解来实现CRUD的的例子. 首先是创建一个接口. package com.bird.mybatis.bean; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotation