springBoot整合JdbcTemplate

1.pom文件中加入

<dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-jdbc</artifactId>  
     </dependency>
     <dependency>  
            <groupId>mysql</groupId>  
            <artifactId>mysql-connector-java</artifactId>  
            <scope>runtime</scope>  
     </dependency>  
2.dao层

package com.aruisi.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.aruisi.dto.StudentEntity;
import com.aruisi.dto.StudentRowMapper;

@Repository
public class StudentRepository {
	@Autowired  
    private JdbcTemplate jdbcTemplate;
	 @Transactional(readOnly = true)  
	 public List<StudentEntity> getStudentList(){  
        List<StudentEntity> studentList=jdbcTemplate.query("select id,name,sex,age from student",new StudentRowMapper());  
        System.out.println(studentList);  
        return studentList;  
    }  
}
3.service层

package com.aruisi.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aruisi.dao.StudentRepository;
import com.aruisi.dto.StudentEntity;

@Service
public class StudentService {
	 @Autowired  
	 private StudentRepository studentRepository;
	 public List<StudentEntity> getStudentList(){  
	        return studentRepository.getStudentList();  
	 }  
}
4.controller层

package com.aruisi.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.RestController;

import com.aruisi.dto.StudentEntity;
import com.aruisi.service.StudentService;

@RestController  
@RequestMapping("/student") 
public class StudentController {
	 @Autowired  
	 private StudentService studentService;  
	 
	 @RequestMapping("/list")  
	    public List<StudentEntity> studentList() {  
	        List<StudentEntity> studentList=studentService.getStudentList(); 
	        System.out.println(studentList);
	        return studentList;  
	    }  
}
5.数据库配置文件application.properties

###### 设置tomcat访问端口号 ######
server.port=8085
###### 设置数据源 ######
spring.datasource.url=jdbc:mysql://localhost:3306/mytest?autoReconnect=true&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
代码下载地址
点击打开链接



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值