SpringBoot学习笔记-使用jdbcTemplate访问mysql

配置maven依赖

使用jdbcTemplate访问jdbc,需要在pom中添加mysql和spring-boot-start-jdbc依赖

<dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

配置数据源

在application.properties中配置数据库地址,账号密码等,springboot会配置一个DataSource,自动注入到jdbcTemplate中,驱动可以不配置,springboot可以通过url推测出来

spring.datasource.url=jdbc:mysql://localhost:3306/world
spring.datasource.username=root
spring.datasource.password=1234

测试 

192149_z17k_2323617.png 

192242_X3Mr_2323617.png 

完整程序

pom.xml

<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.endless</groupId>
  <artifactId>springboot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springboot</name>
  <url>http://maven.apache.org</url>
  
  <parent>
  	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-starter-parent</artifactId>
  	<version>1.4.0.RELEASE</version>
  </parent>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  
  		<dependency>
  			<groupId>org.springframework.boot</groupId>	
  			<artifactId>spring-boot-starter-web</artifactId>
  		</dependency>
  		
  		<dependency>
       	 	<groupId>mysql</groupId>
        	<artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
        	<groupId>org.springframework.boot</groupId>
       		<artifactId>spring-boot-starter-jdbc</artifactId>
       	</dependency>
    
  	  	<dependency>
      		<groupId>junit</groupId>
      		<artifactId>junit</artifactId>
     	 	<scope>test</scope>
    	</dependency>
    	
  </dependencies>
 
  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.springframework.boot</groupId>
  			<artifactId>spring-boot-maven-plugin</artifactId>
  		</plugin>
  	</plugins>
  </build>

</project>

WorldService

package com.endless.springboot.jdbc.services;

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 com.endless.springboot.jdbc.entity.City;


@Service
public class WorldService {

    @Autowired
    private JdbcTemplate jdbcTemplate;
   
    public List<City> listCities(){
        String sql = "select * from city where countryCode='CHN' ";

        List<City> cities = jdbcTemplate.query(sql, new RowMapper<City>() {
            @Override
            public City mapRow(ResultSet resultSet, int i) throws SQLException {
                City city = new City();
                	city.setId(resultSet.getInt("ID"));
                	city.setName(resultSet.getString("Name"));
                	city.setCountryCode(resultSet.getString("CountryCode"));
                	city.setDistrict(resultSet.getString("District"));
                	city.setPopulation(resultSet.getInt("Population"));
                return city;
            }
        });

        return cities;
    }

}

App

package com.endless.springboot.jdbc;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.endless.springboot.jdbc.entity.City;
import com.endless.springboot.jdbc.services.WorldService;


@RestController
@SpringBootApplication
@RequestMapping("jdbc")
public class App {
	
	@Autowired
	private WorldService service;
	
	@RequestMapping("cities")
	public List<City> sayHello(){
		return service.listCities();
	}
    public static void main( String[] args ){
    	SpringApplication.run(App.class,args);
    }
}

 

转载于:https://my.oschina.net/Endless2010/blog/1542559

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值