純手寫springboot專案(詳細)
阿新 • • 發佈:2018-11-12
導語:不會用eclipse建立springboot專案?也不會用IDEA建立springboot專案?沒關係,會建立資料夾和文字文件就行。
1、springboot專案目錄結構
注:矩形為資料夾,橢圓為檔案。
2、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"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <!-- Spring Boot 啟動父依賴 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <mybatis-spring-boot>1.2.0</mybatis-spring-boot> </properties> <dependencies> <!-- Spring Boot Web 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Test 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Spring Boot Mybatis 依賴 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis-spring-boot}</version> </dependency> <!-- Spring Boot LOG 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <!-- Spring Boot AOP 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!-- Mysql驅動包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 資料庫連線池 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- optional=true,依賴不會傳遞,該專案依賴devtools;之後依賴myboot專案的專案如果想要使用devtools,需要重新引入 --> </dependency> <!--apache公共輔助包括檔案上傳下載相關等五個) --> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.31</version> </dependency> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.11.0</version> </dependency> <!-- excel匯入 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>RELEASE</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build> </project>
3、application.properties檔案
spring.profiles.active=dev
## Mybatis
mybatis.mapperLocations=classpath\:mapper/*.xml
mybatis.configuration.call-setters-on-nulls=true
4、application-dev.properties檔案
##埠配置 server.port =8095 ## 資料來源配置 spring.datasource.url=jdbc:mysql://localhost/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true spring.datasource.username=root spring.datasource.password=root spring.datasource.driverClassName=com.mysql.jdbc.Driver
5、Application.java檔案
package com.example; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; /** * Spring Boot 應用啟動類 * */ // Spring Boot 應用的標識 @SpringBootApplication // mapper 介面類掃描包配置 @EnableTransactionManagement @EnableScheduling @EnableAspectJAutoProxy(proxyTargetClass = true) //開啟AspectJ代理,並將proxyTargetClass置為true,表示啟用cglib對Class也進行代理 @MapperScan("com.example.dao") public class Application extends SpringBootServletInitializer { public static void main(String[] args) { // 程式啟動入口 // 啟動嵌入式的 Tomcat 並初始化 Spring 環境及其各 Spring 元件 SpringApplication.run(Application.class, args); } }
6、UserController.java檔案
package com.example.controller;
import com.example.service.IServiceUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping(value = "/")
public class UserController {
private static Logger log = LoggerFactory.getLogger(UserController.class);
@Autowired
IServiceUser serviceUser;
public UserController() {
}
/**
* 查詢使用者
*
* @return Map
*/
@RequestMapping(value = "/User/select", method = { RequestMethod.POST, RequestMethod.GET })
public Map selectSysUser(@RequestParam Map<String, Object> mapParam) {
Map<String,Object> map =new HashMap<String, Object>();
try {
map = serviceUser.selectUser(mapParam);
if(map.size()>0){
map.put("data","success");
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}
7、IDaoUser.java檔案
package com.example.dao;
import java.util.Map;
public interface IDaoUser {
/**
* 查詢使用者
*/
public Map<String , Object> selectUser(Map<String, Object> mapParam);
}
8、IServiceUser.java檔案
package com.example.service;
import java.util.List;
import java.util.Map;
public interface IServiceUser {
/**
* 查詢系統使用者記錄
*/
public Map<String , Object> selectUser(Map<String, Object> mapParam);
}
9、ServiceUserImpl.java檔案
package com.example.service.impl;
import com.example.dao.IDaoUser;
import com.example.service.IServiceUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class ServiceUserImpl implements IServiceUser {
private Logger log = LoggerFactory.getLogger(this.getClass());
@Autowired
private IDaoUser iDao;
public ServiceUserImpl(){
}
/**
* 查詢使用者
*/
public Map<String , Object> selectUser(Map<String, Object> mapParam){
Map<String , Object> map=new HashMap<String , Object>();
try {
map= iDao.selectUser(mapParam);
} catch (Exception e) {
log.error(e.getMessage());
}
return map;
}
}
10、T_USER.XML檔案
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.dao.IDaoUser">
<!-- 使用者查詢 -->
<select id="selectUser" parameterType="map" resultType="HashMap">
SELECT *
FROM T_USER
</select>
</mapper>
10、建立T_USER表(資料庫--MySQL)
CREATE TABLE `test`.`Untitled` (
`USER_ID` int(11) NOT NULL AUTO_INCREMENT,
`USER_NAME` char(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`USER_PASSWORD` char(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`USER_EMAIL` char(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`USER_ID`) USING BTREE,
INDEX `IDX_NAME`(`USER_NAME`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
11、往表中新增一條資訊
12、把專案匯入eclipse或idea中,啟動專案,在谷歌瀏覽器或postmen位址列輸入:localhost:8095/User/select
{
"data": "success",
"USER_PASSWORD": "123",
"USER_EMAIL": "[email protected]",
"USER_ID": 1,
"USER_NAME": "小明"
}
結束語:在網上找過一些springboot專案的建立過程,但是感覺麻煩,這裡的一個簡約版springboot專案算是對自己的一個小總結吧,如果對您也有幫助,那我很榮幸。