摘要:中用訪問一準(zhǔn)備工作運(yùn)行二啟動(dòng)容器并進(jìn)行表的創(chuàng)建啟動(dòng)查看鏡像啟動(dòng)查看全部的鏡像對(duì)應(yīng)的啟動(dòng)連接創(chuàng)建數(shù)據(jù)庫創(chuàng)建表三使用創(chuàng)建項(xiàng)目選擇四配置相關(guān)的文件配置配置實(shí)體類類名為層接口實(shí)現(xiàn)類層接口實(shí)現(xiàn)類層控制層五使用進(jìn)行測(cè)試沒有安裝的話可以
SpringBoot 中用 JdbcTemplate 訪問MySQL
一. 準(zhǔn)備工作
IDEA
docker : 運(yùn)行MySql
二. 啟動(dòng) docker mysql 容器并進(jìn)行表的創(chuàng)建
啟動(dòng) docker,查看docker mysql 鏡像,啟動(dòng)
- docker images : 查看全部的docker 鏡像 - docker run -d -p 3333:3306 -e MYSQL_ROOT_PASSWORD=123 mysql:letest(mysql對(duì)應(yīng)的images : id) - 啟動(dòng) Navicat 連接mysql - 創(chuàng)建數(shù)據(jù)庫 springboot,創(chuàng)建表 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 創(chuàng)建 springBoot項(xiàng)目,選擇 web,MySQL,jpa
四. 配置相關(guān)的文件
配置 application.yml
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3333/springboot username: root password: 123
配置實(shí)體類 類名為 Account
DAO層
接口 :
public interface IAccountDao { int add(Account account); int update(Account account); int delete(int id); Account findAccountById(int id); ListfindAccountList(); }
實(shí)現(xiàn)類 :
@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 = ?"; Listquery = 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); ListfindAccountList(); }
實(shí)現(xiàn)類 :
@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 ListfindAccountList() { 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 ListfindAll(){ System.out.println("query"); return accountService.findAccountList(); } }
五. 使用postman進(jìn)行測(cè)試
沒有安裝的話可以使用 brew cask install postman; 安裝完成后直接打開就可以了 全部測(cè)試通過;
六. 參考
[SpringBoot非官方教程 | 第三篇:SpringBoot用JdbcTemplates訪問Mysql][1]
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/27491.html
閱讀 1884·2021-11-17 09:33
閱讀 6470·2021-10-12 10:20
閱讀 2299·2021-09-22 15:50
閱讀 1783·2021-09-22 15:10
閱讀 615·2021-09-10 10:51
閱讀 618·2021-09-10 10:50
閱讀 3021·2021-08-11 11:19
閱讀 1776·2019-08-30 15:55