JdbcTemplate查询操作

目录

查询操作

查询所有

 查询单个

聚合查询

 知识要点总结


查询操作

查询所有

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

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

数据库中

JdbcTemplate查询操作_第1张图片

 Junit4.11版本导致错误

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

JdbcTemplate查询操作_第2张图片

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.(SpringJUnit4ClassRunner.java:104)
	... 15 more


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

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

运行结果

 运行结果

JdbcTemplate查询操作_第3张图片

 查询单个

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

 运行结果

JdbcTemplate查询操作_第4张图片

聚合查询

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

JdbcTemplate查询操作_第5张图片

 是一个实体就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)

你可能感兴趣的:(SSM框架,数据库,java,mybatis)