spring的数据库开发和常用方法的介绍(八)

一、spring Jdbc Template的常用方法execute()

1、导入相关的jar包

spring的数据库开发和常用方法的介绍(八)_第1张图片

2、在src目录下创建applicationContext.xml文件,在该文件中配置数据源和配置jdbc模板






    
    
    
    
    
    
    








3、进行测试,在com.itheima.jdbc包下,创建测试类JdbcTemplateTest.java

package com.itheima.jdbc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class JdbcTemplateTest {

    public static void main(String[] args){
        //加载配置文件
        ApplicationContext app2=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取JdbcTemplate实例
        JdbcTemplate jdbctemplate=(JdbcTemplate)app2.getBean("jdbcTemplate");
        String sql= "create table account("+"id int primary key auto_increment,"+"username varchar(50),"+"balance double)";
        jdbctemplate.execute(sql);
        System.out.print("账户表account创建成功");
    }
}

二、spring Jdbc Template的常用方法update()

update()方法可以进行插入、更新和删除数据的操作。
1、导入相关的jar包,同上
2、com.itheima.jdbc创建Account.java的实体类

package com.itheima.jdbc;

public class Account {
    private Integer id;//账户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 + "]";
    }
}

3、com.itheima.jdbc包下创建接口类AccountDao.java和其实现类AccountDaoImpl.java

package com.itheima.jdbc;

public interface AccountDao {
    public int addAcount(Account account);
    public int updateAccount(Account account);
    public int deleteAccount(int id);
}
package com.itheima.jdbc;

import org.springframework.jdbc.core.JdbcTemplate;

public class AccountDaoImpl implements AccountDao {
    private JdbcTemplate jdbcTempalet;
    public void setJdbcTempalet(JdbcTemplate jdbcTempalet) {
        this.jdbcTempalet = jdbcTempalet;
    }
    

    @Override
    public int addAcount(Account account) {
        // TODO Auto-generated method stub
        String sql="insert into account(username,balance) value(?,?)";
        Object[] obj=new Object[]{account.getUsername(),account.getBalance()};
        //执行添加操作,返回的是受sql语句影响的记录条数
        int num=this.jdbcTempalet.update(sql,obj);
        return num;
    }

    @Override
    public int updateAccount(Account account) {
        // TODO Auto-generated method stub
        String sql="update account set username=?,balance=? where id=?";
        Object[] obj=new Object[]{account.getUsername(),account.getBalance(),account.getId()};
        int num=this.jdbcTempalet.update(sql,obj);
        return num;
    }

    @Override
    public int deleteAccount(int id) {
        // TODO Auto-generated method stub
        String sql= "delete from account where id=?";
        int num=this.jdbcTempalet.update(sql,id);
        return num;
    }

}

4、在src目录下创建applicationContext.xml文件






    
    
    
    
    
    
    













5、在com.itheima.jdbc中创建测试类JdbcTemplateTest.java

package com.itheima.jdbc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class JdbcTemplateTest {

    @Test
    public void addAccountTest(){
        //加载配置文件
        ApplicationContext app2=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取JdbcTemplate实例
        AccountDao accountdao=(AccountDao)app2.getBean("accountDao");
        Account account=new Account();
        account.setUsername("tom");
        account.setBalance(2000.00);
        //执行addAccount()方法,获取返回结果
        int num=accountdao.addAcount(account);
        if(num>0)
        {
            System.out.print("成功插入"+num+"条数据!");
        }else{
            System.out.print("插入操作执行失败");
            
        }
        
    }
    
    
    
    @Test
    public void updateAccountTest(){
        //加载配置文件
        ApplicationContext app2=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取JdbcTemplate实例
        AccountDao accountdao=(AccountDao)app2.getBean("accountDao");
        Account account=new Account();
        account.setId(1);
        account.setUsername("tom");
        account.setBalance(2000.00);
        //执行addAccount()方法,获取返回结果
        int num=accountdao.updateAccount(account);
        if(num>0)
        {
            System.out.print("成功修改了"+num+"条数据!");
        }else{
            System.out.print("修改操作执行失败");
            
        }
        
    }
    
    
    
    
    @Test
    public void deleteAccountTest(){
        //加载配置文件
        ApplicationContext app2=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取JdbcTemplate实例
        AccountDao accountdao=(AccountDao)app2.getBean("accountDao");
        //执行addAccount()方法,获取返回结果
        int num=accountdao.deleteAccount(1);
        if(num>0)
        {
            System.out.print("成功删除"+num+"条数据!");
        }else{
            System.out.print("删除操作执行失败");
            
        }
        
    }
}

三、spring Jdbc Template的常用方法query()

1、在上面的情况下在AccountDao.java里面创建一个通过id查询单个账户和查询所有账户的方法,并在实现类中实现其相应的方法,具体如下:

public Account findAccountById(int id);
public List findAll();
    @Override
    public Account findAccountById(int id) {
        // TODO Auto-generated method stub
        String sql ="select * from account where id=?";
        //创建一个BeanPropertyRowMapper对象,
        RowMapper rowmapper=new BeanPropertyRowMapper(Account.class);
        //将id捆绑到sql语句中,并且通过RowMapper返回一个Object类型的单行数据
        return this.jdbcTempalet.queryForObject(sql, rowmapper, id);//这里会将Object类型的数据转化为Account对象的类型数据
    }


    @Override
    public List findAll() {
        // TODO Auto-generated method stub
        String sql ="select * from account";
        RowMapper rowmapper=new BeanPropertyRowMapper(Account.class);
        return this.jdbcTempalet.query(sql, rowmapper);//这里会将Object类型的数据转化为Account对象的类型数据
    }

2、在上面的测试类中加上测试方法

    @Test
    public void findAccountTest(){
        //加载配置文件
        ApplicationContext app2=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取JdbcTemplate实例
        AccountDao accountdao=(AccountDao)app2.getBean("accountDao");
        //执行addAccount()方法,获取返回结果
        Account account=accountdao.findAccountById(2);
        System.out.print(account);
        
    }
    
    @Test
    public void findAllAccountTest(){
        //加载配置文件
        ApplicationContext app2=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取JdbcTemplate实例
        AccountDao accountdao=(AccountDao)app2.getBean("accountDao");
        //执行addAccount()方法,获取返回结果
        List account=accountdao.findAll();
        for(Account act:account){
        System.out.print("名字:"+act.getUsername()+","+"金额"+act.getBalance()+"\n");
        }
    }

你可能感兴趣的:(spring的数据库开发和常用方法的介绍(八))