1. 程式人生 > >使用idea maven開發spring boot 分布式開發入門

使用idea maven開發spring boot 分布式開發入門

desc scan 2.6 porting vid onf response username snapshot

1:使用idea創建一個maven工程 bos2

技術分享圖片

2:刪除bos2的src 文件夾,向pom.xml文件 中添加版本號

<packaging>pom</packaging>
<?xml version="1.0" encoding="UTF-8"?>
<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.li</groupId> <artifactId>bos2</artifactId> <version>1.0-SNAPSHOT</version> <modules> <module>crm_domain</module> <module>crm_management</module>
</modules> <packaging>pom</packaging> <name>bos2.0</name> <description>bos v2.0綜合平臺</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId
> <version>1.5.9.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-data-jpa.version>1.9.0.RELEASE</spring-data-jpa.version> <elasticsearch.version>2.4.6</elasticsearch.version> <jackson.version>2.8.5</jackson.version> <boot-starter-jaxrs.version>3.2.0</boot-starter-jaxrs.version> <jna.version>3.0.9</jna.version> <ojdbc6.version>11.2.0.1.0</ojdbc6.version> <shiro.version>1.4.0</shiro.version> <pinyin4j.version>2.5.0</pinyin4j.version> <poi.version>3.11</poi.version> <httpclient.version>4.5.3</httpclient.version> <fastjson.version>1.2.8</fastjson.version> <mail.version>1.4.7</mail.version> <commons-lang.version>2.6</commons-lang.version> <commons-io.version>1.3.2</commons-io.version> <itext.version>5.2.0</itext.version> <quartz.version>2.2.1</quartz.version> <mysql.version>5.1.35</mysql.version> </properties> </project>

2:根目錄右鍵 添加模塊,選擇maven工程。創建maven子模塊 crm_domain

子模塊pom.xml添加

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>bos2</artifactId>
        <groupId>com.li</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>crm_domain</artifactId>
    <name>crm_domain</name>
    <description>客戶關系系統實體類</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

</project>

3:創建對象

package top.kylewang.crm.domain;


import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Date;

/**
 * @description:客戶信息表
 * 
 */
@Entity
@Table(name = "T_CUSTOMER")
@XmlRootElement(name = "customer")
public class Customer {
    @Id
    @GeneratedValue()
    @Column(name = "C_ID")
    private Integer id; // 主鍵id
    @Column(name = "C_USERNAME")
    private String username; // 用戶名
    @Column(name = "C_PASSWORD")
    private String password; // 密碼
    @Column(name = "C_TYPE")
    private Integer type; // 類型 設置1 綁定郵箱
    @Column(name = "C_BRITHDAY")
    @Temporal(TemporalType.DATE)
    private Date birthday; // 生日
    @Column(name = "C_SEX")
    private Integer sex; // 性別 1男 2女
    @Column(name = "C_TELEPHONE")
    private String telephone; // 手機
    @Column(name = "C_COMPANY")
    private String company; // 公司
    @Column(name = "C_DEPARTMENT")
    private String department; // 部門
    @Column(name = "C_POSITION")
    private String position; // 職位
    @Column(name = "C_ADDRESS")
    private String address; // 地址
    @Column(name = "C_MOBILEPHONE")
    private String mobilePhone; // 座機
    @Column(name = "C_EMAIL")
    private String email; // 郵箱
    @Column(name = "C_Fixed_AREA_ID")
    private String fixedAreaId; // 定區編碼

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getMobilePhone() {
        return mobilePhone;
    }

    public void setMobilePhone(String mobilePhone) {
        this.mobilePhone = mobilePhone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFixedAreaId() {
        return fixedAreaId;
    }

    public void setFixedAreaId(String fixedAreaId) {
        this.fixedAreaId = fixedAreaId;
    }

    @Override
    public String toString() {
        return "Customer [id=" + id + ", username=" + username + ", password=" + password + ", type=" + type
                + ", birthday=" + birthday + ", sex=" + sex + ", telephone=" + telephone + ", company=" + company
                + ", department=" + department + ", position=" + position + ", address=" + address + ", mobilePhone="
                + mobilePhone + ", email=" + email + ", fixedAreaId=" + fixedAreaId + "]";
    }

}

3:右鍵根工程創建一個子模塊crm_management,使其依賴 crm_domain.向pom.xml中添加

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>bos2</artifactId>
        <groupId>com.li</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>crm_management</artifactId>
    <packaging>jar</packaging>
    <name>crm_management</name>
    <description>客戶關系管理系統</description>

    <dependencies>
        <dependency>
            <groupId>com.li</groupId>
            <artifactId>crm_domain</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
            <version>${boot-starter-jaxrs.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--jaxrs轉json工具-->
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <!--commons工具包-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>${commons-lang.version}</version>
        </dependency>
     
        <!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

5:在resources目錄下添加application.yml,對子模塊進行配置

server:
  port: 9002   #配置設置服務的端口號
  context-path: /crm   #路徑
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/crm
    username: root
    password: 136735
  jpa:
    show-sql: true   #jpa
  jackson:
    default-property-inclusion: non_null
  devtools:
    restart:
      enabled: true
cxf:
  path: /services
  servlet.init:
    service-list-path: /info
  jaxrs:
    component-scan: true

6:創建controller和applicationMain主函數等

package top.kylewang.crm;

import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

/**
 * @author Kyle.Wang
 * 2018/1/15 0015 11:59
 */

@SpringBootApplication
public class CrmManagementApplication {

    public static void main(String[] args) {
        SpringApplication.run(CrmManagementApplication.class, args);
    }

    @Bean
    public JacksonJaxbJsonProvider jacksonJaxbJsonProvider() {
        return new JacksonJaxbJsonProvider();
    }
}
package top.kylewang.crm.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Test {
    @RequestMapping("hello")
    @ResponseBody
    public String getString(){
        return"hello";
    }
}

7:運行springboot主函數, 訪問 http://localhost:9002/crm/hello

結果為hello.

系統結構圖

技術分享圖片

使用idea maven開發spring boot 分布式開發入門