SpringBoot使用JdbcTemplate案例(学习笔记)

声明:本案例学习http://blog.csdn.net/je_ge,在此感谢je_ge提供的学习用的资料

1、项目结构

这里写图片描述

2、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.jege.spring.boot</groupId>
    <artifactId>spring-boot-jdbc</artifactId>
    <version>1.0.0.RELEASE</version>
    <packaging>jar</packaging>

    <name>spring-boot-jdbc</name>
    <url>http://blog.csdn.net/je_ge</url>

    <developers>
        <developer>
            <id>je_ge</id>
            <name>je_ge</name>
            <email>1272434821@qq.com</email>
            <url>http://blog.csdn.net/je_ge</url>
            <timezone>8</timezone>
        </developer>
    </developers>

    <!-- 公共spring-boot配置,下面依赖jar文件不用在写版本号 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!-- 持久层 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!-- h2内存数据库 -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>spring-boot-jdbc</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

3、User的内容

package com.jege.spring.boot.jdbc.entity;

/**
 * @author JE哥
 * @email 1272434821@qq.com
 * @description:模型对象
 */
public class User {
  private Long id;
  private String name;
  private Integer age;

  public User() {

  }

  public User(String name, Integer age) {
    this.name = name;
    this.age = age;
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

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

}

4、IUserDao的内容

package com.jege.spring.boot.jdbc.dao;

import java.util.List;

import com.jege.spring.boot.jdbc.entity.User;

/**
 * @author JE哥
 * @email 1272434821@qq.com
 * @description:持久层接口,由spring自动生成其实现
 */
public interface IUserDao {

  void dropTable();

  void createTable();

  void save(User user);

  List<User> findAll();

  void deleteAll();

  List<User> findByNameLike(String name);

}

5、UserDaoImpl的内容

package com.jege.spring.boot.jdbc.dao.impl;

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.stereotype.Repository;

import com.jege.spring.boot.jdbc.dao.IUserDao;
import com.jege.spring.boot.jdbc.entity.User;

/**
 * @author JE哥
 * @email 1272434821@qq.com
 * @description:jdbc CRUD
 */
@Repository
public class UserDaoImpl implements IUserDao {
  @Autowired
  JdbcTemplate jdbcTemplate;

  @Override
  public void dropTable() {
    jdbcTemplate.update("drop table t_user if exists");
  }

  @Override
  public void createTable() {
    jdbcTemplate.update(
    "create table t_user (" +
            "id bigint generated by default as identity, " +
            "age integer, " +
            "name varchar(255), " +
            "primary key (id))");
  }

  @Override
  public void save(User user) {
    jdbcTemplate.update("insert into t_user(name,age) values(?,?)", user.getName(), user.getAge());
  }

  @Override
  public List<User> findAll() {
    return jdbcTemplate.query("select id,name,age from t_user", BeanPropertyRowMapper.newInstance(User.class));
  }

  @Override
  public void deleteAll() {
    jdbcTemplate.update("delete from t_user");
  }

  @Override
  public List<User> findByNameLike(String name) {
    return jdbcTemplate.query("select id,name,age from t_user where name like ?", new Object[] { name },
    BeanPropertyRowMapper.newInstance(User.class));
  }

}

6、Application的内容

package com.jege.spring.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author JE哥
 * @email 1272434821@qq.com
 * @description:spring boot 启动类
 */

@SpringBootApplication
public class Application {

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }

}

7、UserDaoTest的内容

package com.jege.spring.boot.data.jpa;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.After;
import org.junit.Before;
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.SpringJUnit4ClassRunner;

import com.jege.spring.boot.jdbc.dao.IUserDao;
import com.jege.spring.boot.jdbc.entity.User;

/**
 * @author JE哥
 * @email 1272434821@qq.com
 * @description:
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest()
public class UserDaoTest {
  @Autowired
  IUserDao userDao;

  // 每次执行Test之前先删除表,创建表
  @Before
  public void before() throws Exception {
    userDao.dropTable();
    userDao.createTable();
  }

  @Test
  public void save() throws Exception {
    for (int i = 0; i < 10; i++) {
      User user = new User("jege" + i, 25 + i);
      userDao.save(user);
    }
  }

  @Test
  public void all() throws Exception {
    save();
    assertThat(userDao.findAll()).hasSize(10);
  }

  @Test
  public void findByName() throws Exception {
    save();
    assertThat(userDao.findByNameLike("jege%")).hasSize(10);
  }

  @After
  public void destroy() throws Exception {
    userDao.deleteAll();
  }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涂作权的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值