springboot之JdbcTemplate

springboot可以使用JdbcTemplate进行数据库访问,代码如下

添加pom文件

   <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

代码如下

CREATE TABLE  if NOT EXISTS springboot.student
(
stu_no INT ,
name VARCHAR(20),
age INT
)
package com.rookie.bigdata.domain;

/**
* @author
* @date 2018/10/9
*/
public class Student {
private Long stuNo;
private String name;
private Integer age; public Long getStuNo() {
return stuNo;
} public void setStuNo(Long stuNo) {
this.stuNo = stuNo;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; if (stuNo != null ? !stuNo.equals(student.stuNo) : student.stuNo != null) return false;
if (name != null ? !name.equals(student.name) : student.name != null) return false;
return age != null ? age.equals(student.age) : student.age == null;
} @Override
public int hashCode() {
int result = stuNo != null ? stuNo.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (age != null ? age.hashCode() : 0);
return result;
} @Override
public String toString() {
return "Student{" +
"stuNo=" + stuNo +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.rookie.bigdata.service;

import com.rookie.bigdata.domain.Student;

/**
* @author
* @date 2018/10/9
*/
public interface StudentService { /**
* 添加一个学生
*
* @param student
*/
void create(Student student); /**
* 删除一个学生
*
* @param stuNo
*/
void deleteByNo(Integer stuNo); /**
* 查询学生
*
* @param stuNo
*/
Integer queryByStuNo(Integer stuNo);
}
package com.rookie.bigdata.service;

import com.rookie.bigdata.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service; /**
* @author
* @date 2018/10/9
*/
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private JdbcTemplate jdbcTemplate; @Override
public void create(Student student) { jdbcTemplate.update("INSERT INTO student(stu_no,name,age)VALUE (?,?,?)", student.getStuNo(), student.getName(), student.getAge()); } @Override
public void deleteByNo(Integer stuNo) {
jdbcTemplate.update("DELETE FROM student WHERE stu_no=?", stuNo);
} @Override
public Integer queryByStuNo(Integer stuNo) {
return jdbcTemplate.queryForObject("select count(1) from student", Integer.class);
}
}

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

测试代码如下:

package com.rookie.bigdata.service;

import com.rookie.bigdata.domain.Student;
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 static org.junit.Assert.*; /**
* @author
* @date 2018/10/9
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentServiceImplTest { @Autowired
private StudentServiceImpl studentService; @Test
public void create1() throws Exception {
Student student = new Student();
student.setStuNo(1L);
student.setName("张三");
student.setAge(23); studentService.create(student); } @Test
public void deleteByName1() throws Exception {
studentService.deleteByNo(1);
} @Test
public void queryByStuNo1() throws Exception {
System.out.println(studentService.queryByStuNo(1)); } }
上一篇:Spring 3 MVC: Themes In Spring-Tutorial With Example---reference


下一篇:深度学习FPGA实现基础知识10(Deep Learning(深度学习)卷积神经网络(Convolutional Neural Network,CNN))