生活随笔
收集整理的這篇文章主要介紹了
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-springclouddemoserver.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.entityspring.freemarker.suffix=.html
spring.freemarker.template-loader-path=classpath:/templates/
<?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/> </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><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><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
);}}
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
;}}
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 mapperPUBLIC "-//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>
package com
.*
**.zgy
.entity
;import javax
.persistence
.Entity
;
import javax
.persistence
.Id
;import lombok
.Data
;@Entity
@Data
public class Product {@Idprivate Integer ID
;private String productName
;private Integer status
;private Double price
;private String productDesc
;private String caption
;private Integer inventory
;
}
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 {@Autowiredprivate ProductMapper productMapper
;@Overridepublic Product
SearchProductById(Integer id
) {Product product
= productMapper
.SearchProductById(id
);return product
;}}
至此結束。如有不妥,還望指正。謝謝^ _ ^
總結
以上是生活随笔為你收集整理的Eclipse搭建SpringCloud+SSM+Maven项目的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。