博客详情

springboot中集成jdbcTemplate (原创)

作者: 朝如青丝暮成雪
发布时间:2018-09-01 06:51:06  文章分类:springboot   阅读(1327)  评论(0)

springboot工程,可以通过 STS中starter project向导创建。 

springboot中集成jdbcTemplate 操作数据库。 


1、pom.xml中引入 spring-boot-starter-jdbc 和 mysql jdbc驱动包。


 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
           
          <!-- spring-boot test支持  -->
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-test</artifactId> 
            <scope>test</scope> 
        </dependency> 
         
         
         <!-- spring boot  devtools 开发者工具 -->
       <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-devtools</artifactId> 
            <optional>true</optional>
       </dependency> 
 
    <!-- servlet api 包 -->
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
           <!-- Overriding managed version 3.1.0 for javax.servlet-api -->
           <!--  <version>3.1.0</version> -->
          <scope>provided</scope>
      </dependency>
       
         <!-- tomcat支持 -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-tomcat</artifactId>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.apache.tomcat.embed</groupId>
           <artifactId>tomcat-embed-jasper</artifactId>
           <scope>provided</scope>
       </dependency>
        <!-- jstl标签库 -->
        <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>jstl</artifactId>
       </dependency>
      
 
 
         <!-- jdbc starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
         
        <!-- mysql jdbc连接驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <!--5.1.46  --> 
        </dependency>


2、application.yml 中配置spring.datasource数据库连接参数
server:
  port: 7004

#dev热加载工具
spring:
  devtools:
    restart: 
      enabled: true
      exclude:  resouces/**
  
  #配置jdbc数据库连接
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/db_springdemo3?useUnicode=true&characterEncoding=UTF8&useSSL=false
    driver-class-name: com.mysql.jdbc.Driver
      
        
#jsp视图
  mvc: 
    view: 
      prefix: /WEB-INF/pages
      #suffix: .jsp 


3 、业务类StudentService.java 中使用@Autowire注入jdbcTemplate  


package com.tingcream.springdemo3.service;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
 
import com.tingcream.springdemo3.model.Student;
 
@Service
public class StudentService {
 
    @Autowired
    private JdbcTemplate jdbcTemplate ;
     
   
    /**
     * 查询所有
     * @return
     */
    public  List<student> findAll(){
        String sql="select  * from t_student";
        return  jdbcTemplate.query(sql, new BeanPropertyRowMapper<student>(Student.class));
    }
     
    /**
     * 根据id查询 
     * @param id
     * @return
     */
    public  Student findById(Integer id) {
        String sql ="select * from  t_student where id=?";
         RowMapper<student> rowMapper = new BeanPropertyRowMapper<student>(Student.class);
        List<student> list = jdbcTemplate.query(sql, rowMapper,new Object[] {id});
          
         if(list!=null && list.size()>0) {
             return list.get(0);
         }else {
             return  null;
         }
    }
}


4、测试代码 (StudentTest.java)
package com.tingcream.springdemo3.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.tingcream.springdemo3.model.Student;
import com.tingcream.springdemo3.service.StudentService;

@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentTest {
	@Autowired
	private StudentService studentService ;
	@Test
	public   void test1() {
		Student student =studentService.findById(1);
		System.out.println(student);
	}
}




关键字:  springboot  jdbcTemplate  test
评论信息
暂无评论
发表评论

亲,您还没有登陆,暂不能评论哦! 去 登陆 | 注册

博主信息
   
数据加载中,请稍候...
文章分类
   
数据加载中,请稍候...
阅读排行
 
数据加载中,请稍候...
评论排行
 
数据加载中,请稍候...

Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1

鄂公网安备 42011102000739号