Spring Boot 连接ORACLE

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011134399/article/details/79034296

JDBC 连接数据库

1、属性配置文件(application.properties)

spring.datasource.driver-class-name: oracle.jdbc.driver.OracleDriver
spring.datasource.url: jdbc:oracle:thin:@192.168.0.11:1521:orcl
spring.datasource.username: ljkj_test
spring.datasource.password: ljkj_test

2、pom.xml 配置maven依赖

 <!-- Spring Boot JDBC --> 
<dependency> 
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- oracle -->
<dependency>
       <groupId>com.oracle</groupId>
       <artifactId>ojdbc14</artifactId>
       <version>10.2.0.4.0</version>
</dependency>
3、实体类

 

package spring.boot.entity;

public class bnUser {
	
	private long userId;
	private long userPhone;
	private String userName;
	private double hisMoney;
	
	
	public long getUserId() {
		return userId;
	}
	public long getUserPhone() {
		return userPhone;
	}
	public String getUserName() {
		return userName;
	}
	public double getHisMoney() {
		return hisMoney;
	}
	public void setUserId(long userId) {
		this.userId = userId;
	}
	public void setUserPhone(long userPhone) {
		this.userPhone = userPhone;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public void setHisMoney(double hisMoney) {
		this.hisMoney = hisMoney;
	}
	
	@Override
	public String toString() {

		return "{userId:"+userId+",userPhone:"+userPhone+",userName:"+userName+",hisMoney:"+hisMoney+"}";
	}
}
4、service

  

package spring.boot.service;

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.Service;

import spring.boot.entity.bnUser;

@Service
public class testService {
 
    @Autowired
    private JdbcTemplate jdbcTemplate;
 
    public List getList(){
    	
        String sql = "select user_id,user_phone,user_name,his_money from bn_user";
        return (List) jdbcTemplate.query(sql, new RowMapper(){
 
            public bnUser mapRow(ResultSet rs, int rowNum) throws SQLException {
                bnUser bnUser = new bnUser();
                bnUser.setUserId(rs.getLong("user_id"));
                bnUser.setUserPhone(rs.getLong("user_phone"));
                bnUser.setUserName(rs.getString("user_name"));
                bnUser.setHisMoney(rs.getDouble("his_money"));
                
                return bnUser;
            }
 
        });
    }
}

5、Controller

package spring.boot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import spring.boot.service.testService;

@RestController
public class testController {
	
	@Autowired
	private testService test;
	
	@RequestMapping("/index")
	public List login(@RequestParam("id") Integer id){
		
		
		return test.getList();
	}
}

6、Main

package spring.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableAutoConfiguration
public class springBootMain {
	
	
	
	public static void main(String[] args){
		
		SpringApplication.run(springBootMain.class, args);
	}
}




猜你喜欢

转载自blog.csdn.net/u011134399/article/details/79034296