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

資訊專欄INFORMATION COLUMN

SpringBoot-JdbcTemplates-MySQL

CoffeX / 1437人閱讀

摘要:中用訪問一準備工作運行二啟動容器并進行表的創建啟動查看鏡像啟動查看全部的鏡像對應的啟動連接創建數據庫創建表三使用創建項目選擇四配置相關的文件配置配置實體類類名為層接口實現類層接口實現類層控制層五使用進行測試沒有安裝的話可以

SpringBoot 中用 JdbcTemplate 訪問MySQL

一. 準備工作

IDEA

docker : 運行MySql

二. 啟動 docker mysql 容器并進行表的創建

啟動 docker,查看docker mysql 鏡像,啟動

- docker images : 查看全部的docker 鏡像
- docker run -d -p 3333:3306 -e MYSQL_ROOT_PASSWORD=123 mysql:letest(mysql對應的images : id)
- 啟動 Navicat 連接mysql
- 創建數據庫 springboot,創建表
    create table account(
        id int(11) not null auto_increment,
        name varchar(20) not null,
        money double default null,
        premary 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"); 

三. 使用IDEA 創建 springBoot項目,選擇 web,MySQL,jpa
四. 配置相關的文件

配置 application.yml

   spring:
     datasource:
       driver-class-name: com.mysql.jdbc.Driver
       url: jdbc:mysql://localhost:3333/springboot
       username: root
       password: 123

配置實體類 類名為 Account

DAO層
接口 :

   public interface IAccountDao {
       int add(Account account);

       int update(Account account);

       int delete(int id);

       Account findAccountById(int id);

       List findAccountList();
   }

實現類 :

   @Repository
   public class AccountDao implements IAccountDao {
       @Autowired
       private JdbcTemplate jdbcTemplate;
       @Override
       public int add(Account account) {
           int add = jdbcTemplate.update("INSERT INTO account(NAME,money) values(?,?)", new Object[]{account.getName(), account.getMoney()});
           return add;
       }
   
       @Override
       public int update(Account account) {
           String sql = " update account set name = ?,money = ? where id = ?";
           int update = jdbcTemplate.update(sql, new Object[]{account.getName(),account.getMoney(),account.getId()});
           return update;
       }
   
       @Override
       public int delete(int id) {
           String sql = "delete from account where id = ?";
           return jdbcTemplate.update(sql,new Object[]{id});
       }
   
       @Override
       public Account findAccountById(int id) {
           String sql = "select id,name,money from account where id = ?";
           List query = jdbcTemplate.query(sql, new Object[]{id}, new BeanPropertyRowMapper(Account.class));
           if (query != null){
               return query.get(0);
           }
           return null;
       }
   
       @Override
       public List findAccountList() {
           String sql = "select id,name,money from account";
           List accounts = jdbcTemplate.query(sql,new Object[]{}, new BeanPropertyRowMapper(Account.class));
           return accounts;
       }
   
       public List find() {
           String sql = "select id,name,money from account";
           List accounts = jdbcTemplate.query(sql, new Object[]{}, new RowMapper() {
               List lists = new ArrayList();
               @Override
               public Account mapRow(ResultSet resultSet, int i) throws SQLException {
                   Account account = new Account();
                   account.setId(resultSet.getInt(1));
                   account.setName(resultSet.getString(2));
                   account.setMoney(resultSet.getDouble(3));
                   return account;
               }
           });
           return accounts;
       }
   }

service層
接口 :

   public interface IAccountService {
       int add(Account account);
   
       int update(Account account);
   
       int delete(int id);
   
       Account findAccountById(int id);
   
       List findAccountList();
   }

實現類 :

   @Service
   public class AccountService implements IAccountService {
       @Autowired
       private 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.find();
       } 

controller層
控制層

       @RestController
       @RequestMapping("account")
       public class AccountController {
       
           @Autowired
           private IAccountService accountService;
       
           @RequestMapping(value = "add",method = RequestMethod.POST)
           public int insert(@RequestParam(value = "name")String name,@RequestParam(value = "money") Double money){
               Account account = new Account();
               account.setName(name);
               account.setMoney(money);
               System.out.println("add");
               int add = accountService.add(account);
               return add;
           }
           @RequestMapping(value = "delete",method = RequestMethod.GET)
           public int delete(@RequestParam("id")  int id){
               System.out.println("delete");
               return accountService.delete(id);
           }
           @RequestMapping("update")
           public int modify(@RequestParam("id") int id,@RequestParam("name") String name,@RequestParam("money") Double money){
               System.out.println("update");
               Account account = new Account();
               account.setId(id);
               account.setName(name);
               account.setMoney(money);
               return accountService.update(account);
           }
           @RequestMapping(value = "/{id}",method = RequestMethod.GET)
           public Account findOne(@PathVariable("id") int id){
               System.out.println("queryById");
               return accountService.findAccountById(id);
           }
           @RequestMapping(value = "findAll",method = RequestMethod.GET)
           public List findAll(){
               System.out.println("query");
               return accountService.findAccountList();
           }
       }

五. 使用postman進行測試

沒有安裝的話可以使用 brew cask install postman; 安裝完成后直接打開就可以了

全部測試通過;

六. 參考

[SpringBoot非官方教程 | 第三篇:SpringBoot用JdbcTemplates訪問Mysql][1]




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

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

相關文章

發表評論

0條評論

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