1. 程式人生 > >IntelliJ IDEA+springboot+jdbctemplet+easyui+maven+oracle搭建簡易開發框架(一)

IntelliJ IDEA+springboot+jdbctemplet+easyui+maven+oracle搭建簡易開發框架(一)

前言:

  這兩天為了鞏固easyui的各個控制元件用法,搭建了一個簡易的框架用於開發,大家可以用來參考,如果發現文章中有哪些不正確不合理的地方,也請各位不吝賜教,感激不盡。文章最下面有原始碼,可以用於參考。整個框架共分三層controller service serviceImpl,並且包括easyui的整合。

 

框架的目錄:

  

 

 

一:建立專案

 

開啟IntelliJ IDEA軟體,點選File-New-Project建立專案

 選中Spring Initializr 點選next

 

 

 

填寫專案名 點選finish  注意:剛剛建立好的專案可能會自動引包,這裡可以暫時暫停,因為我們後面是要修改pom.xml檔案的內容的。

建立之後的專案路徑如下:

 

 

 

二:修改配置,部分程式碼

 

點選File-setting 搜尋maven

 修改成功之後,修改下三個檔案的內容,之後重新引包 三個檔案分別是pom.xml  (這個檔名稱是跟你建立專案時候的名稱對應生成的)Application.properties

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.14.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->

    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <packaging>war</packaging>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!--&lt;!&ndash;伺服器&ndash;&gt;-->
<!--            <scope>provided</scope>-->
            <!--本地-->
                <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0</version>
            <!--<version>12.1.0</version>-->
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>1.5.14.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>
pom.xml

 啟動類TmApplication

package com.example.tm;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class TmApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(TmApplication.class);
    }

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

Application.properties  注意:資料庫連線地址賬號和密碼都改成自己的就可以了

server.port=8080
server.application.name=demo

spring.datasource.url=jdbc:oracle:thin:@system:1521/orcl
spring.datasource.username=ORCL
spring.datasource.password=orcl
spring.datasource.driver-class-oracle.jdbc.driver.OracleDriver

spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
#熱部署檔案,頁面不產生快取,及時更新
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
Application.properties

 

 

三:引入easyui

 

在static資料夾下先建立一個檔案easyui

 建立之後可以修改下顯示的格式,有的人顯示是下面這種‘點’連線的,我們改成樹的會對結構看的更清楚

 修改成樹狀結構的方法。

 

從網上下載easyui的包

 

 

 

 

 

 

將解壓之後的內容直接複製到static資料夾下

 這裡因為test檔案我們暫時不用,所以可以先遮蔽掉,不遮蔽啟動的時候可能會報錯。

 

 

 

四:後臺程式碼

 

參考此目錄進行建立

進入頁面暫時只需要controller層,這裡先將整個框架寫好。要注意的是後臺類,方法上的註解不要寫錯。由於暫時沒有與後臺互動,所以資料庫不需要建立表。但是上面配置檔案中,還是需要指定資料庫的地址賬號密碼,否則啟動專案會報錯

package com.example.tm.report.controller;

import com.example.tm.report.service.reportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/report")
public class reportController {

    @Autowired
    private reportService reportservice;

    /**
     * 功能:報表
     * 建立人:tm
     * 時間:2019-12-28
     */
    @GetMapping(value = "/report")
    public String report(){
        return "/report/report";
    }

}
reportController

 reportService

package com.example.tm.report.service;

import org.springframework.stereotype.Service;

@Service("reportService")
public interface reportService {

}
reportService

 ReportServiceImpl

package com.example.tm.report.service.Impl;

import com.example.tm.report.service.reportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service("reportService")
public class ReportServiceImpl implements reportService {

    @Autowired
    private JdbcTemplate jdbcTemplate;



}
ReportServiceImpl

 

 

五:前臺程式碼

 

參考此目錄進行建立

注意:需要確認的幾個地方

  1. link和script標籤指定的easyui相關路徑地址名稱一定要正確

  2. 在js裡如果要寫js程式碼 要寫在  /*<![CDATA[*/      /*]]>*/之內

 

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>報表</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="../easyui/themes/default/easyui.css"/>
    <link rel="stylesheet" type="text/css" href="../easyui/themes/icon.css"/>
    <script type="text/javascript" src="../easyui/jquery.min.js"></script>
    <script type="text/javascript" src="../easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../easyui/datagrid-export.js"></script>
    <script type="text/javascript" src="../easyui/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript">
        /*<![CDATA[*/

        /*]]>*/
    </script>
</head>
<body>
<div class="easyui-layout" style="width:100%;height:1000px;" fit="true">
    <div id="tb" style="padding:3px">
        <span>開始時間</span>
        <input type="text" class="easyui-datebox" id="starttime"/>
        <span>結束時間</span>
        <input type="text" class="easyui-datebox" id="endtime"/>
        <span>所在班組</span>
        <select id = "ProjectTree" class="easyui-combotree" url="getClassGroupTree" name="city" style="width:156px;"/>
        <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="doSearch()">查詢</a>
        <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="toExcel()">匯出</a>
    </div>
</div>
</body>
</html>
report.html

 

 

六:啟動專案

 

啟動專案分為兩種,正常啟動和debug模式啟動

 如果啟動之後如下圖所示沒有報錯,瀏覽器中直接輸入  http://localhost:8080/report/report 就可以了(8080是application.properties檔案中配置的,可以自行修改)

顯示效果:

 

總結:此框架沒有太深的技術,適用於剛剛接觸springboot和開始學習搭框架的小夥伴們,接下來的文章裡我會依據easyui為基礎來完善各種easyui控制元件的用法,感興趣的小夥伴可以檢視我的部落格springboot分類中的內容,如果文章有那些不合理不正確的內容,也請各位大神不吝賜教,小編感激不盡。

 

持續更新!!!