成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

SpringBoot非官方教程 | 第三篇:SpringBoot用JdbcTemplates訪問(wèn)My

tigerZH / 504人閱讀

摘要:本文介紹通過(guò)訪問(wèn)關(guān)系型通過(guò)的去訪問(wèn)。通過(guò)引入這些依賴和配置一些基本信息,就可以訪問(wèn)數(shù)據(jù)庫(kù)類。具體編碼實(shí)體類省略了層具體的實(shí)現(xiàn)類層具體實(shí)現(xiàn)類構(gòu)建一組來(lái)展示可以通過(guò)來(lái)測(cè)試,具體的我已經(jīng)全部測(cè)試通過(guò),沒(méi)有任何問(wèn)題。注意構(gòu)建的風(fēng)格。

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

準(zhǔn)備工作
jdk 1.8
maven 3.0
idea
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的依賴:

 
    org.springframework.boot
    spring-boot-starter-jdbc

引入mysql連接類和連接池:


    mysql
    mysql-connector-java
    runtime



    com.alibaba
    druid
    1.0.29

開(kāi)啟web:

    
        org.springframework.boot
        spring-boot-starter-web
    
配置相關(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 findAccountList();
}
dao具體的實(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 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 findAccountList() {
        List 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 findAccountList();

}
service具體實(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 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 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/Spr...

參考資料

relational-data-access

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/70356.html

相關(guān)文章

  • SpringBoot-JdbcTemplates-MySQL

    摘要:中用訪問(wèn)一準(zhǔn)備工作運(yùn)行二啟動(dòng)容器并進(jìn)行表的創(chuàng)建啟動(dòng)查看鏡像啟動(dòng)查看全部的鏡像對(duì)應(yīng)的啟動(dòng)連接創(chuàng)建數(shù)據(jù)庫(kù)創(chuàng)建表三使用創(chuàng)建項(xiàng)目選擇四配置相關(guān)的文件配置配置實(shí)體類類名為層接口實(shí)現(xiàn)類層接口實(shí)現(xiàn)類層控制層五使用進(jìn)行測(cè)試沒(méi)有安裝的話可以 SpringBoot 中用 JdbcTemplate 訪問(wèn)MySQL 一. 準(zhǔn)備工作 IDEA docker : 運(yùn)行MySql 二. 啟動(dòng) docker my...

    CoffeX 評(píng)論0 收藏0
  • SpringBoot官方教程 | 第十三篇springboot集成spring cache

    摘要:本文介紹如何在中使用默認(rèn)的聲明式緩存定義和接口用來(lái)統(tǒng)一不同的緩存技術(shù)。在使用集成的時(shí)候,我們需要注冊(cè)實(shí)現(xiàn)的的。默認(rèn)使用在我們不使用其他第三方緩存依賴的時(shí)候,自動(dòng)采用作為緩存管理器。源碼下載參考資料揭秘與實(shí)戰(zhàn)二數(shù)據(jù)緩存篇快速入門 本文介紹如何在springboot中使用默認(rèn)的spring cache 聲明式緩存 Spring 定義 CacheManager 和 Cache 接口用來(lái)統(tǒng)一不...

    Magicer 評(píng)論0 收藏0
  • SpringBoot 實(shí)戰(zhàn) (六) | JdbcTemplates 訪問(wèn) Mysql

    摘要:微信公眾號(hào)一個(gè)優(yōu)秀的廢人如有問(wèn)題或建議,請(qǐng)后臺(tái)留言,我會(huì)盡力解決你的問(wèn)題。前言如題,今天介紹通過(guò)訪問(wèn)關(guān)系型通過(guò)的去訪問(wèn)。我這里已經(jīng)全部測(cè)試通過(guò),請(qǐng)放心使用。源碼下載后語(yǔ)以上用訪問(wèn)的教程。另外,關(guān)注之后在發(fā)送可領(lǐng)取免費(fèi)學(xué)習(xí)資料。 微信公眾號(hào):一個(gè)優(yōu)秀的廢人如有問(wèn)題或建議,請(qǐng)后臺(tái)留言,我會(huì)盡力解決你的問(wèn)題。 前言 如題,今天介紹 springboot 通過(guò)jdbc訪問(wèn)關(guān)系型mysql,通過(guò)...

    wh469012917 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<