码迷,mamicode.com
首页 > 数据库 > 详细

springboot之JdbcTemplate

时间:2018-10-09 21:37:35      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:mysq   删除   ice   ott   interface   drive   unit   arc   test   

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 +
                ‘}‘;
    }
}
View Code

 

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));

    }

}

 

springboot之JdbcTemplate

标签:mysq   删除   ice   ott   interface   drive   unit   arc   test   

原文地址:https://www.cnblogs.com/haizhilangzi/p/9762668.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!