欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

準(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>

開啟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

通過引入這些依賴和配置一些基本信息,springboot就可以訪問數(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來展示

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

  }

}

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

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

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

相關(guān)文章

  • Java:com.netflix.client.ClientException錯(cuò)誤解決

    Java:com.netflix.client.ClientException錯(cuò)誤解決

    本文主要介紹了Java:com.netflix.client.ClientException錯(cuò)誤解決,主要是指出客戶端?module-sso?試圖通過負(fù)載均衡器訪問服務(wù)時(shí),負(fù)載均衡器沒有找到可用的服務(wù)器來處理請(qǐng)求,下面就來介紹一下解決方法
    2024-08-08
  • Java設(shè)計(jì)模式之迭代器模式解析

    Java設(shè)計(jì)模式之迭代器模式解析

    這篇文章主要介紹了Java設(shè)計(jì)模式之迭代器模式解析,迭代器模式提供一個(gè)對(duì)象來順序訪問聚合對(duì)象中的一系列數(shù)據(jù),而不暴露聚合對(duì)象的內(nèi)部表示,本文提供了部分代碼,需要的朋友可以參考下
    2023-09-09
  • Java反射機(jī)制(Reflection)淺析

    Java反射機(jī)制(Reflection)淺析

    這篇文章主要介紹了Java反射機(jī)制(Reflection)淺析,本文以實(shí)例講解Java的反射機(jī)制,需要的朋友可以參考下
    2014-07-07
  • SpringBoot集成Spring Data JPA及讀寫分離

    SpringBoot集成Spring Data JPA及讀寫分離

    這篇文章主要介紹了SpringBoot集成Spring Data JPA及讀寫分離的相關(guān)知識(shí),需要的朋友可以參考下
    2017-04-04
  • SpringBoot 實(shí)現(xiàn)定時(shí)任務(wù)的方法詳解

    SpringBoot 實(shí)現(xiàn)定時(shí)任務(wù)的方法詳解

    這篇文章主要介紹了SpringBoot 實(shí)現(xiàn)定時(shí)任務(wù)的方法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • spring boot實(shí)現(xiàn)軟刪除的示例代碼

    spring boot實(shí)現(xiàn)軟刪除的示例代碼

    這篇文章主要介紹了spring boot實(shí)現(xiàn)軟刪除的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • Java游戲服務(wù)器之?dāng)?shù)據(jù)庫(kù)表存取封裝

    Java游戲服務(wù)器之?dāng)?shù)據(jù)庫(kù)表存取封裝

    這篇文章主要介紹了Java游戲服務(wù)器之?dāng)?shù)據(jù)庫(kù)表存取封裝的相關(guān)資料,需要的朋友可以參考下
    2015-11-11
  • java中TCP實(shí)現(xiàn)回顯服務(wù)器及客戶端

    java中TCP實(shí)現(xiàn)回顯服務(wù)器及客戶端

    本文主要介紹了java中TCP實(shí)現(xiàn)回顯服務(wù)器及客戶端,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • springmvc+mybatis 做分頁(yè)sql 語(yǔ)句實(shí)例代碼

    springmvc+mybatis 做分頁(yè)sql 語(yǔ)句實(shí)例代碼

    本文通過一段實(shí)例代碼給大家介紹了springmvc+mybatis 做分頁(yè)sql 語(yǔ)句的方法,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-07-07
  • 詳談Servlet和Filter的區(qū)別以及兩者在Struts2和Springmvc中的應(yīng)用

    詳談Servlet和Filter的區(qū)別以及兩者在Struts2和Springmvc中的應(yīng)用

    下面小編就為大家?guī)硪黄斦凷ervlet和Filter的區(qū)別以及兩者在Struts2和Springmvc中的應(yīng)用。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08

最新評(píng)論