spring boot 使用 JdbcTemplate 连接oracle数据库 及修改启动端口方法

spring boot 连接oracle数据库
只需要在application.properties中配置数据库连接信息即可
具体步骤按下面的流程贴贴就ok了

第一步先配置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.github.abel533</groupId>
     <artifactId>spring-boot</artifactId>
     <version>1.0-SNAPSHOT</version>
     <parent>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-parent</artifactId>
           <version>1.3.0.RELEASE</version>
     </parent>
     <dependencies>
           <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
           </dependency>
           <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
           </dependency>
           <dependency>
                <groupId>oracle.jdbc.driver</groupId>
                <artifactId>ojdbc14</artifactId>
                <version>10.2.0.1.0</version>
           </dependency>
     </dependencies>
     <build>
           <plugins>
                <plugin>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-maven-plugin</artifactId>
                     <dependencies>
                           <dependency>
                                <groupId>org.springframework</groupId>
                                <artifactId>springloaded</artifactId>
                                <version>1.2.5.RELEASE</version>
                           </dependency>
                     </dependencies>
                </plugin>
           </plugins>
     </build>
</project>

第二步 application.properties配置文件:

spring.main.banner-mode=off

# Set true for first time db initialization.
spring.datasource.initialize=false

spring.datasource.url=jdbc:oracle:thin:@192.168.70.102:1521:ora11gR2
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-oracle.jdbc.driver.OracleDriver

# dbcp2 settings
# spring.datasource.dbcp2.*

spring.datasource.dbcp2.initial-size=7
spring.datasource.dbcp2.max-total=20
spring.datasource.dbcp2.pool-prepared-statements=true

#port
server.port=8080
第三步 application.java

package com.koolyun.msc.notify;
import static java.lang.System.exit;
import java.util.Date;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
@SpringBootApplication
@RequestMapping("rest")
public class Application implements CommandLineRunner,EmbeddedServletContainerCustomizer {
     @Autowired
     CustomerRepository customerRepository;

     @RequestMapping("/123")
     String home() {
           return "Hello World!";
     }

     @RequestMapping("/now")
     String hehe() {
           return "现在时间:" + (new Date()).toLocaleString();
     }
     @RequestMapping("/find")
     String haha() {
           return customerRepository.findAll().toString();
     }
     public static void main(String[] args) {
           SpringApplication.run(Application.class, args);
           System.out.println("start successfuly!!");
     }

     @Override
     public void run(String... arg0) throws Exception {
      System.out.println("starting -----------");
      System.out.println("starting -----------");
      System.out.println("starting -----------");
     }
               //设置启动端口
     @Override
     public void customize(ConfigurableEmbeddedServletContainer arg0) {
           arg0.setPort(8088);
     }
}
第四步 customer实体类:
package com.koolyun.msc.notify;
import java.util.Date;
public class Customer {

    int app_id;
    String api_id;

    public Customer(int app_id, String api_id) {
        this.app_id = app_id;
        this.api_id = api_id;
    }

    public Customer() {
           super();
     }

     @Override
    public String toString() {
        return "Customer{" +
                "id=" + app_id +
                ", name='" + api_id + '\'' +'}';
    }
     public synchronized int getApp_id() {
           return app_id;
     }
     public synchronized void setApp_id(int app_id) {
           this.app_id = app_id;
     }
     public synchronized String getApi_id() {
           return api_id;
     }
     public synchronized void setApi_id(String api_id) {
           this.api_id = api_id;
     }
}
第五步 CustomerRepository 数据库操作:
package com.koolyun.msc.notify;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class CustomerRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Customer> findAll() {
        List<Customer> result = jdbcTemplate.query(
                "SELECT app_id, api_id   FROM o_app_api where app_id=599",
                new UserRowMapper()
        );
        return result;
    }
    public class UserRowMapper implements RowMapper<Customer> {

        @Override
        public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
            Customer user = new Customer();
            user.setApp_id(rs.getInt("app_id"));
            user.setApi_id(rs.getString("api_id"));
            return user;
        }
    }
}

实验结果:


修改spring-boot启动端口

1、java -jar 打包之后的SpringBoot.jar  --server.port=8000
2、实现EmbeddedServletContainerCustomizer直接设置port
@Override
     public void customize(ConfigurableEmbeddedServletContainer arg0) {
           arg0.setPort(8088);
     }
3、在application.properties 中添加
#port
server.port=8080


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值