1、修改端口号主要是通过配置文件修改。如图:
完整版配置
########################################################
###server 配置信息
########################################################
#spring boot 端口号配置
server.port = 8081
#spring boot 默认访问路径是/
server.context-path = /springboot
server.tomcat.uri-encoding = UTF-8 #######################################################
##datasource -- 指定mysql数据库连接信息.
#######################################################
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10 ########################################################
### Java Persistence Api -- Spring jpa的配置信息.
########################################################
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
#[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy]
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
此时的访问路径就会改为
http://127.0.0.1:8080/springboot/cat/selectByCatName?catName=catName(使用的名字必须是唯一的)
项目完整搭建
1、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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.spring</groupId>
<artifactId>boot_jpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>boot_jpa</name>
<url>http://maven.apache.org</url>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!--自定义配置文件-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency> <!-- 添加fastjson 依赖包. -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency> <!-- spring boot devtools 依赖包. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency> <!-- 添加MySQL数据库驱动依赖包. -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <!-- 添加Spring-data-jpa依赖. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--fork : 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart -->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build> </project>
2、实体类搭建
package com.spring.boot.jap.perform.pojo; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; /**
* Created by liuya on 2018-01-26.
*/
@Entity
public class Cat { /**
* 使用@Id指定主键.
* <p>
* 使用代码@GeneratedValue(strategy=GenerationType.AUTO)
* 指定主键的生成策略,mysql默认的是自增长。
*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;//主键. private String catName;//姓名. cat_name private int catAge;//年龄. cat_age; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getCatName() {
return catName;
} public void setCatName(String catName) {
this.catName = catName;
} public int getCatAge() {
return catAge;
} public void setCatAge(int catAge) {
this.catAge = catAge;
} }
3、dao搭建
package com.spring.boot.jap.perform.dao; import com.spring.boot.jap.perform.pojo.Cat;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository; import javax.annotation.Resource; /**
* Created by liuya on 2018-01-27.
* <p>
* 使用@Repository注解,标注这是一个持久化操作对象.
*/ @Repository
public class CatDao { @Resource
private JdbcTemplate jdbcTemplate; /**
* 1、定义一个Sql语句;
* 2、定义一个RowMapper.
* 3、执行查询方法.
*/
public Cat selectByCatName(String catName) { //查询的sql语句
String sql = "select * from cat where cat_name=?";
//封装数据的mapper
RowMapper<Cat> rowMapper = new BeanPropertyRowMapper<>(Cat.class);
//执行语句的位置
Cat cat = jdbcTemplate.queryForObject(sql,new Object[]{catName},rowMapper); //返回封装类
return cat;
} }
4、service搭建
package com.spring.boot.jap.perform.service; import com.spring.boot.jap.perform.dao.CatDao;
import com.spring.boot.jap.perform.pojo.Cat;
import org.springframework.stereotype.Service; import javax.annotation.Resource;
import javax.transaction.Transactional; /**
* Created by liuya on 2018-01-27.
*/ @Service
public class CatService { @Resource
private CatDao catDao; /**
* 通过名称查询用户的信息
*
* @param catName
* @return
*/
public Cat selectByCatName(String catName) {
return catDao.selectByCatName(catName);
} }
5、controller搭建
package com.spring.boot.jap.perform.controller; import com.spring.boot.jap.perform.pojo.Cat;
import com.spring.boot.jap.perform.service.CatService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /**
* Created by liuya on 2018-01-27.
*/ @RestController
@RequestMapping("/cat")
public class CatController { @Resource
private CatService catService; //新建用户信息(修改了application.properties后需要下边方式访问)
//访问连接:http://127.0.0.1:8080/springboot/cat/selectByCatName?catName=catName(使用的名字必须是唯一的)
@RequestMapping("/selectByCatName")
public Cat selectByCatName(String catName) {
return catService.selectByCatName(catName);
} }
6、application配置在上边