最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot用JdbcTemplates訪問(wèn)Mysql實(shí)例代碼

 更新時(shí)間:2017年05月04日 10:51:19   作者:方志朋  
本篇文章主要介紹了SpringBoot用JdbcTemplates訪問(wèn)Mysql實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文介紹springboot通過(guò)jdbc訪問(wèn)關(guān)系型MySQL,通過(guò)spring的JdbcTemplate去訪問(wèn)。

準(zhǔn)備工作

  1. jdk 1.8
  2. maven 3.0
  3. idea
  4. mysql

初始化mysql:

-- create table `account`
DROP TABLE `account` IF EXISTS
CREATE TABLE `account` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(20) NOT NULL,
 `money` double DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `account` VALUES ('1', 'aaa', '1000');
INSERT INTO `account` VALUES ('2', 'bbb', '1000');
INSERT INTO `account` VALUES ('3', 'ccc', '1000');

創(chuàng)建工程

引入依賴:

在pom文件引入spring-boot-starter-jdbc的依賴:

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

引入mysql連接類和連接池:

<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.29</version>
    </dependency>

開(kāi)啟web:

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

配置相關(guān)文件

在application.properties文件配置mysql的驅(qū)動(dòng)類,數(shù)據(jù)庫(kù)地址,數(shù)據(jù)庫(kù)賬號(hào)、密碼信息。

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

通過(guò)引入這些依賴和配置一些基本信息,springboot就可以訪問(wèn)數(shù)據(jù)庫(kù)類。

具體編碼

實(shí)體類

public class Account {
  private int id ;
  private String name ;
  private double money;

....省略了getter. setter

}

dao層

public interface IAccountDAO {
  int add(Account account);

  int update(Account account);

  int delete(int id);

  Account findAccountById(int id);

  List<Account> findAccountList();
}

具體的實(shí)現(xiàn)類:

package com.forezp.dao.impl;

import com.forezp.dao.IAccountDAO;
import com.forezp.entity.Account;
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 java.util.List;

/**
 * Created by fangzhipeng on 2017/4/20.
 */
@Repository
public class AccountDaoImpl implements IAccountDAO {

  @Autowired
  private JdbcTemplate jdbcTemplate;
  @Override
  public int add(Account account) {
    return jdbcTemplate.update("insert into account(name, money) values(?, ?)",
       account.getName(),account.getMoney());

  }

  @Override
  public int update(Account account) {
    return jdbcTemplate.update("UPDATE account SET NAME=? ,money=? WHERE id=?",
        account.getName(),account.getMoney(),account.getId());
  }

  @Override
  public int delete(int id) {
    return jdbcTemplate.update("DELETE from TABLE account where id=?",id);
  }

  @Override
  public Account findAccountById(int id) {
    List<Account> list = jdbcTemplate.query("select * from account where id = ?", new Object[]{id}, new BeanPropertyRowMapper(Account.class));
    if(list!=null && list.size()>0){
      Account account = list.get(0);
      return account;
    }else{
      return null;
    }
  }

  @Override
  public List<Account> findAccountList() {
    List<Account> list = jdbcTemplate.query("select * from account", new Object[]{}, new BeanPropertyRowMapper(Account.class));
    if(list!=null && list.size()>0){
      return list;
    }else{
      return null;
    }
  }
}

service層

public interface IAccountService {


  int add(Account account);

  int update(Account account);

  int delete(int id);

  Account findAccountById(int id);

  List<Account> findAccountList();

}

具體實(shí)現(xiàn)類:

@Service
public class AccountService implements IAccountService {
  @Autowired
  IAccountDAO accountDAO;
  @Override
  public int add(Account account) {
    return accountDAO.add(account);
  }

  @Override
  public int update(Account account) {
    return accountDAO.update(account);
  }

  @Override
  public int delete(int id) {
    return accountDAO.delete(id);
  }

  @Override
  public Account findAccountById(int id) {
    return accountDAO.findAccountById(id);
  }

  @Override
  public List<Account> findAccountList() {
    return accountDAO.findAccountList();
  }
}

構(gòu)建一組restful api來(lái)展示

package com.forezp.web;

import com.forezp.entity.Account;
import com.forezp.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by fangzhipeng on 2017/4/20.
 */

@RestController
@RequestMapping("/account")
public class AccountController {

  @Autowired
  IAccountService accountService;

  @RequestMapping(value = "/list",method = RequestMethod.GET)
  public List<Account> getAccounts(){
    return accountService.findAccountList();
  }

  @RequestMapping(value = "/{id}",method = RequestMethod.GET)
  public Account getAccountById(@PathVariable("id") int id){
    return accountService.findAccountById(id);
  }

  @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
  public String updateAccount(@PathVariable("id")int id , @RequestParam(value = "name",required = true)String name,
  @RequestParam(value = "money" ,required = true)double money){
    Account account=new Account();
    account.setMoney(money);
    account.setName(name);
    account.setId(id);
    int t=accountService.update(account);
    if(t==1){
      return account.toString();
    }else {
      return "fail";
    }
  }

  @RequestMapping(value = "",method = RequestMethod.POST)
  public String postAccount( @RequestParam(value = "name")String name,
                 @RequestParam(value = "money" )double money){
    Account account=new Account();
    account.setMoney(money);
    account.setName(name);
    int t= accountService.add(account);
    if(t==1){
      return account.toString();
    }else {
      return "fail";
    }

  }

}

可以通過(guò)postman來(lái)測(cè)試,具體的我已經(jīng)全部測(cè)試通過(guò),沒(méi)有任何問(wèn)題。注意restful構(gòu)建api的風(fēng)格。

源碼下載:https://github.com/forezp/SpringBootLearning

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談Java包裝類型Long的==操作引發(fā)的低級(jí)bug

    淺談Java包裝類型Long的==操作引發(fā)的低級(jí)bug

    本文主要介紹了淺談Java包裝類型Long的==操作引發(fā)的低級(jí)bug,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Spring Cloud Gateway 緩存區(qū)異常問(wèn)題及解決方案

    Spring Cloud Gateway 緩存區(qū)異常問(wèn)題及解決方案

    最近在測(cè)試環(huán)境spring cloud gateway突然出現(xiàn)了異常,接下來(lái)通過(guò)本文給大家介紹Spring Cloud Gateway 緩存區(qū)異常問(wèn)題解決方案,需要的朋友可以參考下
    2024-06-06
  • SpringBoot?整合?Grizzly的過(guò)程

    SpringBoot?整合?Grizzly的過(guò)程

    Grizzly?是一個(gè)高性能的、異步的、非阻塞的?HTTP?服務(wù)器框架,它可以與?Spring?Boot?一起提供比傳統(tǒng)的?Tomcat?或?Jetty?更高的吞吐量和更低的延遲,這篇文章主要介紹了SpringBoot?整合?Grizzly的過(guò)程,需要的朋友可以參考下
    2025-01-01
  • 往DAO類中注入@PersistenceContext和@Resource的區(qū)別詳解

    往DAO類中注入@PersistenceContext和@Resource的區(qū)別詳解

    這篇文章主要介紹了往DAO類中注入@PersistenceContext和@Resource的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Spring注入Bean的一些方式總結(jié)

    Spring注入Bean的一些方式總結(jié)

    這篇文章主要給大家總結(jié)介紹了關(guān)于Spring注入Bean的一些方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Spring?Data?JPA實(shí)現(xiàn)數(shù)據(jù)持久化過(guò)程詳解

    Spring?Data?JPA實(shí)現(xiàn)數(shù)據(jù)持久化過(guò)程詳解

    Spring?Data?JPA是一個(gè)流行的Java持久化框架,它在Java應(yīng)用程序中提供了一種簡(jiǎn)單、一致和易于使用的方式來(lái)訪問(wèn)各種數(shù)據(jù)庫(kù)。本文將介紹Spring?Data?JPA的基本概念和用法并提供一個(gè)完整的實(shí)例,幫助您更好地理解它的使用方法和優(yōu)勢(shì)
    2023-05-05
  • Java和C的隨機(jī)數(shù)(Random)詳解

    Java和C的隨機(jī)數(shù)(Random)詳解

    本篇文章主要介紹了Java和C隨機(jī)數(shù)(Random),現(xiàn)在分享給大家,也給大家做個(gè)參考,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-09-09
  • Java實(shí)現(xiàn)多數(shù)據(jù)源的幾種方式總結(jié)

    Java實(shí)現(xiàn)多數(shù)據(jù)源的幾種方式總結(jié)

    這篇文章主要給大家總結(jié)介紹了關(guān)于Java實(shí)現(xiàn)多數(shù)據(jù)源的幾種方式,最近項(xiàng)目中的工作流需要查詢多個(gè)數(shù)據(jù)源的數(shù)據(jù),數(shù)據(jù)源可能是不同種類的,需要的朋友可以參考下
    2023-08-08
  • Mybatis中mapper.xml實(shí)現(xiàn)熱加載介紹

    Mybatis中mapper.xml實(shí)現(xiàn)熱加載介紹

    大家好,本篇文章主要講的是Mybatis中mapper.xml實(shí)現(xiàn)熱加載介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • Java GC 機(jī)制與內(nèi)存分配策略詳解

    Java GC 機(jī)制與內(nèi)存分配策略詳解

    這篇文章主要介紹了Java GC 機(jī)制與內(nèi)存分配策略詳解的相關(guān)資料,需要的朋友可以參考下
    2017-02-02

最新評(píng)論

铜川市| 夏邑县| 宜阳县| 信丰县| 南皮县| 股票| 黑龙江省| 临清市| 尤溪县| 南宫市| 临沭县| 久治县| 怀化市| 肇东市| 凭祥市| 梨树县| 江门市| 湾仔区| 柏乡县| 阿荣旗| 竹溪县| 安福县| 周口市| 洪江市| 迁安市| 洪洞县| 瑞丽市| 平凉市| 保山市| 鄯善县| 曲水县| 平度市| 沭阳县| 灌云县| 若羌县| 徐水县| 千阳县| 荥经县| 乌兰察布市| 靖宇县| 喜德县|