国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

SpringCloud(第 041 篇)鏈接Mysql數據庫,通過JdbcTemplate編寫數據庫

Eirunye / 3165人閱讀

摘要:通過注解引入對象。添加一個使用接收請求添加賬號訪問層添加微服務啟動類鏈接數據庫通過編寫數據庫訪問。

SpringCloud(第 041 篇)鏈接Mysql數據庫,通過JdbcTemplate編寫數據庫訪問

-

一、大致介紹
1、前面章節提到了JPA操作訪問數據庫,本章節我們講解一下如何用JdbcTemplate操作訪問數據庫;
2、使用JdbcTemplate是一個相對更底層的操作,可以直接編寫sql語句操作數據,更具靈活性;
3、另外配置文件中的mysql數據庫配置,那么就得大家自己用自己的了哈;
二、實現步驟 2.1 添加 maven 引用包


    4.0.0

    springms-provider-user-mysql-jdbctemplate
    1.0-SNAPSHOT
    jar
    
    
        com.springms.cloud
        springms-spring-cloud
        1.0-SNAPSHOT
    
    
    
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            mysql
            mysql-connector-java
        
    

2.2 添加應用配置文件(springms-provider-user-mysql-jdbctemplatesrcmainresourcesapplication.yml)
server:
  port: 8315
spring:
  application:
    name: springms-provider-user-mysql-jdbctemplate  #全部小寫


#####################################################################################################
# mysql 屬性配置
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://ip:3306/hmilyylimh
    username: username
    password: password
  jpa:
    hibernate:
      #ddl-auto: create #ddl-auto:設為create表示每次都重新建表
      ddl-auto: update #ddl-auto:設為update表示每次都不會重新建表
    show-sql: true
#####################################################################################################







#####################################################################################################
# 打印日志
logging:
  level:
    root: INFO
    org.hibernate: INFO
    org.hibernate.type.descriptor.sql.BasicBinder: TRACE
    org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
    com.springms: DEBUG
#####################################################################################################
2.3 添加實體用戶類User(springms-provider-user-mysql-jdbctemplate/src/main/java/com/springms/cloud/entity/User.java)
package com.springms.cloud.entity;

public class User {

  private Long id;

  private String username;

  private String name;

  private Integer age;

  private String balance;

  public Long getId() {
    return this.id;
  }

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

  public String getUsername() {
    return this.username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Integer getAge() {
    return this.age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }

  public String getBalance() {
    return this.balance;
  }

  public void setBalance(String balance) {
    this.balance = balance;
  }

  @Override
  public String toString() {
    return "User{" +
            "id=" + id +
            ", username="" + username + """ +
            ", name="" + name + """ +
            ", age=" + age +
            ", balance="" + balance + """ +
            "}";
  }
}
2.4 添加實體賬戶類Account(springms-provider-user-mysql-jdbctemplate/src/main/java/com/springms/cloud/entity/Account.java)
package com.springms.cloud.entity;

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

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name="" + name + """ +
                ", money=" + money +
                "}";
    }
}

2.5 添加用戶DAO接口(springms-provider-user-mysql-jdbctemplate/src/main/java/com/springms/cloud/dao/IUserDao.java)
package com.springms.cloud.dao;

import com.springms.cloud.entity.User;

import java.util.List;

/**
 * 簡單用戶鏈接Mysql數據庫微服務(通過@Repository注解標注該類為持久化操作對象)。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 17/10/18
 *
 */
public interface IUserDao {

    User findUserById(Long id);

    List findAllUsers();

    int insertUser(User user);
}
2.6 添加賬號DAO接口(springms-provider-user-mysql-jdbctemplate/src/main/java/com/springms/cloud/dao/IAccountDao.java)
package com.springms.cloud.dao;

import com.springms.cloud.entity.Account;
import java.util.List;


public interface IAccountDao {

    int add(Account account);

    int update(Account account);

    int delete(int id);

    Account findAccountById(int id);

    List findAccountList();
}
2.7 添加用戶DAO接口實現類(springms-provider-user-mysql-jdbctemplate/src/main/java/com/springms/cloud/dao/impl/UserDaoImpl.java)
package com.springms.cloud.dao.impl;

import com.springms.cloud.dao.IUserDao;
import com.springms.cloud.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 簡單用戶鏈接Mysql數據庫微服務(通過@Repository注解標注該類為持久化操作對象)。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 17/10/18
 *
 */
@Repository
public class UserDaoImpl implements IUserDao {

    /**
     * 通過@Resource注解引入JdbcTemplate對象。
     */
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //@Transactional(readOnly = true)
    @Override
    public User findUserById(Long id){
        // 1. 定義一個sql語句
        String querySQL = "select * from user where id = ?";

        // 2. 定義一個RowMapper
        RowMapper rowMapper = new BeanPropertyRowMapper(User.class);

        // 3. 執行查詢方法

        //List list = jdbcTemplate.query("select * from account where id = ?", new Object[]{id}, new BeanPropertyRowMapper(User.class));

        User user = jdbcTemplate.queryForObject(querySQL, new Object[]{id}, rowMapper);

        return user;
    }

    //@Transactional(readOnly = true)
    @Override
    public List findAllUsers(){
        // 1. 定義一個sql語句
        String querySQL = "select * from user";

        // 2. 定義一個RowMapper
        RowMapper rowMapper = new BeanPropertyRowMapper(User.class);

        // 3. 執行查詢方法
        List users = jdbcTemplate.query(querySQL, new Object[]{}, rowMapper);

        return users;
    }

    @Override
    public int insertUser(User user) {
        // 1. 定義一個sql語句
        String execSQL = "INSERT into user (username, name, age, balance) values (?, ?, ?, ?)";

        // 2. 執行查詢方法
        return jdbcTemplate.update(execSQL,
                new Object[]{user.getUsername(), user.getName(), user.getAge(), user.getBalance()});
    }
}
2.8 添加賬號DAO接口實現類(springms-provider-user-mysql-jdbctemplate/src/main/java/com/springms/cloud/dao/impl/AccountDaoImpl.java)
package com.springms.cloud.dao.impl;

import com.springms.cloud.dao.IAccountDao;
import com.springms.cloud.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;


@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;
        }
    }
}
2.9 添加用戶Service接口(springms-provider-user-mysql-jdbctemplate/src/main/java/com/springms/cloud/service/IUserService.java)
package com.springms.cloud.service;

import com.springms.cloud.entity.User;

import java.util.List;

/**
 * 簡單用戶鏈接Mysql數據庫微服務(通過@Service注解標注該類為持久化操作對象)。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 17/10/18
 *
 */
public interface IUserService {

    User findUserById(Long id);

    List findAllUsers();

    int insertUser(User user);
}
2.10 添加賬號Service接口(springms-provider-user-mysql-jdbctemplate/src/main/java/com/springms/cloud/service/IAccountService.java)
package com.springms.cloud.service;

import com.springms.cloud.entity.Account;

import java.util.List;


public interface IAccountService {


    int add(Account account);

    int update(Account account);

    int delete(int id);

    Account findAccountById(int id);

    List findAccountList();

}
2.11 添加用戶Service接口實現類(springms-provider-user-mysql-jdbctemplate/src/main/java/com/springms/cloud/service/impl/UserServiceImpl.java)
package com.springms.cloud.service.impl;

import com.springms.cloud.dao.IUserDao;
import com.springms.cloud.entity.User;
import com.springms.cloud.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 簡單用戶鏈接Mysql數據庫微服務(通過@Service注解標注該類為持久化操作對象)。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 17/10/18
 *
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserDao userDao;

    @Override
    public User findUserById(Long id){
        return userDao.findUserById(id);
    }

    @Override
    public List findAllUsers(){
        return userDao.findAllUsers();
    }

    @Override
    public int insertUser(User user) {
        return userDao.insertUser(user);
    }
}
2.12 添加賬號Service接口實現類(springms-provider-user-mysql-jdbctemplate/src/main/java/com/springms/cloud/service/impl/AccountServiceImpl.java)
package com.springms.cloud.service.impl;

import com.springms.cloud.dao.IAccountDao;
import com.springms.cloud.entity.Account;
import com.springms.cloud.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class AccountServiceImpl 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();
    }
}
2.13 添加用戶Web訪問層Controller(springms-provider-user-mysql-crudrepository/src/main/java/com/springms/cloud/controller/ProviderUserMysqlCrudRepoController.java)
package com.springms.cloud.controller;

import com.springms.cloud.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.springms.cloud.entity.User;
import java.util.List;

/**
 * 用戶微服務Controller。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 17/10/18
 *
 */
@RestController
public class ProviderUserMysqlJdbcController {

    @Autowired
    private IUserService userService;

    @GetMapping("/simplejdbc/{id}")
    public User findUserById(@PathVariable Long id) {
        return this.userService.findUserById(id);
    }

    @GetMapping("/simplejdbc/list")
    public List findAllUsers() {
        return this.userService.findAllUsers();
    }

    /**
     * 添加一個student,使用postMapping接收post請求
     *
     * http://localhost:8315/simple/addUser?username=user11&age=11&balance=11
     *
     * @return
     */
    @PostMapping("/simplejdbc/addUser")
    public User insertUser(@RequestParam(value = "username", required=false) String username, @RequestParam(value = "age", required=false) Integer age, @RequestParam(value = "balance", required=false) String balance){
        User user=new User();

        user.setUsername(username);
        user.setName(username);
        user.setAge(age);
        user.setBalance(balance);

        int result = userService.insertUser(user);
        if(result > 0){
            return user;
        }

        user.setId(0L);
        user.setName(null);
        user.setUsername(null);
        user.setBalance(null);
        return user;
    }
}
2.14 添加賬號Web訪問層Controller(springms-provider-user-mysql-crudrepository/src/main/java/com/springms/cloud/controller/ProviderUserMysqlCrudRepoController.java)
package com.springms.cloud.controller;

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

import java.util.List;


@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";
        }
    }
}
2.15 添加微服務啟動類(springms-provider-user-mysql-crudrepository/src/main/java/com/springms/cloud/MsProviderUserMysqlCrudRepoApplication.java)
package com.springms.cloud;

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

/**
 * 鏈接Mysql數據庫,通過JdbcTemplate編寫數據庫訪問。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 17/10/18
 *
 */
@SpringBootApplication
public class MsProviderUserMysqlJdbcApplication {

    public static void main(String[] args) {
        SpringApplication.run(MsProviderUserMysqlJdbcApplication.class, args);
        System.out.println("【【【【【【 鏈接MysqlJdbc數據庫微服務 】】】】】】已啟動.");
    }
}
三、測試
/****************************************************************************************
 一、簡單用戶鏈接Mysql數據庫微服務(通過JdbcTemplate編寫數據訪問):

 1、啟動 springms-provider-user-mysql-jdbctemplate 模塊服務,啟動1個端口;
 2、在瀏覽器輸入地址 http://localhost:8315/simplejdbc/6 可以看到用戶ID=10的信息成功的被打印出來;

 3、使用 IDEA 自帶工具 Test Restful WebService 發送 HTTP POST 請求,并添加 username、age、balance三個參數,然后執行請求,并去 mysql 數據庫查看數據是否存在,正常情況下 mysql 數據庫剛剛插入的數據成功了:
 4、使用 REST Client 執行 "/simplejdbc/list" 接口,也正常將 mysql 數據庫中所有的用戶信息打印出來了;

 5、然后再操作 http://localhost:8315/account/ 該鏈接的接口操作,也是可以正常操作訪問的;
 ****************************************************************************************/
四、下載地址

https://gitee.com/ylimhhmily/SpringCloudTutorial.git

SpringCloudTutorial交流QQ群: 235322432

SpringCloudTutorial交流微信群: 微信溝通群二維碼圖片鏈接

歡迎關注,您的肯定是對我最大的支持!!!

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/67821.html

相關文章

  • SpringCloud 042 鏈接Mysql據庫,通過JdbcTemplate編寫據庫

    摘要:由于簡單用戶鏈接數據庫微服務通過注解標注該類為持久化操作對象。由于是的子類,所以屬于的子類異常拋出來,都會回滾數據。添加一個使用接收請求添加一個使用接收請求添加微服務啟動類鏈接數據庫通過編寫數據庫訪問而且支持事物處理機制。 SpringCloud(第 042 篇)鏈接Mysql數據庫,通過JdbcTemplate編寫數據庫訪問,而且支持事物處理機制 - 一、大致介紹 1、在數據庫操作中...

    zorpan 評論0 收藏0
  • SpringCloud 039 鏈接Mysql據庫,通過JpaRepository編寫數據

    摘要:添加一個使用接收請求添加用戶微服務啟動類鏈接數據庫通過編寫數據庫訪問。 SpringCloud(第 039 篇)鏈接Mysql數據庫,通過JpaRepository編寫數據庫訪問 - 一、大致介紹 1、本章節開始講解一下訪問數據庫的操作,不過其實這還不算是SpringCloud的范疇,應該說是SpringBoot的操作范疇,這些就先拋開了,我們就還是講解數據庫的操作; 2、JpaRep...

    ethernet 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<