JdbcTemplate查询操作

举报
执久呀 发表于 2022/10/22 21:20:01 2022/10/22
【摘要】 ​ 目录查询操作查询所有 查询单个聚合查询 知识要点总结查询操作查询所有查询操作需要放入字节码对象.class @Test public void testQueryAll(){//后面括号中需要放入字节码对象 List<Account> accountList = jdbcTemplate.query("select * from account", new Be...

 目录


查询操作

查询所有

 查询单个

聚合查询

 知识要点总结




查询操作

查询所有

查询操作需要放入字节码对象.class

    @Test
    public void testQueryAll(){
//后面括号中需要放入字节码对象
        List<Account> accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
        System.out.println(accountList);
    }

数据库中

编辑


 Junit4.11版本导致错误

一开始我使用了junit4.11的版本

编辑
C:\java64\jdk1.8.0_131\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=4613:D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.3\plugins\junit\lib\junit5-rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.3\plugins\junit\lib\junit-rt.jar;C:\java64\jdk1.8.0_131\jre\lib\charsets.jar;C:\java64\jdk1.8.0_131\jre\lib\deploy.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\nashorn.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\java64\jdk1.8.0_131\jre\lib\javaws.jar;C:\java64\jdk1.8.0_131\jre\lib\jce.jar;C:\java64\jdk1.8.0_131\jre\lib\jfr.jar;C:\java64\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\java64\jdk1.8.0_131\jre\lib\jsse.jar;C:\java64\jdk1.8.0_131\jre\lib\management-agent.jar;C:\java64\jdk1.8.0_131\jre\lib\plugin.jar;C:\java64\jdk1.8.0_131\jre\lib\resources.jar;C:\java64\jdk1.8.0_131\jre\lib\rt.jar;D:\小项目\webTest\target\test-classes;D:\小项目\webTest\target\classes;E:\Maven_project\lib\javax.transaction.jar;E:\Maven_project\lib\javax.annotation.jar;E:\Maven_project\lib\javax.jms.jar;E:\Maven_project\lib\javax.resource.jar;E:\Maven_project\lib\javax.ejb.jar;E:\Maven_project\lib\javax.persistence.jar;E:\Maven_project\lib\javax.servlet.jar;E:\Maven_project\lib\javax.servlet.jsp.jstl.jar;E:\Maven_project\lib\javax.servlet.jsp.jar;C:\Users\执久\.m2\repository\mysql\mysql-connector-java\5.1.32\mysql-connector-java-5.1.32.jar;C:\Users\执久\.m2\repository\c3p0\c3p0\0.9.1.2\c3p0-0.9.1.2.jar;C:\Users\执久\.m2\repository\com\alibaba\druid\1.1.10\druid-1.1.10.jar;C:\Users\执久\.m2\repository\junit\junit\4.11\junit-4.11.jar;C:\Users\执久\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\执久\.m2\repository\org\springframework\spring-test\5.0.5.RELEASE\spring-test-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\org\springframework\spring-core\5.0.5.RELEASE\spring-core-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\org\springframework\spring-jcl\5.0.5.RELEASE\spring-jcl-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\org\springframework\spring-context\5.0.5.RELEASE\spring-context-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\org\springframework\spring-aop\5.0.5.RELEASE\spring-aop-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\org\springframework\spring-beans\5.0.5.RELEASE\spring-beans-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\org\springframework\spring-expression\5.0.5.RELEASE\spring-expression-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\org\springframework\spring-web\5.0.5.RELEASE\spring-web-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\org\springframework\spring-webmvc\5.0.5.RELEASE\spring-webmvc-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.9.0\jackson-core-2.9.0.jar;C:\Users\执久\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.9.0\jackson-databind-2.9.0.jar;C:\Users\执久\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;C:\Users\执久\.m2\repository\org\springframework\spring-jdbc\5.0.5.RELEASE\spring-jdbc-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\org\springframework\spring-tx\5.0.5.RELEASE\spring-tx-5.0.5.RELEASE.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 test.JdbcTemplateCRUDTest,testUpdate

java.lang.ExceptionInInitializerError
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
	at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
	at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
	at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
	at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:31)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: java.lang.IllegalStateException: SpringJUnit4ClassRunner requires JUnit 4.12 or higher.
	at org.springframework.util.Assert.state(Assert.java:73)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<clinit>(SpringJUnit4ClassRunner.java:104)
	... 15 more


进程已结束,退出代码 -1


 将其改为Junit4.12就可以正确执行了


运行结果

 运行结果

编辑

 查询单个

  @Test
    public void testQueryOne(){
        Account account = jdbcTemplate.queryForObject("select *from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "ZH");
        System.out.println(account);
    }

 运行结果

编辑

聚合查询

 @Test
    public void testQueryCount(){
//查询数据条数
        Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
        System.out.println(count);
    }

编辑

 是一个实体就new一个对象,但是一个简单的数据,则创建一个普通的数据

 知识要点总结

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

2创建数据库表和实体

3创建JdbcTemplate对象
    JdbcTemplate jdbcTemplate=new JdbcTemplate(); 
    jdbcTemplate.setDatasource(datasource);

4执行数据库操作
 更新操作:
    jdbcTemplate.update(sql,params)

查询操作:
    jdbcTemplate.query(sql,Mapper,params)
    jdbcTemplate.queryForobject(sql,Mapper,params)


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。