摘要:中用訪問一準備工作運行二啟動容器并進行表的創建啟動查看鏡像啟動查看全部的鏡像對應的啟動連接創建數據庫創建表三使用創建項目選擇四配置相關的文件配置配置實體類類名為層接口實現類層接口實現類層控制層五使用進行測試沒有安裝的話可以
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); ListfindAccountList(); }
實現類 :
@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(); }
實現類 :
@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進行測試
沒有安裝的話可以使用 brew cask install postman; 安裝完成后直接打開就可以了 全部測試通過;
六. 參考
[SpringBoot非官方教程 | 第三篇:SpringBoot用JdbcTemplates訪問Mysql][1]
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/27491.html
閱讀 1891·2021-11-17 09:33
閱讀 6484·2021-10-12 10:20
閱讀 2306·2021-09-22 15:50
閱讀 1793·2021-09-22 15:10
閱讀 626·2021-09-10 10:51
閱讀 630·2021-09-10 10:50
閱讀 3049·2021-08-11 11:19
閱讀 1786·2019-08-30 15:55