springboot整合springmvc+hibernate+swagger
阿新 • • 發佈:2019-01-12
SpringBoot+SpringMvc+Hibernate整合
SpringBoot+SpringMvc+Hibernate+Swagger簡單的整合步驟 。使用Eclipse工具1、新建一個maven專案
2、pom.xm設定
<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>cn.itcast.demo</groupId> <artifactId>springBootDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <java.version>1.7</java.version> </properties> <!-- 引入springBoot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <!-- 配置中央倉庫 --> <repositories> <repository> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <!-- 整合 springMvc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 整合 Hibernate --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- 模板 thymeleaf Demo --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- swagger 介面文件 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency> <!-- 熱部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <!-- 單元測試 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 資料庫連線池 --> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> --> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- oracle --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.1.0</version> </dependency> </dependencies> </project>
3、配置application.properties
#mysql\u6570\u636E\u5E93\u914D\u7F6E spring.datasource.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.max-idle=10 spring.datasource.max-wait=10000 spring.datasource.min-idle=5 spring.datasource.initial-size=5 spring.jpa.database = MYSQL spring.jpa.show-sql = true spring.jpa.hibernate.ddl-auto = update spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect #oracle\u6570\u636E\u5E93\u914D\u7F6E #spring.datasource.url=jdbc:oracle:thin:@192.168.80.10:1521:orcl #spring.datasource.username=wateruser #spring.datasource.password=itcast #spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver #spring.datasource.max-idle=10 #spring.datasource.max-wait=10000 #spring.datasource.min-idle=5 #spring.datasource.initial-size=5 server.port=8080 #Swagger Default Access Address : http://localhost:8080/swagger-ui.html
3、Repository介面層
package com.springboot.service.repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import com.springboot.service.entity.User; public interface UserRepository extends JpaRepository<User, Integer> { @Query(value = "SELECT u FROM User u WHERE u.id = ?") public User getUserById(Integer id); }
4、實體類Entity層
package com.springboot.service.entity;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue
@Column(name = "id")
private Integer id;
@Column(name = "username")
private String username;
@Column(name = "birthday")
private Date birthday;
@Column(name = "sex")
private String sex;
@Column(name = "address")
private String address;
省略set/get方法
}
5.web控制層
package com.springboot.web;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.springboot.service.entity.User;
import com.springboot.service.repository.UserRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping(value = "/user")
@Api(value = "使用者資訊")
@EnableAutoConfiguration
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping(value = "/getUserById")
@ApiOperation(value = "通過使用者ID查詢使用者資訊")
public User getUserById(@RequestParam(name = "id") Integer id){
List<User> users = userRepository.findAll();
return userRepository.getUserById(id);
}
}
6、Application啟動類
package com.springboot.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@ComponentScan(basePackages={"com.springboot"}) // 掃描該包路徑下的所有spring元件
@EnableJpaRepositories("com.springboot.service.repository") // JPA掃描該包路徑下的Repositorie
@EntityScan("com.springboot.service.entity") // 掃描實體類
@EnableSwagger2 // Swagger
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
7、配置Swagger 的一個Swagger配置類,放在啟動類掃描的包下,建議和啟動類放在同一目錄下;
package com.springboot.application;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.springboot.web"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot利用swagger構建api文件")
.description("簡單優雅的restfun風格")
.termsOfServiceUrl("http://blog.csdn.net/saytime")
.version("2.0")
.build();
}
}
Run as --- java Application就可以啟動專案
注意:
maven除了本地化之外,還需在 .../conf/setting.xml檔案中,增加以下配置,主要和工程中配置中央倉庫對應,本地倉庫沒有的依賴jar包,可以從中央倉庫中下載。
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
springboot整合springmvc+hibernate+swagger