JdbcTemplate实现增删改查操做

JdbcTemplate介绍

为了使 JDBC 更加易于使用,Spring 在 JDBCAPI 上定义了一个抽象层, 以此创建一个JDBC存取框架,Spring Boot Spring Data-JPA。html

做为 SpringJDBC 框架的核心, JDBC 模板的设计目的是为不一样类型的JDBC操做提供模板方法. 每一个模板方法都能控制整个过程,并容许覆盖过程当中的特定任务。spring

经过这种方式,能够在尽量保留灵活性的状况下,将数据库存取的工做量降到最低。sql

JdbcTemplate方法介绍

JdbcTemplate主要提供如下五类方法:数据库

一、execute方法:能够用于执行任何SQL语句,通常用于执行DDL语句;app

       Execute、executeQuery、executeUpdate框架

二、update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句 SQL SERVCER(GO SQL语句 GO) ;ide

三、query方法及queryForXXX方法:用于执行查询相关语句;函数

四、call方法:用于执行存储过程、函数相关语句。测试

JdbcTemplate实现增删改查

JdbcTemplate添加数据

1. 使用配置实现
1.1 建立实体类
public class Account { private Integer accountid; private String accountname; private Double balance; public Integer getAccountid() { return accountid; } public void setAccountid(Integer accountid) { this.accountid = accountid; } public String getAccountname() { return accountname; } public void setAccountname(String accountname) { this.accountname = accountname; } public Double getBalance() { return balance; } public void setBalance(Double balance) { this.balance = balance; } }
实体类

 

1.2 建立Dao
//查询全部全部帐户
public List<Account> getAllAccounts();
查询方法

 

.3 建立Dao层实现类并继承JdbcDaoSupport接口this

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { @Override public List<Account> getAllAccounts() { JdbcTemplate jdbcTemplate = getJdbcTemplate(); String sql = "select * from  accounts"; //RowMapper:接口 封装了记录的行映射关系
        List<Account> lists = jdbcTemplate.query(sql, new RowMapper<Account>() { @Override public Account mapRow(ResultSet resultSet, int i) throws SQLException { //建立Account对象
                Account account = new Account(); //从ResultSet中解数据保到Accounts对象中
                account.setAccountid(resultSet.getInt("accountid")); account.setAccountname(resultSet.getString("accountname")); account.setBalance(resultSet.getDouble("balance")); return account; } }); return account; }
实现查询方法

 

1.4建立Service
//查询全部全部帐户
public List<Account> getAllAccounts();
查询方法

 

1.5建立Service层实现类
AccountDao accountDao; @Override public List<Account> getAllAccounts() { List<Account> allAccounts = accountDao.getAllAccounts(); return allAccounts; }
实现查询方法

 

 

1.6 编写applicationContext.xml文件
<!--识别到配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源-->
<!--spring内置的数据源:提供链接的,不负责管理,使用链接池-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>
<!--构建jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<bean id="accountDao" class="cn.spring.accounttest.dao.impl.AccountDaoImpl">
    <property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--Service-->
<bean id="accountService" class="cn.spring.accounttest.service.impl.AccountServiceImpl">
    <property name="accountDao" ref="accountDao"></property>
</bean>
applicationContext.xml

 

1.7编写测试类 
@Test public void getAllAccount(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); //从spring容器中获取Service对象
    AccountService accountService = (AccountService)context.getBean("accountService"); List<Account> allAccounts = accountService.getAllAccounts(); for (Account account:allAccounts) { System.out.println("帐户名:"+account.getAccountname()+",余额为:"+account.getBalance()); } }
查询测试类

 

 

2. 使用注解方式实现
2.1 建立实体类

 

实体类

 

 

2.2 建立Dao
查询方法

 

2.3 建立Dao层实现类
@Repository public class AccountDaoImpl implements AccountDao { @Autowired private JdbcTemplate jdbcTemplate; Account account = new Account(); @Override public List<Account> getAllAccounts() { String sql = "select * from  accounts"; //自动映射
        RowMapper<Account> rowMapper = new BeanPropertyRowMapper<>(Account.class); List<Account> query = jdbcTemplate.query(sql, rowMapper); for (Account account : query) { System.out.println(account); } return query; } }
Dao实现类

 

2.4建立Service
查询方法

 

 

2.5建立Service层实现类
实现查询方法

 

2.6编写applicationContext.xml文件
<!--扫描注解:包扫描器-->
<context:component-scan base-package="cn.spring"/>

<!--识别到配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源-->
<!--spring内置的数据源:提供链接的,不负责管理,使用链接池-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>
<!--构建jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
</bean>
applicationContext.xml

 

 

2.7编写测试类
查询测试类

 

 

JdbcTemplate实现增删改操做

使用注解方式实现,配置式同添加操做

1.建立Dao
//删除帐户
public int delAccount(int id); //添加用户
public int addAccount(Account account); //修改帐户
public int updaAccount(Account account);
增删改方法

 

 

2.建立Dao曾实现类
@Override public int delAccount(int id) { String sql="delete from accounts where accountid=2"; int count = jdbcTemplate.update(sql); return count; } @Override public int addAccount(Account account) { String sql="insert into Accounts(accountname,balance) values(?,?)"; int count = jdbcTemplate.update(sql,account.getAccountname(),account.getBalance()); return count; } @Override public int updaAccount(Account account) { String sql="update accounts set accountname=?,balance=? where accountid=?"; int count = jdbcTemplate.update(sql, account.getAccountname(),account.getBalance(),account.getAccountid() ); return count; }
增删改方法实现类

 

 

3. 建立Service
增删改方法

 

 

4. 建立Service层实现类
增删改方法实现类

 

 

5. 编写applicationContext.xml文件
applicationContext.xml

 

 

6. 编写测试类
@Test public void delAccount(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService accountService =(AccountService) context.getBean("accountServiceImpl"); int i = accountService.delAccount(2); if (i>0){ System.out.println("删除成功"); } } @Test public void addAccount(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService accountServiceImpl = (AccountService) context.getBean("accountServiceImpl"); Account account=new Account(); account.setAccountname("刘磊"); account.setBalance(Double.valueOf(784)); int count = accountServiceImpl.addAccount(account); if (count>0){ System.out.println("添加成功"); } } @Test public void updaAcccount(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService accountServiceImpl = (AccountService) context.getBean("accountServiceImpl"); Account account=new Account(); account.setAccountid(10); account.setAccountname("刘磊"); account.setBalance(Double.valueOf(784)); int count = accountServiceImpl.updaAccount(account); if (count>0){ System.out.println("修改为功"); } }
增删改测试类

 

原文出处:https://www.cnblogs.com/szhhhh/p/11781308.html

相关文章
相关标签/搜索