現有前面遷移到Spring-boot


  做為一個剛轉java不到兩個月的.net 程序員,第一個拿到的任務就是把一個以前的eclipse項目遷移到spring-boot,我的內心是崩潰的。eclipse我都還用的不是很熟,spring也才是限於能看懂,對於那一大堆的配置文件更是,更是一臉懵逼,好吧,工作還是得做。記錄下整個過程中踩到的各式各樣的坑。

  第一步加了maven做項目構建,唉,都是現學現賣。在轉換過程中出現了各種錯誤。

    a).net.sf.json-lib引用報錯,解決方案是指定JDK版本

    

    b) spring配置的 xml文件一直報錯:發現時xml中寫的spring版本和實際maven安裝的不一致,maven暗轉的是spring-4.39,xml中寫的是4.0,

    

    c)"Cannot change version of project facet Dynamic web module to 2.5",這個主要是兩個地方版本不一致導致的,一個是web.xml

      

    另一個地方是主目錄下.setting 文件夾下的“org.eclipse.wst.common.project.facet.core.xml”文件里面的jst.web的版本,這兩個要保持一致

      

  中間還碰到過一個問題,忘了是啥,解決方案是添加spring-tx的依賴,怕又碰到

 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.3.7.RELEASE</version>
</dependency>  

 

下面是轉springboot的基本步驟和踩到的坑

  1.修改配置文件,增加springboot的配置

  添加繼承的parent

 
<!--繼承spring boot的parent-->
<
parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent>
  <!--添加spring boot  maven 插件用做項目編譯-->
   <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><!--指定啟動文件-->
                </manifest>
            </archive>
        </configuration>
    </plugin>

  添加依賴

    <dependency>
            <groupId>org.springframework.boot</groupId><!--web項目需要的依賴-->
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
            <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId><!--單元測試需要的依賴-->
            <scope>test</scope>
        </dependency>

  2.編寫Application,完成上述這些操作基本上就可以講項目作為一個springboot項目進項啟動了,當然了可能會報一大堆的錯誤。

@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication
public class Application {
      
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

 整合mybatis到springboot

  1.添加依賴  

<!--springboot mybatis的依賴項-->
<dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.0.0</version>
</dependency>

  

<!--jdbc的驅動,這個是必須有的,不然會拋驅動不能加載的異--> 
<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
</dependency>

  2.定義datasource ,sqlSessionFactory,這個需要定義到剛剛的Application中,修改Applicaiton文件  ,配置好數據庫,項目應該是能夠正常啟動了

@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication
public class Application {
  
    
  //DataSource配置
    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    @Primary
    public DataSource dataSource() {
        //DataSource dataSource = new DataSource();
        return DataSourceBuilder.create().type(com.mchange.v2.c3p0.ComboPooledDataSource.class).build();
        //com.mchange.v2.c3p0.ComboPooledDataSource
        //return new org.apache.tomcat.jdbc.pool.DataSource();
    }
 
    //提供SqlSeesion
    @Bean
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
 
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
 
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
 
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/com/seaskylight/kaow/dao/*.xml"));
 
        return sqlSessionFactoryBean.getObject();
    }
    
    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

  這里的線程池用的是c3p0,需要添加對應的依賴,並且在springboot的配置文件中配置好參數  

<dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
</dependency>

  配置文件  

spring.datasource.jdbcUrl=jdbc:mysql://localhost:3306/zxtk?characterEncoding=UTF-8&amp;characterSetResults=UTF-8&amp;zeroDateTimeBehavior=convertToNull
spring.datasource.user=user
spring.datasource.password=password
spring.datasource.driverClass=com.mysql.jdbc.Driver
spring.datasource.initialPoolSize=5
spring.datasource.minPoolSize=5
spring.datasource.maxPoolSize=50
spring.datasource.maxIdleTime=600
spring.datasource.acquireIncrement=5
spring.datasource.acquireRetryDelay=1000
spring.datasource.acquireRetryAttempts=30
spring.datasource.checkoutTimeout=2000
spring.datasource.maxStatements=50
spring.datasource.idleConnectionTestPeriod=60
spring.datasource.preferredTestQuery=SELECT SYSDATE()
spring.datasource.numHelperThreads=3

 

  因為這個項目是個web項目,所以有一些靜態資源。springboot默認的靜態資源是放在/resources,/resources/static,等默認文件夾下的(還有幾個忘記了),這里我是在resources下面建了個static文件夾,然后再將靜態資源移動到改文件夾下。這樣就能進行靜態文件的訪問。

  

 

 

 

      

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM