本文共 12150 字,大约阅读时间需要 40 分钟。
package com.yzb.chapter04.example1;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.jdbc.core.JdbcTemplate;/** 使用execute()方法进行创建表的工作* */public class JdbcTemplateTest { /* * 第一种方式 * */ public static void main(String[] args) { String path = "com/yzb/chapter04/example1/applicationContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(path); JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate"); //使用execute()方法执行SQL语句,创建用户账户管理表acount jdbcTemplate.execute("create table account("+ "id int primary key auto_increment,"+ "uesrname varchar(50),"+ "balance double)"); System.out.println("将建用户表account成功"); } /* * 第二种方式:单元测试 * */ @Test public static void mainTest() { String path = "com/yzb/chapter04/example1/applicationContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(path); JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate"); //使用execute()方法执行SQL语句,创建用户账户管理表acount jdbcTemplate.execute("create table account("+ "id int primary key auto_increment,"+ "uesrname varchar(50),"+ "balance double)"); System.out.println("将建用户表account成功"); }}
package com.yzb.chapter04.example2;public class Account { private Integer id; private String username; private Double balance; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Double getBalance() { return balance; } public void setBalance(Double balance) { this.balance = balance; } @Override public String toString() { return "Account{" + "id=" + id + ", username='" + username + '\'' + ", balance=" + balance + '}'; }}
package com.yzb.chapter04.example2;public interface AccountDao { //添加账户 public int addAccount(Account account);// 更改用户 public int updateAccount(Account account); //删除用户 public int deleteAccount(int id );}
package com.yzb.chapter04.example2;import org.springframework.jdbc.core.JdbcTemplate;public class AccountDaoImpl implements AccountDao { //声明JdbcTemplate属性以及setter方法 private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } //添加用户 @Override public int addAccount(Account account) { //定义SQL String sql = "insert into account(username,balance) value(?,?)"; //定义一个数组存储sql中的参数 Object[] obj = {account.getUsername(),account.getBalance()}; //执行添加操作,返回的是受sql语句影响的记录条数 int update = jdbcTemplate.update(sql, obj); return update; } //更改用户 @Override public int updateAccount(Account account) { //定义SQL String sql = "update account set username=?,balance=? where id=?"; //定义一个数组存储sql中的参数 Object[] obj = {account.getUsername(),account.getBalance(),account.getId()}; //执行更改操作,返回的是受sql语句影响的记录条数 int update = jdbcTemplate.update(sql, obj); return update; } //删除用户 @Override public int deleteAccount(int id) { //定义sql String sql = "delete from account where id=?"; //执行更改操作,返回的是受sql语句影响的记录条数 int update = jdbcTemplate.update(sql, id); return update; }}
package com.yzb.chapter04.example2;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class UpdateTest { public static void main(String[] args) { //加载配置文件 String path = "com/yzb/chapter04/example2/applicationContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(path); //获取AccountDao的实例 AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao"); //创建Account的对象 Account account = new Account(); account.setUsername("tom"); account.setBalance(1234.00); int num = accountDao.addAccount(account); if(num >0){ System.out.println("成功插入了"+num+"条数据"); }else { System.out.println("插入失败"); } }}
package com.yzb.chapter04.example2;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class UpdateTest1 { public static void main(String[] args) { //加载配置文件 String path = "com/yzb/chapter04/example2/applicationContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(path); //获取AccountDao的实例 AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao"); //创建Account的对象 Account account = new Account(); account.setUsername("tom"); account.setId(1); account.setBalance(1234.00); int num = accountDao.updateAccount(account); if(num >0){ System.out.println("成功修改了"+num+"条数据"); }else { System.out.println("修改失败"); } }}
package com.yzb.chapter04.example2;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class UpdateTest2 { public static void main(String[] args) { //加载配置文件 String path = "com/yzb/chapter04/example2/applicationContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(path); //获取AccountDao的实例 AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao"); int num = accountDao.deleteAccount(1); if(num >0){ System.out.println("成功删除了"+num+"条数据"); }else { System.out.println("删除失败"); } }}
package com.yzb.chapter04.example2;import java.util.List;public interface AccountDao { //添加账户 public int addAccount(Account account);// 更改用户 public int updateAccount(Account account); //删除用户 public int deleteAccount(int id ); //通过id查询 public Account findAccountById(int id); //查询所有 public ListfindAllAccount();}
package com.yzb.chapter04.example2;import org.springframework.jdbc.core.BeanPropertyRowMapper;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.RowMapper;import java.util.List;public class AccountDaoImpl implements AccountDao { //声明JdbcTemplate属性以及setter方法 private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } //添加用户 @Override public int addAccount(Account account) { //定义SQL String sql = "insert into account(username,balance) value(?,?)"; //定义一个数组存储sql中的参数 Object[] obj = {account.getUsername(),account.getBalance()}; //执行添加操作,返回的是受sql语句影响的记录条数 int update = jdbcTemplate.update(sql, obj); return update; } //更改用户 @Override public int updateAccount(Account account) { //定义SQL String sql = "update account set username=?,balance=? where id=?"; //定义一个数组存储sql中的参数 Object[] obj = {account.getUsername(),account.getBalance(),account.getId()}; //执行更改操作,返回的是受sql语句影响的记录条数 int update = jdbcTemplate.update(sql, obj); return update; } //删除用户 @Override public int deleteAccount(int id) { //定义sql String sql = "delete from account where id=?"; //执行更改操作,返回的是受sql语句影响的记录条数 int update = jdbcTemplate.update(sql, id); return update; } //通过ID查询账户 @Override public Account findAccountById(int id) { //定义sql语句 String sql ="select * from account where id=?"; //创建一个新的BeanPropertyRowMapper对象 RowMapperrowMapper = new BeanPropertyRowMapper (Account.class); //将id保定到sql语句中,通过RowMapper返回一个Object类型的单行记录 Account account = this.jdbcTemplate.queryForObject(sql, rowMapper, id); return account; } //查询所有的账户信息 @Override public List findAllAccount() { //定义sql语句 String sql ="select * from account"; //创建一个新的BeanPropertyRowMapper对象 RowMapper rowMapper = new BeanPropertyRowMapper (Account.class); //执行静态的sql语句查询,通过RowMapper返回结果 List query = this.jdbcTemplate.query(sql, rowMapper); return query; }}
package com.yzb.chapter04.example2;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class UpdateTest3 { public static void main(String[] args) { //加载配置文件 String path = "com/yzb/chapter04/example2/applicationContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(path); //获取AccountDao的实例 AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao"); Account accountById = accountDao.findAccountById(2); System.out.println(accountById); }}
package com.yzb.chapter04.example2;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.List;public class UpdateTest4 { public static void main(String[] args) { //加载配置文件 String path = "com/yzb/chapter04/example2/applicationContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(path); //获取AccountDao的实例 AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao"); ListallAccount = accountDao.findAllAccount(); for (Account account : allAccount) { System.out.println(account); } }}
转载地址:http://bzwki.baihongyu.com/