栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring模板对象 -------- JDBCTemplate基本使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring模板对象 -------- JDBCTemplate基本使用

JDBCTemplate基本使用 JdbcTemplate概述

它是spring框架中提供的一个对象,是对原始繁琐的Jdbc API对象的简单封装。spring框架为我们提供了很多的操作模板类。例如:

  • 操作关系型数据的JdbcTemplate和HibernateTemplate
  • 操作nosq|数据库的RedisTemplate,
  • 操作消息队列的JmsTemplate等等。
JdbcTemplate开发步骤 ①导入spring-jdbc和spring-tx坐标
  • spring-jdbc这个包内部封装了jdbc

  • spring-tx:tx就是transaction(事务),JDBC模板操作底层使用的是事务,所以才需要导入这个坐标

  • 在pom文件里面导入坐标

    
      org.springframework
      spring-jdbc
      5.0.5.RELEASE
    
    
      org.springframework
      spring-tx
      5.0.5.RELEASE
    
    
②创建数据库表和实体
  • 在数据库里面创建了一个account表,表里面有name和money两个字段

  • domain实体:Account.java

    public class Account {
    
        private String name;
        private double money;
     .....   
    }
    
③创建JdbcTemplate对象 ④执行数据库操作
@Test
//测试JdbcTemplate开发步骤
public void test1() throws PropertyVetoException {
    //创建数据源对象
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    dataSource.setDriverClass("com.mysql.jdbc.Driver");
    dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
    dataSource.setUser("root");
    dataSource.setPassword("root");

    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    //设置数据源对象  知道数据库在哪
    jdbcTemplate.setDataSource(dataSource);
    //执行操作,插入数据
    int row = jdbcTemplate.update("insert into account values(?,?)", "tom", 5000);
    System.out.println(row);

}
Spring产生JdbcTemplate对象

我们可以将JdbcTemplate的创建权交给Spring,将数据源DataSource的创建权也交给Spring,在Spring容器内部将数据源DataSource注入到JdbcTemplate模版对象中,配置如下:

  • 在applicationContext.xml中配置jabc数据源对象和jdbc模板对象

    
    
    
        
        
    
        
        
            
            
            
            
        
    
        
        
            
        
    
    
    
  • jdbc.properties

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/test
    jdbc.username=root
    jdbc.password=root
    
  • 测试

    @Test
    //测试Spring产生jdbcTemplate对象
    public void test2() throws PropertyVetoException {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class);
        //操作数据库,插入数据
        int row = jdbcTemplate.update("insert into account values(?,?)", "lisi", 5000);
        System.out.println(row);
    }
    
JdbcTemplate的常用操作 修改/删除操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;
	
    //修改操作
    @Test
    public void testUpdate(){
        jdbcTemplate.update("update account set money=? where name=?",10000,"tom");
    }
    
    //删除操作
    @Test
    public void testDelete(){
        jdbcTemplate.update("delete from account where name=?","tom");
    }

}
查询操作
  • 查询多个对象query

    • jdbcTemplate.query("sql语句", new BeanPropertyRowMapper<实体类>(实体类.class));
  • 查询单个对象queryForObject

    • jdbcTemplate.queryForObject("含有?占位符的sql语句", new BeanPropertyRowMapper<实体类>(实体类.class), "第一个占位符的数据","第二个占位符的数据"......);
      • ?:问号占位符,就是在该sql语句中输入的一些条件
    • jdbcTemplate.queryForObject("含有聚合函数的sql语句", 返回的类型.class);
  • queryForObject的使用:

    • 如果是一个实体,想要new一个BeanPropertyRowMapper<实体类>(实体类.class))进行映射
    • 如果是一个简单的类型(比如8个基础数据类型),使用简单类型.class
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;
	
    //聚合查询
    @Test
    public void testQueryCount(){
        Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
        System.out.println(count);
    }
    
	//查询单个对象
    @Test
    public void testQueryOne(){
        Account account = jdbcTemplate.queryForObject("select * from account where name=?", new BeanPropertyRowMapper(Account.class), "tom");
        System.out.println(account);
    }
	
    //查询全部
    @Test
    public void testQueryAll(){
        List accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper(Account.class));
        System.out.println(accountList);
    }

}
总结

①导入spring-jdbc和spring-tx坐标

②创建数据库表和实体

③创建JdbcTemplate对象

JdbcTemplate j dbcTemplate = new JdbcTemplate();
jdbcTemplate . setDa taSource (dataSource);

④执行数据库操作

  • 更新操作:

    jdbcTemplate.update (sql, params)//params就是看sql语句中有几个?占位符,就有几个params
    
  • 查询操作:

    //查询多个对象,返回的是list泛型
    jdbcTemplate.query(sql,Mappe,params)//params就是看sql语句中有几个?占位符,就有几个params
    
    //查询单个
    jdbcTemplate.queryFor0bject (sql,Mapper,params)//params就是看sql语句中有几个?占位符,就有几个params
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/324533.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号