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

jdbctemplate.query(jdbctemplate.update)

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

jdbctemplate.query(jdbctemplate.update)

Spring JdbcTemplate的使用

1 JdbcTemplate开发步骤2 JdbcTemplate常用操作

2.1 更新、删除操作2.2 查询操作

1 JdbcTemplate开发步骤
① 导入spring-jdbc和spring-tx坐标
② 创建数据库表和实体
③ 创建JdbcTemplate对象
④ 执行数据库操作

① 导入spring-jdbc和spring-tx坐标基本包以及连接池数据库等坐标


    
    org.springframework
    spring-context
    5.0.5.RELEASE


    org.springframework
    spring-jdbc
    5.0.5.RELEASE


    org.springframework
    spring-tx
    5.0.5.RELEASE


    c3p0
    c3p0
    0.9.1.2


    mysql
    mysql-connector-java
    8.0.25


    junit
    junit
    4.11
    test

② 创建数据库表和实体

CREATE TABLE account (
	name VARCHAR ( 10 ),
	money DOUBLE 
)
public class Account {
    private String name;
    private String money;

    public void setName(String name) {
        this.name = name;
    }

    public void setMoney(String money) {
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public String getMoney() {
        return money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "name='" + name + ''' +
                ", money='" + money + ''' +
                '}';
    }
}

③ 创建JdbcTemplate对象:这里使用C3P0数据库连接池
在applicationContext.xml中配置数据源对象等:




    
    
    
    
    
        
        
        
        
    

    
    
        
    

④ 执行数据库操作

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class JdbcTemplateTest {
    @Test
    //测试spring产生jdbc模板
    public void test1() {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class);
        int row = jdbcTemplate.update("insert into account values (?,?)", "lisi", 1000);
        System.out.println(row);
    }
}
2 JdbcTemplate常用操作

使用spring进行测试:

1) 导入spring-test坐标
2)	@RunWith(SpringJUnit4ClassRunner.class)
3) @ContextConfiguration("classpath:applicationContext.xml")

    org.springframework
    spring-test
    5.0.5.RELEASE

2.1 更新、删除操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringTest {
    @Autowired  //注入
    private JdbcTemplate jdbcTemplate;

    @Test
    public void testUpdate() {
        jdbcTemplate.update("update account set money = ? where name = ?", 200, "lisi");
    }

    @Test
    public void testDelete() {
        jdbcTemplate.update("delete from account where name = ?", "lisi");
    }
}
2.2 查询操作



@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringTest {
    @Autowired  //注入
    private JdbcTemplate jdbcTemplate;

    @Test
    public void testQueryOne() {
        //查询单个
        Account query = jdbcTemplate.queryForObject("select * from account where name = ?", new BeanPropertyRowMapper(Account.class), "黄蓉");
        System.out.println(query);
    }

    @Test
    public void testQueryAll() {
        //查询全部
        List accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper(Account.class));
        System.out.println(accountList);
    }

    @Test
    public void testQueryAccount() {
        //查询总数
        Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
        System.out.println(count);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/776846.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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