Spring04-应用层面

Spring中的 JdbcTemplate

Spring框架中提供的一个对象,是对原始的JDBC进行了一个简单封装。
作用:它就是用于和数据库交互的,实现与数据库的CRUD。
使用之前需要在pom.xml导入两个jar包spring-jdbc-5.0.2.RELEASE.jarspring-tx-5.0.2.RELEASE.jar (它是和事务相关的)

JdbcTemplate的创建

1 导入jar包两个。
2 编写spring配置文件
resources/bean.xml

<?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">
 
  </beans>

3 配置JdbcTemplate

<?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="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
        <!--<property name="jdbcTemplate" ref="jdbcTemplate"></property>-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>


       <!-- 配置一个数据库的操作模板:JdbcTemplate -->
      <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
               <property name="dataSource" ref="dataSource"></property>
        </bean>   

    <!-- 配置数据源 -->
  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  <property name="url" value="jdbc:mysql://localhost:3306/eesy"></property>
  <property name="username" value="root"></property>
  <property name="password" value="1234"></property> 
</bean> 
  </beans>

JdbcTemplate的CRUD操作

/**
*  JdbcTemplate的CRUD用法
*/
public  class JdbcTemplateDemo1{
      public static void main (String [ ] args){
        //1 获取容器
              ApplicationContext ac = ClassPathXmlApplicationContext("bean.xml");
       // 2 获取对象
                JdbcTemplate  jt = ac.getBean("jdbcTemplate",JdbcTemplate.class);
      // 3 执行操作
          //保存
                jt.update("insert into account (name,money) values ("?",?)","eee",333f);
          //更新
                jt.update("update account set name=?,money= ? where id=?" ,"test",4567,7);  
          //删除
                jt.update("delete from account where id =?" ,8);   
          //查询所有
             List<Account> accounts = jt.query("select * from account where money > ?,new BeanPropertyRowMapper<Account>(Account.class),1000f");
                           for(Account account : accounts){
                            System.out.println(account);
                          }
          //查询一个
      List<Account> accounts = jt.query("select * from account where id=? > ,new BeanPropertyRowMapper<Account>(Account.class),1");
      System.out.println(accounts.isEmpty()?"没有内容:"account.get(0));
          //查询多个
Long count = jt.queryForObject("select count(*) from account where money >?",Long.class,1000f);
       System.out.println(count);
  }
}

/**
*  定义Account的封装策略
*/
public class AccountRowMapper implements RowMapper<Account>{
      //把结果集中的数据封装到Account中,然后由Spring把每个Account加入集合中 

       @Override
          public Account mapRow(ResultSet rs ,int rowNum) throw SQLException{
                 Account account = new Account();
                  account .setId(rs.getInt("id"));
                  account.setName(rs.getString("name"));
                  account.setMoney(rs.getFloat("money"));
                return  account;
  }
}



©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容