JAVA--高级基础开发

♀尐吖头ヾ 提交于 2020-03-13 21:01:59

第三章SpringJdbcTemplate模板类

3.1 JdbcTemplate模板类

  1. 它是spring框架中提供的一个对Jdbc进行封装的一个模板类,类似于我们使用的commons-dbutils这个jdbc的开源jar包。如果要使用JdbcTemplate,需要额外导入两个依赖包:
  2. <!--spring-jdbc封装的Jar-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.1.6.RELEASE</version>
    </dependency>
    <!--事物spring-tx -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>5.1.6.RELEASE</version>
    </dependency>
  3. Spring中数据源的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!--映入外部的属性文件-->
<context:property-placeholder location="classpath:ab.properties"/>

<!--配置C3P0的数据源-->
<bean id="c3p0DataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass"  value="${driverClassName}"/>
    <property name="jdbcUrl"  value="${url}"/>
    <property name="user"  value="${root}"/>
    <property name="password"  value="${root}"/>
</bean>
<!--spring框架提供了一个内置的数据源,它位于spring-jdbc包中-->
<bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${driverClassName}"/>
    <property name="url" value="${url}"/>
    <property name="username" value="${root}"/>
    <property name="password" value="${root}"/>
</bean>
<!--配置阿里Druid数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
    <property name="driverClassName" value="${driverClassName}"/>
    <property name="url" value="${url}"/>
    <property name="username" value="${user}"/>
    <property name="password" value="${password}"/>
</beans>

3将数据库信息连接到属性文件中

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ab_wzy?serverTimezone=GMT
user=root
password=root

beans.xml文件中配置JdbcTemplate

<!--配置JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置持久层-->
<bean id="accountDao" class="com.bdit.dao.impl.IAccountDaoImpl">
    <property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--配置业务层-->
<bean id="accountService" class="com.bdit.Service.impl.AccountServiceImpl">
    <property name="accountDao" ref="accountDao"></property>
</bean>

- ----实现CRUD-----

import com.bdit.Service.IAccountService;
import com.bdit.entify.Account;
import java.util.List;
public interface IAccountDao {
    public  int   save(Account account);
    public  int   update(Account account);
    public  int delete(Integer  id);
    public Account  findByid(Integer  id);
    public List<Account> findAll();
}
package com.bdit.dao.impl;

import com.bdit.dao.IAccountDao;
import com.bdit.entify.Account;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.swing.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository   //此注解用于持久层的注解
public class IAccountDaoImpl implements IAccountDao {
 private JdbcTemplate jdbcTemplate;
 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
   @Override
    public int save(Account account) {
        String sql="insert into  account (zid,money)values(?,?)";
        int i=jdbcTemplate.update(sql,account.getZid(),account.getMoney());
        return i;
    }

    @Override
    public int update(Account account) {
        String sql="update account set uid=?,money=? where zid=?";
        int i=jdbcTemplate.update(sql,account.getZid(),account.getUid(),account.getMoney());
        return i;
    }

    @Override
    public int delete(Integer id){
        String sql="delete from  account where zid=?";
        int i=jdbcTemplate.update(sql,id);
        return i;
    }

    @Override
    public Account findByid(Integer id) {
        String sql="select  *  from  account where zid=?";
        Account account=jdbcTemplate.queryForObject(sql,new AccountRowMapper(),id);
        return account;
    }

    @Override
    public List<Account> findAll() {
       String  sql="select  *  from  account";
       List<Account>list=jdbcTemplate.query(sql,new AccountRowMapper());
       return list;
    };
    public class AccountRowMapper implements RowMapper<Account>{
        @Override
        public Account mapRow(ResultSet resultSet, int i) throws SQLException {
            Account account=new Account();
            account.setZid(resultSet.getInt("zid"));
            account.setUid(resultSet.getInt("uid"));
            account.setMoney(resultSet.getDouble("money"));
            return account;
        }
    }
}
package com.bdit.Service.impl;
import com.bdit.Service.IAccountService;
import com.bdit.dao.IAccountDao;
import com.bdit.entify.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private IAccountDao accountDao;
    public void setAccountDao(IAccountDao accountDao) {
           this.accountDao = accountDao;
       }
 @Override
    public int save(Account account) {
       return  accountDao.save(account);
    }

    @Override
    public int update(Account account) {
        return accountDao.update(account);
    }
    @Override
    public int  delete(Integer id) {
        return accountDao.delete(id);
    }
    @Override
    public Account findByid(Integer id) {
        return accountDao.findByid(id);
    }
  @Override
    public List<Account> findAll() {
        return accountDao.findAll();
    }
}

测试类

//基于纯注解的测试
@RunWith(SpringJUnit4ClassRunner.class)
//指定Spring配置文件的位置
@ContextConfiguration(locations = {"classpath:Spring.xml"})
public class DataSpp {
    @Autowired
    private IAccountService iAccountService;
    @Test
    public  void  testFind(){
        List<Account>list=iAccountService.findAll();
        for(Account  account:list){
            System.out.println(account);
        }
    }
//properties 中不能用 username 作为变量,这种方式会注入自己的系统环境变量的 用户名,本来是 root ,不应是那个 windows 用户名

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!