300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Eclipse搭建SpringCloud+SSM+Maven项目

Eclipse搭建SpringCloud+SSM+Maven项目

时间:2021-03-29 07:02:51

相关推荐

Eclipse搭建SpringCloud+SSM+Maven项目

1、使用的是eclipse

2、本次创建的是client端,server是已存在的

首先新创建一个Spring Starter Project

填写项目基本信息

选取项目需要的依赖(后期也可以在pom.xml中补充),选择后,点击finish

这是项目创建后,中间有些包是我自己创建的,可以按照需要补充

这是application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT&useAffectedRows=true&allowPublicKeyRetrieval=truespring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.cj.jdbc.Drivermybatis.configuration.map-underscore-to-camel-case=true#设置SpringCloudspring.application.name=system-springclouddemoserver.port=10090#使用ip地址向eureka注册中心注册eureka.instance.prefer-ip-address=trueeureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}eureka.client.service-url.defaultZone=http://**********/eureka/mybatis.type-aliases-package=com.****.zgy.entityspring.freemarker.suffix=.htmlspring.freemarker.template-loader-path=classpath:/templates/

下面是pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.1</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.*****.zgy</groupId><artifactId>system-springclouddemo1</artifactId><version>${project.release.version}</version><name>system-springclouddemo1</name><description>Demo project for Spring Boot</description><!-- 指定了依赖的版本信息 --><properties><project.release.version>1.0.0-SNAPSHOT</project.release.version><java.version>8</java.version><spring-cloud.version>.0.0</spring-cloud.version></properties><!-- 多环境配置 --><profiles><profile><id>dev</id></profile><profile><id>prod</id><properties><project.release.version>1.0.0</project.release.version></properties></profile></profiles><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-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>2.1.4</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- 可以使用@entity等注解 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- 为pojo类提供持久化 --><dependency><groupId>javax.persistence</groupId><artifactId>persistence-api</artifactId><version>1.0.2</version></dependency><!--访问静态资源--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url></repository></repositories></project>

启动类

package com.*****.zgy;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.ponentScan;@SpringBootApplication@ComponentScan("com.*****.zgy.controller,com.*****.zgy.dao,com.*****.zgy.entity,com.*****.zgy.service")public class SystemSpringclouddemo1Application {public static void main(String[] args) {SpringApplication.run(SystemSpringclouddemo1Application.class, args);}}

controller

package com.***.zgy.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.servlet.ModelAndView;import com.***.zgy.entity.Product;import com.***.zgy.service.ProductService;@RestController@RequestMapping("/Product")public class SearchProductController {@Autowiredprivate ProductService productService;@RequestMapping("/")public ModelAndView goHome() {System.out.println("get OK");return new ModelAndView("index");}@RequestMapping(value = "/SearchProduct/{id}", method = RequestMethod.GET,produces = "application/json;charset=utf-8")public Product SearchProductById(@PathVariable Integer id) {Product product = productService.SearchProductById(id);return product;}}

dao

package com.***.zgy.dao;import org.apache.ibatis.annotations.Mapper;import com.***.zgy.entity.Product;@Mapperpublic interface ProductMapper {Product SearchProductById(Integer id);}

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-////DTD Mapper 3.0//EN""/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.***.zgy.dao.ProductMapper"><select id="SearchProductById" parameterType="int" resultType="com.***.zgy.entity.Product">select * from tb_product where id = #{ID}</select></mapper>

entity实体类

package com.***.zgy.entity;import javax.persistence.Entity;import javax.persistence.Id;import lombok.Data;@Entity@Datapublic class Product {@Idprivate Integer ID;private String productName;private Integer status;private Double price;private String productDesc;private String caption;private Integer inventory;}

service

package com.***.zgy.service;import org.springframework.stereotype.Service;import com.***.zgy.entity.Product;@Servicepublic interface ProductService {Product SearchProductById(Integer id);}

package com.***.zgy.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.***.zgy.dao.ProductMapper;import com.***.zgy.entity.Product;@Servicepublic class ProductServiceImpl implements ProductService {@Autowiredprivate ProductMapper productMapper;@Overridepublic Product SearchProductById(Integer id) {// TODO Auto-generated method stubProduct product = productMapper.SearchProductById(id);return product;}}

至此结束。如有不妥,还望指正。谢谢^ _ ^

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。