Spring-JDBCTemplate

Spring 框架针对数据库开发中的应用提供了 JDBCTemplate 类,该类是 Spring 对 JDBC 支持的核心,它提供了所有对数据库操作功能的支持。

Spring 框架提供的JDBC支持主要由四个包组成,分别是 core(核心包)、object(对象包)、dataSource(数据源包)和 support(支持包),org.springframework.jdbc.core.JdbcTemplate 类就包含在核心包中。作为 Spring JDBC 的核心,JdbcTemplate 类中包含了所有数据库操作的基本方法。

JdbcTemplate 类继承自抽象类 JdbcAccessor,同时实现了 JdbcOperations 接口。其直接父类 JdbcAccessor 为子类提供了一些访问数据库时使用的公共属性,具体介绍如下。

1)DataSource

其主要功能是获取数据库连接,具体实现时还可以引入对数据库连接的缓冲池和分布式事务的支持,它可以作为访问数据库资源的标准接口。

2)SQLExceptionTranslator

org.springframework.jdbc.support.SQLExceptionTranslator 接口负责对 SQLException 进行转译工作。通过必要的设置或者获取 SQLExceptionTranslator 中的方法,可以使 JdbcTemplate 在需要处理 SQLException 时,委托 SQLExceptionTranslator 的实现类完成相关的转译工作。

JdbcOperations 接口定义了在 JdbcTemplate 类中可以使用的操作集合,包括添加、修改、查询和删除等操作。

Spring 中 JDBC 的相关信息是在 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> 

<!-- 配置数据源 --> 
<bean id="dataSource" class="org.springframework.jdbc.dataSource.DriverManagerDataSource">
<!--数据库驱动-->
<property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
<!--连接数据库的url-->
<property name= "url" value="jdbc:mysql://localhost/spring" />
<!--连接数据库的用户名-->
<property name="username" value="root" />
<!--连接数据库的密码-->
<property name="password" value="root" />
</bean>
<!--配置JDBC模板-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.jdbcTemplate">
<!--默认必须使用数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置注入类-->
<bean id="xxx" class="xxx">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
...
</beans>

在上述代码中,定义了三个 Bean,分别是 dataSource、jdbcTemplate 和需要注入类的 Bean。其中 dataSource 对应的是 DriverManagerDataSource 类,用于对数据源进行配置;jdbcTemplate 对应 JdbcTemplate 类,该类中定义了 JdbcTemplate 的相关配置。

在 dataSource 中,定义了四个连接数据库的属性,如表 1 所示。

表 1 dataSource的四个属性
属性名含义
driverClassName 所使用的驱动名称,对应驱动 JAR 包中的 Driver 类
url 数据源所在地址
username 访问数据库的用户名
password 访问数据库的密码


表 1 中的属性值需要根据数据库类型或者机器配置的不同进行相应设置。如果数据库类型不同,则需要更改驱动名称。如果数据库不在本地,则需要将 localhost 替换成相应的主机 IP。

在定义 jdbcTemplate 时,需要将 dataSource 注入 jdbcTemplate 中。而在其他的类中要使用 jdbcTemplate,也需要将 jdbcTemplate 注入使用类中(通常注入 dao 类中)。

在 JdbcTemplate 类中,提供了大量的查询和更新数据库的方法,如 query()、update() 等.

1.1.1 导入jar

 1.1.2Ctiy实体

public class City implements Serializable {
    private Integer cid;
    private String cname;
    private Integer pid;

    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }
}

Dao层

public interface AccountDao
{
    //汇款
    public void out(String outUser,Integer money);

    //收款
    public void in(String inUser,Integer money);

    //查询方法
    public List<City> city();

    //删除方法

    public Integer delect(Integer de);

    //增加方法

    public Integer add(City city);
}

Dao层实现类

@Repository
public class AccountDaoImpl  implements AccountDao {
    @Resource
    private JdbcTemplate jdbcTemplate;

    //汇款的实现方法
    @Override
    public void out(String outUser, Integer money) {
        this.jdbcTemplate.update("update city set pid=pid-? where cid=?",money,outUser);
    }
    //收款的实现方法
    @Override
    public void in(String inUser, Integer money) {
        this.jdbcTemplate.update("update city set pid=pid+? where cid=?",money,inUser);
    }

    RowMapper<City> rowMapper=new BeanPropertyRowMapper<>(City.class);
    @Override
    public List<City> city() {
        return jdbcTemplate.query("select * from city",rowMapper);
    }

    @Override
    public Integer delect(Integer de) {

        return jdbcTemplate.update("delete from city where cid=?",de);
    }
    @Override
    public Integer add(City city){
        String xml="insert INTO city(cid,cname,pid) values (?,?,?)";
        Object[] objects={city.getCid(),city.getCname(),city.getPid()};
        return jdbcTemplate.update(xml,objects);
    }
}

Service层

public interface AccountService {
    //转账
    public void transfet(String outUser,String inUser,Integer money);
    //查询方法
    public List<City> city();

    //删除方法

    public Integer delect(Integer de);

    //增加方法

    public Integer add(City city);
}

Service实现类

@Service("accountService")
public class AccountServiceImpl implements AccountService {
    @Resource
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }


    @Override
    public void transfet(String outUser, String inUser, Integer money) {
        this.accountDao.out(outUser,money);
        this.accountDao.in(inUser, money);
    }

    @Override
    public List<City> city() {
        return accountDao.city();
    }

    @Override
    public Integer delect(Integer de) {

        return accountDao.delect(de);
    }
    @Override
    public Integer add(City city) {

        return accountDao.add(city);
    }
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--根节点是我们的beans节点-->
<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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <!--扫描包-->
    <context:component-scan base-package="com.mengma"/>
    <!--加载properties文件-->
    <context:property-placeholder location="classpath:c3p0-db.properties"/>
    <!--配置数据源。读取properties文件信息-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}" />
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
        <property name="user" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />

    </bean>
    <!-- 配置dao -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>

c3p0-db.properties

jdbc.driverClass =com.mysql.jdbc.Driver
jdbc.jdbcUrl = jdbc:mysql://localhost:3306/test?serverTimezone=UTC 
jdbc.user = root
jdbc.password = root

测试

   @Test
    public void shouldAnswerWithTrue()
    {
        // 获得Spring容器,并操作
        String xmlPath = "applicationContext.xml";
       ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        AccountService accountService = (AccountService) applicationContext.getBean("accountService");
        //修改操作
        accountService.transfet("130100", "130400", 100);
        //查询操作
        List<City> cityList=accountService.city();
        for (City city:cityList){
            System.out.println(city.getCname());
        }
        //删除操作
        accountService.delect(130000);
        //添加操作
        City city=new City();
        city.setCid(130000);
        city.setCname("超哥");
        city.setPid(12314);
        Integer add = accountService.add(city);

    }

以上代码是实现注解方式

结构

 希望对你们有帮助,谢谢

原文地址:https://www.cnblogs.com/lowerma/p/11781537.html