博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring之JDBC
阅读量:4975 次
发布时间:2019-06-12

本文共 9315 字,大约阅读时间需要 31 分钟。

项目目录

 

基本代码

 

applicationContext.xml

1 
2
6 7
8
9
10
11
12
13
14
15
16
17
18 19
20
21
22
23
24 25
26
27
28
29
30

 

Account.java

1 package com.xichuan.jdbc; 2  3 public class Account { 4     private Integer id; 5     private String username; 6     private Double balance; 7     public Integer getId() { 8         return id; 9     }10     public void setId(Integer id) {11         this.id = id;12     }13     public String getUsername() {14         return username;15     }16     public void setUsername(String username) {17         this.username = username;18     }19     public Double getBalance() {20         return balance;21     }22     public void setBalance(Double balance) {23         this.balance = balance;24     }25     @Override26     public String toString() {27         return "Acoount [id=" + id + ", " 28                 + "username = " + username +29                 ", balance=" + balance + "]";30     }31     32 }

 

AccountDao.java

1 package com.xichuan.jdbc; 2  3 import java.util.List; 4  5 public interface AccountDao { 6     public int addAccount(Account account); 7     public int updateAccount(Account account); 8     public int deleteAccount(int id); 9     public Account findAccountById(int id);10     public List
findAllAccount();11 }

 

AccountDaoImpl.java

1 package com.xichuan.jdbc; 2  3 import java.util.List; 4  5 import org.springframework.jdbc.core.BeanPropertyRowMapper; 6 import org.springframework.jdbc.core.JdbcTemplate; 7 import org.springframework.jdbc.core.RowMapper; 8  9 public class AccountDaoImpl implements AccountDao{10     private JdbcTemplate jdbcTemplate;11     public void setJdbcTemplate(JdbcTemplate jdbcTemplate){12         this.jdbcTemplate = jdbcTemplate;13     }14     //添加账户15     public int addAccount(Account account) {16         // 定义SQL17         String sql = "insert into account(username,balance) value(?,?)";18         //定义数组来储存SQL语句中的参数19         Object[] obj = new Object[]{20             account.getUsername(),21             account.getBalance()22         };23         //执行添加操作,返回的是受SQL语句影响的记录条数24         int num = this.jdbcTemplate.update(sql, obj);25         return num;26     }27 28     //更新账户29     public int updateAccount(Account account) {30         // 定义SQL31         String sql = "update account set username=?, balance=? where id=?";32         //定义数组来储存SQL语句中的参数33         Object[] params = new Object[]{34             account.getUsername(),35             account.getBalance(),36             account.getId()37         };38         //执行更新操作,返回的是受SQL语句影响的记录条数39         int num = this.jdbcTemplate.update(sql, params);40         return num;41     }42     43     //删除账户44     public int deleteAccount(int id) {45         // 定义SQL46         String sql = "delete from account where id =?";47         //执行删除操作,返回的是受SQL语句影响的记录条数48         int num = this.jdbcTemplate.update(sql, id);49         return num;50     }51 52     public Account findAccountById(int id) {53         //定义sql语句54         String sql = "select * from account where id = ?";55         //创建一个新的BeanPropertyRowMapper对象56         RowMapper
rowMapper = new BeanPropertyRowMapper
(Account.class);57 return this.jdbcTemplate.queryForObject(sql, rowMapper, id);58 }59 60 public List
findAllAccount(){61 //定义sql62 String sql = "select * from account";63 //创建一个新的BeanPropertyRowMapper对象64 RowMapper
rowMapper = new BeanPropertyRowMapper
(Account.class);65 return this.jdbcTemplate.query(sql, rowMapper);66 67 }68 }

 

 

测试类

 

Test_CreateTable.java  (添加mysql表)

1 package com.xichuan.test; 2  3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 import org.springframework.jdbc.core.JdbcTemplate; 6  7 public class Test_CreateTable { 8     //添加表 9     public static void main(String[] args) {10         //加载配置文件11         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");12         //获取JdbcTemplate实例13         JdbcTemplate jdTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");14         //使用execute()方法执行SQL语句,创建用户账户管理表account15         jdTemplate.execute("create table account(" +16                 "id int primary key auto_increment," +17                 "username varchar(50)," +18                 "balance double)");19         System.out.println("账户表accout创建成功");20     }21 }

 

 

Test_Add.java  (添加单行数据)

1 package com.xichuan.test; 2  3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5  6 import com.xichuan.jdbc.Account; 7 import com.xichuan.jdbc.AccountDao; 8  9 public class Test_Add {10     public static void main(String[] args) {11         //加载配置文件12         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");13         //获取Account实例14         AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");15         //创建account对象,并向Account对象中添加数据16         Account account = new Account();17         account.setUsername("tom");18         account.setBalance(1000.00);19         int num = accountDao.addAccount(account);20         if (num > 0) {21             System.out.println("成功插入了" + num + "条数据!");22         } else {23             System.out.println("插入执行失败!");24         }25     }26 }

 

Test_Delete.java  (删除数据)

1 package com.xichuan.test; 2  3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5  6 import com.xichuan.jdbc.AccountDao; 7  8 public class Test_Delete { 9     public static void main(String[] args) {10         //加载配置文件11         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");12         //获取Account实例13         AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");14         //创建account对象,并向Account对象中添加数据15         int num = accountDao.deleteAccount(1);16         if (num > 0) {17             System.out.println("成功删除了" + num + "条数据!");18         } else {19             System.out.println("删除操作执行失败!");20         }21     }22 }

 

Test_Update.java  (更新数据)

1 package com.xichuan.test; 2  3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5  6 import com.xichuan.jdbc.Account; 7 import com.xichuan.jdbc.AccountDao; 8  9 public class Test_Update {10     public static void main(String[] args) {11         //加载配置文件12         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");13         //获取Account实例14         AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");15         //创建account对象,并向Account对象中添加数据16         Account account = new Account();17         account.setId(2);18         account.setUsername("tom");19         account.setBalance(2000.00);20         int num = accountDao.updateAccount(account);21         if (num > 0) {22             System.out.println("成功修改了" + num + "条数据!");23         } else {24             System.out.println("修改执行失败!");25         }26     }27 }

 

Test_Find.java  (查询单行数据)

1 package com.xichuan.test; 2  3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5  6 import com.xichuan.jdbc.Account; 7 import com.xichuan.jdbc.AccountDao; 8  9 public class Test_Find {10     public static void main(String[] args) {11         //加载配置文件12         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");13         //获取Account实例14         AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");15         //执行findAccountById()方法16         Account account = accountDao.findAccountById(1);17         System.out.println(account);18     }19 }

 

Test_FindAll.java  (查询所有数据)

1 package com.xichuan.test; 2  3 import java.util.List; 4  5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7  8 import com.xichuan.jdbc.Account; 9 import com.xichuan.jdbc.AccountDao;10 11 public class Test_FindAll {12     public static void main(String[] args) {13         //加载配置文件14         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");15         //获取Account实例16         AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");17         //执行findAllAccount()方法,获取Account对象的集合18         List
account = accountDao.findAllAccount();19 for (Account act : account) {20 //循环输出集合中的对象21 System.out.println(act);22 }23 }24 }

 

转载于:https://www.cnblogs.com/salander/p/10637987.html

你可能感兴趣的文章
关于SQL2008 “不允许保存更改。您所做的更改要求删除并重新创建以下表。您对无法重新创建的标进行了更改或者启用了‘阻止保存要求重新创建表的更改’” 解决方案...
查看>>
php文件操作(上传文件)2
查看>>
linux内核驱动模型
查看>>
给WebApp加一个“壳”,实现Andriod系统添加到桌面
查看>>
js 浏览器复制功能
查看>>
数据库总编
查看>>
redis 字符串(string)函数
查看>>
杭州电 1372 Knight Moves(全站搜索模板称号)
查看>>
POJ--3268--Silver Cow Party【SPFA+邻接表】
查看>>
c语言的几个简单memo
查看>>
C#的默认访问权限
查看>>
selenium下打开Chrome报错解决
查看>>
红酒初识
查看>>
BNUOJ 5629 胜利大逃亡(续)
查看>>
HDU-1150 Machine Schedule(二分图、匈牙利)
查看>>
Python assert 断言函数
查看>>
Android 学习笔记之ContentProvider实现数据共享....
查看>>
35)PHP,关于PHP和html
查看>>
区块链到底是什么?
查看>>
java_线程的开启与结束(可用于android)
查看>>