1. 程式人生 > >myeclipse搭建spring boot+mybatis

myeclipse搭建spring boot+mybatis

本專案使用的環境:

  • 開發工具:myeclipse
  • springboot: 1.5.6
  • jdk:1.7
  • maven:3.3.9  

1.使用myeclipse建立一個maven專案。

ps:選擇jar java project,選擇war 為 web project

點選finish結束。如圖:

2.整合spring boot

將app放到com.guo下

package com.guo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.guo.dao")//將專案中對應的mapper類的路徑加進來就可以了
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

application.yml檔案放置:src/main/resoures下

server:
  port: 8080

spring:
    datasource:
        name: test
        url: jdbc:mysql://127.0.0.1:3306/rce
        username: root
        password: wangyun123
        # 使用druid資料來源
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        filters: stat
        maxActive: 20
        initialSize: 1
        maxWait: 60000
        minIdle: 1
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: select 'x'
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        maxOpenPreparedStatements: 20
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.guo.vo

#pagehelper分頁外掛
pagehelper:
    helperDialect: mysql
    reasonable: true
    supportMethodsArguments: true
    params: count=countSql

對映檔案:userMapper.xml放置:src/main/resoures/mapper下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.guo</groupId>
  <artifactId>springboot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <!-- 1、設定Spring boot的parent:引入以後在宣告其它dependency的時候就不需要version -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.6.RELEASE</version>
	</parent>
	<!-- 2、匯入Spring boot的web支援:spring官方解釋spring-boot-starter-web包含了
	         spring webmvc和tomcat等web開發的特性。 -->
    <dependencies>
	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
	    <groupId>org.mybatis.spring.boot</groupId>
	    <artifactId>mybatis-spring-boot-starter</artifactId>
	    <version>1.3.1</version>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<optional>true</optional>
	</dependency> 
	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
	 </dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-tomcat</artifactId>
       <scope>provided</scope>
   </dependency>
 
	<!-- mysql連線 -->    
     <dependency>    
         <groupId>mysql</groupId>    
         <artifactId>mysql-connector-java</artifactId>    
         <version>5.1.42</version>    
     </dependency> 
	<!-- mybatis逆向工程包 -->
    <dependency>  
        <groupId>org.mybatis.generator</groupId>  
        <artifactId>mybatis-generator-core</artifactId>  
        <version>1.3.5</version>  
    </dependency> 
    <!--json、基本包-->  
    <dependency>  
      <groupId>com.alibaba</groupId>  
      <artifactId>fastjson</artifactId>  
      <version>1.2.23</version>  
    </dependency>  
    <dependency>  
      <groupId>xml-apis</groupId>  
      <artifactId>xml-apis</artifactId>  
      <version>2.0.2</version>  
    </dependency>  
    <dependency>  
      <groupId>commons-net</groupId>  
      <artifactId>commons-net</artifactId>  
      <version>3.5</version>  
    </dependency>  
    <dependency>
	    <groupId>net.sf.json-lib</groupId>
	    <artifactId>json-lib</artifactId>
	    <version>2.4</version>
	    <classifier>jdk15</classifier><!--指定jdk版本-->  
	</dependency>
	       <!-- 分頁外掛 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.1.2</version>
        </dependency>
        <!-- alibaba的druid資料庫連線池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.0</version>
        </dependency>
     </dependencies>
	 <properties>
	    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	    <java.version>1.8</java.version>
     </properties>
	
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<!-- 3、新增Spring boot的外掛:如果我們要直接Main啟動spring,那麼以下plugin必須要新增,
			      否則是無法啟動的。如果使用maven的spring-boot:run的話就不需要此配置。 -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>