项目目录
基本代码
applicationContext.xml
1 26 7 8 9 10 18 19 2011 12 13 14 15 16 17 21 22 24 25 2623 27 28 3029
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 ListfindAllAccount();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 RowMapperrowMapper = 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 Listaccount = accountDao.findAllAccount();19 for (Account act : account) {20 //循环输出集合中的对象21 System.out.println(act);22 }23 }24 }