IntelliJ IDEA 2017版 spring-boot修改端口號配置把端口號改成8081

一、修改端口號主要是經過配置文件修改。如圖:
java

完整版配置mysql

 1 ########################################################  2 ###server 配置信息  3 ########################################################  4 #spring boot 端口號配置  5 server.port = 8081  6 #spring boot 默認訪問路徑是/  7 server.context-path = /springboot  8 server.tomcat.uri-encoding = UTF-8  9 
10 
11 ####################################################### 12 ##datasource -- 指定mysql數據庫鏈接信息. 13 ####################################################### 14 spring.datasource.url = jdbc:mysql://localhost:3306/test 15 spring.datasource.username = root 16 spring.datasource.password = 123456 17 spring.datasource.driverClassName = com.mysql.jdbc.Driver 18 spring.datasource.max-active=20 19 spring.datasource.max-idle=8 20 spring.datasource.min-idle=8 21 spring.datasource.initial-size=10 22 
23 
24 ######################################################## 25 ### Java Persistence Api -- Spring jpa的配置信息. 26 ######################################################## 27 # Specify the DBMS 28 spring.jpa.database = MYSQL 29 # Show or not log for each sql query 30 spring.jpa.show-sql = true 31 # Hibernate ddl auto (create, create-drop, update) 32 spring.jpa.hibernate.ddl-auto = update 33 # Naming strategy 34 #[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy] 35 spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy 36 # stripped before adding them to the entity manager) 37 spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
View Code

此時的訪問路徑就會改成web

http://127.0.0.1:8080/springboot/cat/selectByCatName?catName=catName(使用的名字必須是惟一的)
項目完整搭建
一、pom.xml搭建
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.spring</groupId>
 7     <artifactId>boot_jpa</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>boot_jpa</name>
12     <url>http://maven.apache.org</url>
13     <description>Demo project for Spring Boot</description>
14 
15     <parent>
16         <groupId>org.springframework.boot</groupId>
17         <artifactId>spring-boot-starter-parent</artifactId>
18         <version>1.5.9.RELEASE</version>
19         <relativePath/> <!-- lookup parent from repository -->
20     </parent>
21 
22     <properties>
23         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
25         <java.version>1.8</java.version>
26     </properties>
27 
28     <dependencies>
29         <dependency>
30             <groupId>org.springframework.boot</groupId>
31             <artifactId>spring-boot-starter-web</artifactId>
32         </dependency>
33 
34         <dependency>
35             <groupId>org.springframework.boot</groupId>
36             <artifactId>spring-boot-starter-test</artifactId>
37             <scope>test</scope>
38         </dependency>
39 
40         <!--自定義配置文件-->
41         <dependency>
42             <groupId>com.alibaba</groupId>
43             <artifactId>fastjson</artifactId>
44             <version>1.2.15</version>
45         </dependency>
46 
47         
48         <!-- 添加fastjson 依賴包. -->
49         <dependency>
50             <groupId>com.alibaba</groupId>
51             <artifactId>fastjson</artifactId>
52             <version>1.2.15</version>
53         </dependency>
54 
55         <!-- spring boot devtools 依賴包. -->
56         <dependency>
57             <groupId>org.springframework.boot</groupId>
58             <artifactId>spring-boot-devtools</artifactId>
59             <optional>true</optional>
60             <scope>true</scope>
61         </dependency>
62 
63         <!-- 添加MySQL數據庫驅動依賴包. -->
64         <dependency>
65             <groupId>mysql</groupId>
66             <artifactId>mysql-connector-java</artifactId>
67         </dependency>
68 
69         <!-- 添加Spring-data-jpa依賴. -->
70         <dependency>
71             <groupId>org.springframework.boot</groupId>
72             <artifactId>spring-boot-starter-data-jpa</artifactId>
73         </dependency>
74 
75 
76     </dependencies>
77 
78     <build>
79         <plugins>
80             <plugin>
81                 <groupId>org.springframework.boot</groupId>
82                 <artifactId>spring-boot-maven-plugin</artifactId>
83                 <configuration>
84                     <!--fork : 若是沒有該項配置,肯呢個devtools不會起做用,即應用不會restart -->
85                     <fork>true</fork>
86                 </configuration>
87             </plugin>
88         </plugins>
89     </build>
90 
91 
92 </project>
View Code

二、實體類搭建spring

 1 package com.spring.boot.jap.perform.pojo;  2 
 3 import javax.persistence.Entity;  4 import javax.persistence.GeneratedValue;  5 import javax.persistence.GenerationType;  6 import javax.persistence.Id;  7 
 8 /**
 9  * Created by liuya on 2018-01-26. 10  */
11 @Entity 12 public class Cat { 13 
14     /**
15  * 使用@Id指定主鍵. 16  * <p> 17  * 使用代碼@GeneratedValue(strategy=GenerationType.AUTO) 18  * 指定主鍵的生成策略,mysql默認的是自增加。 19      */
20  @Id 21     @GeneratedValue(strategy = GenerationType.AUTO) 22     private int id;//主鍵.
23 
24     private String catName;//姓名. cat_name
25 
26     private int catAge;//年齡. cat_age;
27 
28     public int getId() { 29         return id; 30  } 31 
32     public void setId(int id) { 33         this.id = id; 34  } 35 
36     public String getCatName() { 37         return catName; 38  } 39 
40     public void setCatName(String catName) { 41         this.catName = catName; 42  } 43 
44     public int getCatAge() { 45         return catAge; 46  } 47 
48     public void setCatAge(int catAge) { 49         this.catAge = catAge; 50  } 51 
52 }
View Code

三、dao搭建sql

 1 package com.spring.boot.jap.perform.dao;  2 
 3 import com.spring.boot.jap.perform.pojo.Cat;  4 import org.springframework.jdbc.core.BeanPropertyRowMapper;  5 import org.springframework.jdbc.core.JdbcTemplate;  6 import org.springframework.jdbc.core.RowMapper;  7 import org.springframework.stereotype.Repository;  8 
 9 import javax.annotation.Resource; 10 
11 /**
12  * Created by liuya on 2018-01-27. 13  * <p> 14  * 使用@Repository註解,標註這是一個持久化操做對象. 15  */
16 
17 
18 @Repository 19 public class CatDao { 20 
21  @Resource 22     private JdbcTemplate jdbcTemplate; 23 
24     /**
25  * 一、定義一個Sql語句; 26  * 二、定義一個RowMapper. 27  * 三、執行查詢方法. 28      */
29     public Cat selectByCatName(String catName) { 30 
31         //查詢的sql語句
32         String sql = "select * from cat where cat_name=?"; 33         //封裝數據的mapper
34         RowMapper<Cat> rowMapper = new BeanPropertyRowMapper<>(Cat.class); 35         //執行語句的位置
36         Cat cat = jdbcTemplate.queryForObject(sql,new Object[]{catName},rowMapper); 37 
38         //返回封裝類
39         return cat; 40  } 41 
42 }
View Code

四、service搭建數據庫

 1 package com.spring.boot.jap.perform.service;  2 
 3 import com.spring.boot.jap.perform.dao.CatDao;  4 import com.spring.boot.jap.perform.pojo.Cat;  5 import org.springframework.stereotype.Service;  6 
 7 import javax.annotation.Resource;  8 import javax.transaction.Transactional;  9 
10 /**
11  * Created by liuya on 2018-01-27. 12  */
13 
14 
15 @Service 16 public class CatService { 17 
18  @Resource 19     private CatDao catDao; 20 
21     /**
22  * 經過名稱查詢用戶的信息 23  * 24  * @param catName 25  * @return
26      */
27     public Cat selectByCatName(String catName) { 28         return catDao.selectByCatName(catName); 29  } 30 
31 
32 }
View Code

五、controller搭建apache

 1 package com.spring.boot.jap.perform.controller;  2 
 3 import com.spring.boot.jap.perform.pojo.Cat;  4 import com.spring.boot.jap.perform.service.CatService;  5 import org.springframework.web.bind.annotation.RequestMapping;  6 import org.springframework.web.bind.annotation.RestController;  7 
 8 import javax.annotation.Resource;  9 
10 /**
11  * Created by liuya on 2018-01-27. 12  */
13 
14 
15 @RestController 16 @RequestMapping("/cat") 17 public class CatController { 18 
19  @Resource 20     private CatService catService; 21 
22     //新建用戶信息(修改了application.properties後須要下邊方式訪問) 23     //訪問鏈接:http://127.0.0.1:8080/springboot/cat/selectByCatName?catName=catName(使用的名字必須是惟一的)
24     @RequestMapping("/selectByCatName") 25     public Cat selectByCatName(String catName) { 26         return catService.selectByCatName(catName); 27  } 28 
29 
30 }
View Code

六、application配置在上邊json

相關文章
相關標籤/搜索