Spring Boot starter--jdbctemplate usage and related issues resolution

Source: Internet
Author: User
Tags assert

1, in the Pom.xml file to introduce the corresponding dependency

    <!--MySQL Dependency -    <Dependency>        <groupId>Mysql</groupId>        <Artifactid>Mysql-connector-java</Artifactid>        <version>5.1.42</version>    </Dependency>        <!--jdbc Dependency -    <Dependency>        <groupId>Org.springframework.boot</groupId>        <Artifactid>Spring-boot-starter-jdbc</Artifactid>    </Dependency>

2. Create Grade Class

public class Grade {    private int id;    Private String Gradenm;    private int teacherid;        public int getId () {        return ID;    }    public void setId (int id) {        this.id = ID;    }    Public String Getgradenm () {        return gradenm;    }    public void Setgradenm (String gradenm) {        this.gradenm = gradenm;    }    public int Getteacherid () {        return teacherid;    }    public void Setteacherid (int teacherid) {        This.teacherid = Teacherid;    }}
View Code

3. Create Gradedao

import java.sql.resultset;import Java.sql.sqlexception;import Javax.annotation.resource;import Org.springframework.jdbc.core.beanpropertyrowmapper;import Org.springframework.jdbc.core.jdbctemplate;import Org.springframework.jdbc.core.rowmapper;import Org.springframework.stereotype.repository;import        Com.wyl.bean.Grade, @Repositorypublic class gradedao{@Resource private JdbcTemplate jdbctemplate;        Public Grade Findbygradenm (string gradenm) {string-sql = "SELECT * from Grade where grade_nm= '" + gradenm + "'"; rowmapper   <grade> rm = new Beanpropertyrowmapper<>   (grade.class);    Return Jdbctemplate.queryforobject (SQL,RM); }/*    
* You must manually encapsulate the grade class yourself, because Springboot encapsulates a column in the database as an object by default
* / class Graderowmapper implements RowMapper <Grade> {@Override public Grade Maprow (ResultSet rs, int rowNum) throws SQLException {//TODO auto-generated method Stub Grade Grade = new Grade (); Grade.setgradenm (rs.getstring ("grade_nm")); Grade.setid (Rs.getint ("id")); Grade.setteacherid (Rs.getint ("teacher_id")); return grade; } }}

4. Create Gradeservice

ImportJavax.annotation.Resource;ImportOrg.springframework.stereotype.Service;ImportCom.wyl.bean.Grade;ImportCom.wyl.dao.GradeDAO;/*** Gradeservice class, call method in Gradedao **/@Service Public classGradeservice {@ResourcePrivateGradedao Gradedao;  PublicGrade fingbygradenm (String gradenm) {returnGradedao.findbygradenm (GRADENM); }}

5. Create Gradecontroller

ImportJavax.annotation.Resource;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController;ImportCom.wyl.bean.Grade;ImportCom.wyl.service.GradeService; @RestController Public classJpacontroller {@ResourcePrivateGradeservice Gradeservice; @RequestMapping ("/finfbygradenm")     PublicGrade finfbygradenm (String gradenm) {System.out.println ("Finfbygradenm"); return Gradeservice.fingbygradenm (GRADENM); }}

6. Testing

Org.springframework.beans.factory.BeanCreationException:Error creating bean with Name ' Org.springframework.boot.autoconfigure.liquibase.liquibaseautoconfiguration$liquibaseconfiguration ': Invocation of Init method failed; Nested exception is java.lang.IllegalStateException:Cannot find changelog location:class path resource [db/changelog/db . Changelog-master.yaml] (please add changelog or check your liquibase configuration) at Org.springframework.beans.facto Ry.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization ( initdestroyannotationbeanpostprocessor.java:137) ~[spring-beans-4.3.8.release.jar:4.3.8.release] at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization (abstractautowirecapablebeanfactory.java:409) ~[spring-beans-4.3.8.release.jar:4.3.8.release] at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean ( Abstractautowirecapablebeanfactory. java:1620) ~[spring-beans-4.3.8.release.jar:4.3.8.release] at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean ( abstractautowirecapablebeanfactory.java:555) ~[spring-beans-4.3.8.release.jar:4.3.8.release] at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean ( abstractautowirecapablebeanfactory.java:483) ~[spring-beans-4.3.8.release.jar:4.3.8.release] at Org.springframework.beans.factory.support.abstractbeanfactory$1.getobject (abstractbeanfactory.java:306) ~[ Spring-beans-4.3.8.release.jar:4.3.8.release] At Org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton ( defaultsingletonbeanregistry.java:230) ~[spring-beans-4.3.8.release.jar:4.3.8.release] at Org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (abstractbeanfactory.java:302) ~[ Spring-beans-4.3.8.release.jar:4.3.8.release] At Org.springframework.beans.factory.support.AbstractBeanFactory.getBeAn (abstractbeanfactory.java:197) ~[spring-beans-4.3.8.release.jar:4.3.8.release] at Org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons ( defaultlistablebeanfactory.java:761) ~[spring-beans-4.3.8.release.jar:4.3.8.release] at Org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization ( abstractapplicationcontext.java:866) ~[spring-context-4.3.8.release.jar:4.3.8.release] at Org.springframework.context.support.AbstractApplicationContext.refresh (abstractapplicationcontext.java:542) ~[ Spring-context-4.3.8.release.jar:4.3.8.release] At Org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh ( embeddedwebapplicationcontext.java:122) ~[spring-boot-1.5.3.release.jar:1.5.3.release] at Org.springframework.boot.SpringApplication.refresh (springapplication.java:737) [Spring-boot-1.5.3.release.jar : 1.5.3.RELEASE] at Org.springframework.boot.SpringApplication.refreshContext (springapplication.java:370) [Spring-boot-1.5.3.release.jar:1.5.3.release] at Org.springframework.boot.SpringApplication.run ( springapplication.java:314) [Spring-boot-1.5.3.release.jar:1.5.3.release] at Org.springframework.boot.SpringApplication.run (springapplication.java:1162) [Spring-boot-1.5.3.release.jar : 1.5.3.RELEASE] at Org.springframework.boot.SpringApplication.run (springapplication.java:1151) [ Spring-boot-1.5.3.release.jar:1.5.3.release] at Com.wyl.App.main (app.java:16) [Classes/:na] at Sun.reflect.NativeMe THODACCESSORIMPL.INVOKE0 (Native Method) ~[na:1.8.0_60] at Sun.reflect.NativeMethodAccessorImpl.invoke ( nativemethodaccessorimpl.java:62) ~[na:1.8.0_60] at Sun.reflect.DelegatingMethodAccessorImpl.invoke ( delegatingmethodaccessorimpl.java:43) ~[na:1.8.0_60] at Java.lang.reflect.Method.invoke (method.java:497) ~[na : 1.8.0_60] at Org.springframework.boot.devtools.restart.RestartLauncher.run (restartlauncher.java:49) [ Spring-boot-devtools-1.5.3.release.jar:1.5.3.release]cauSed by:java.lang.IllegalStateException:  cannot find changelog location:class path resource [db/changelog/ DB.CHANGELOG-MASTER.YAML] (please add changelog or check your liquibase configuration)     At Org.springframework.util.Assert.state (assert.java:70) ~[spring-core-4.3.8.release.jar:4.3.8.release "at ORG.SPR ingframework.boot.autoconfigure.liquibase.liquibaseautoconfiguration$ Liquibaseconfiguration.checkchangelogexists (liquibaseautoconfiguration.java:92) ~[ Spring-boot-autoconfigure-1.5.3.release.jar:1.5.3.release] at Sun.reflect.NativeMethodAccessorImpl.invoke0 ( Native Method) ~[na:1.8.0_60] at Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:62) ~[na : 1.8.0_60] at Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43) ~[na:1.8.0_60 ] at Java.lang.reflect.Method.invoke (method.java:497) ~[na:1.8.0_60 "at Org.springframework.beans.factory.annotatio N.initdestroyannotationbeanpostprocessor$lifecycleelement.invoke (Initdestroyannotationbeanpostprocessor.java : 366) ~[spring-beans-4.3.8.release.jar:4.3.8.release] at Org.springframework.beans.factory.annotation.initdestroyannotationbeanpostprocessor$lifecyclemeTadata.invokeinitmethods (initdestroyannotationbeanpostprocessor.java:311) ~[spring-beans-4.3.8.release.jar : 4.3.8.RELEASE] At Org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization (initdestroyannotationbeanpostprocessor.java:134) ~[spring-beans-4.3.8.release.jar:4.3.8.release] ... Common frames omitted

This test occurs because Db.changelog-master.yaml files are required in liquibase .

Workaround:

1. Add the following code to the Application.properties:

Liquibase.change-log=classpath:/liquibase/db.changelog.xml

  2. Create the /liquibase/db.changelog.xml directory under the Src/main/resource package, and the contents of Db.changelog.xml are as follows:

<?XML version= "1.0" encoding= "UTF-8"?><Databasechangelogxmlns= "http://www.liquibase.org/xml/ns/dbchangelog/1.9"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://www.liquibase.org/xml/ns/dbchangelog/1.9 http://www.liquibase.org/xml/ns/dbchangelog/dbchange Log-1.9.xsd ">    <ChangeSetID= "1"author= "Jim">        <createtableTableName= "UserAccount">            <columnname= "userid"type= "bigint">                <ConstraintsPrimaryKey= "true" />            </column>            <columnname= "Accountstate"type= "varchar (255)" />            <columnname= "Expireddate"type= "datetime" />            <columnname= "username"type= "varchar (255)" />        </createtable>        <AdduniqueconstraintTableName= "UserAccount"ColumnNames= "username"ConstraintName= "username" />    </ChangeSet>    <ChangeSetID= "2"author= "Jim">        <AddColumnTableName= "UserAccount">            <columnname= "Gender"type= "varchar (1)"value= "M">                <ConstraintsNullable= "false" />            </column>        </AddColumn>    </ChangeSet></Databasechangelog>   

After the test results are resolved:

  

Spring Boot starter--jdbctemplate usage and related issues resolution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.