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=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.configuration.map-underscore-to-camel-case=true

#设置SpringCloud
spring.application.name=system-springclouddemo

server.port=10090

#使用ip地址向eureka注册中心注册
eureka.instance.prefer-ip-address=true
eureka.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.entity

spring.freemarker.suffix=.html
spring.freemarker.template-loader-path=classpath:/templates/
  • 下面是pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/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>2020.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.context.annotation.ComponentScan;

@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 {

    @Autowired
    private 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;

@Mapper
public interface ProductMapper {
    Product SearchProductById(Integer id);
}

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/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
@Data
public class Product {
    @Id
    private 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;

@Service
public 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;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public Product SearchProductById(Integer id) {
        // TODO Auto-generated method stub
        Product product = productMapper.SearchProductById(id);
        return product;
    }

}

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值