Existing front migration to Spring-boot

Source: Internet
Author: User

As a. NET programmer who had just turned Java for less than two months, the first task was to move a previous eclipse project to Spring-boot, and my heart was broken. Eclipse I also use is not very familiar, spring is also limited to can understand, for that a lot of configuration file is more, is a face Meng force, OK, work or have to do. Record the various pits that have been stepped on during the whole process.

The first step is to add MAVEN to build the project, alas, are now learning to sell. A variety of errors occurred during the conversion process.

a). Net.sf.json-lib quoted error, solution is to specify JDK version

    

b) The XML file for spring configuration has been the error: the spring version written in XML was found to be inconsistent with the actual MAVEN installation, and MAVEN installation was written in Spring-4.39,xml of 4.0,

    

c) "Cannot change version of the project Facet Dynamic Web module to 2.5", which is mainly caused by inconsistencies in two local versions, one is Web. xml

      

Another place is the home directory under the setting folder under the "Org.eclipse.wst.common.project.facet.core.xml" file in the Jst.web version, the two to remain consistent

      

In the middle also encountered a problem, forget what is, the solution is to add spring-tx dependence, afraid of encountering

 <Dependency>            <groupId>Org.springframework</groupId>            <Artifactid>Spring-tx</Artifactid>            <version>4.3.7.RELEASE</version></Dependency>  

Here are the basic steps of the springboot and the pits to be trampled

1. Modify the configuration file to increase the configuration of Springboot

Add an inherited Parent

 
<!--inherit from spring boot parent-->
<Parent> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-parent</Artifactid> <version>1.5.2.RELEASE</version> </Parent>
<!--add a spring boot  maven plugin for project compilation--
<plugin>        <groupId>Org.springframework.boot</groupId>        <Artifactid>Spring-boot-maven-plugin</Artifactid>     </plugin>    <plugin>        <groupId>Org.apache.maven.plugins</groupId>        <Artifactid>Maven-jar-plugin</Artifactid>        <Configuration>            <Archive>                <Manifest>                    <Addclasspath>True</Addclasspath>                    <Classpathprefix></Classpathprefix>                    <MainClass>Com.seaskylight.kaow.Application</MainClass><!--Specify the startup file --                </Manifest>            </Archive>        </Configuration>    </plugin>

Add dependency

<Dependency>            <groupId>Org.springframework.boot</groupId><!--Web projects require dependency--            <Artifactid>Spring-boot-starter-web</Artifactid>        </Dependency>            <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-test</Artifactid><!--Unit tests need to be dependent--            <Scope>Test</Scope>        </Dependency>

2. Write application, complete these operations basically can say the project as a Springboot project input started, of course, may be reported a lot of errors.

@EnableAutoConfiguration @componentscan@springbootapplication  Public class Application {          publicstaticvoid  main (string[] args) {        Springapplication.run (Application. class , args);}    }

Integrate MyBatis to Springboot

1. Add dependencies  

<!--springboot dependencies for MyBatis -<Dependency>        <groupId>Org.mybatis.spring.boot</groupId>        <Artifactid>Mybatis-spring-boot-starter</Artifactid>        <version>1.0.0</version></Dependency>

  

<!--JDBC Driver, this is a must, otherwise it will throw the driver can not load the different - <Dependency>        <groupId>Mysql</groupId>        <Artifactid>Mysql-connector-java</Artifactid>        <version>5.1.6</version></Dependency>

2. Define DataSource, Sqlsessionfactory, this need to define to just application, modify the Applicaiton file, configure the database, the project should be able to start normally

@EnableAutoConfiguration @componentscan@springbootapplication Public classApplication {//DataSource Configuration@Bean @ConfigurationProperties (prefix= "Spring.datasource") @Primary PublicDataSource DataSource () {//DataSource DataSource = new DataSource ();        returnDatasourcebuilder.create (). Type (Com.mchange.v2.c3p0.ComboPooledDataSource.class). build (); //Com.mchange.v2.c3p0.ComboPooledDataSource//return new Org.apache.tomcat.jdbc.pool.DataSource ();    }     //provide sqlseesion@Bean PublicSqlsessionfactory Sqlsessionfactorybean ()throwsException {Sqlsessionfactorybean Sqlsessionfactorybean=NewSqlsessionfactorybean ();         Sqlsessionfactorybean.setdatasource (DataSource ()); Pathmatchingresourcepatternresolver Resolver=NewPathmatchingresourcepatternresolver (); Sqlsessionfactorybean.setmapperlocations (Resolver.getresources ("Classpath:/com/seaskylight/kaow/dao/*.xml")); returnSqlsessionfactorybean.getobject (); } @Bean PublicPlatformtransactionmanager TransactionManager () {return NewDatasourcetransactionmanager (DataSource ()); }     Public Static voidMain (string[] args) {springapplication.run (application.class, args); }}

The thread pool here uses C3P0, needs to add the corresponding dependencies, and configures the parameters in the Springboot configuration file.

<Dependency>      <groupId>C3p0</groupId>      <Artifactid>C3p0</Artifactid>      <version>0.9.1.2</version></Dependency>

Configuration file  

Spring.datasource.jdbcurl=jdbc:mysql://localhost:3306/zxtk?characterencoding=utf-8&amp; Charactersetresults=utf-8&amp; zerodatetimebehavior=converttonullspring.datasource.user=userspring.datasource.password= Passwordspring.datasource.driverclass=com.mysql.jdbc.driverspring.datasource.initialpoolsize= 5spring.datasource.minpoolsize=5spring.datasource.maxpoolsize=50spring.datasource.maxidletime= 600spring.datasource.acquireincrement=5spring.datasource.acquireretrydelay= 1000spring.datasource.acquireretryattempts=30spring.datasource.checkouttimeout= 2000spring.datasource.maxstatements=50spring.datasource.idleconnectiontestperiod= 60spring.datasource.preferredtestquery=select sysdate () spring.datasource.numhelperthreads=3

Because this project is a Web project, there are some static resources. Springboot default static resources are placed in the/resources,/resources/static, such as the default folder (and a few forgotten), here I built a static folder under Resources, Then move the static resources to the changed folder. This makes it possible to access the static files.

  

      

Existing front migration to Spring-boot

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.