• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

纯手写springboot项目(详细)

java 来源:WeirdoLong 19次浏览

导语:不会用eclipse创建springboot项目?也不会用IDEA创建springboot项目?没关系,会创建文件夹和文本文档就行。

1、springboot项目目录结构

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": "xiaom@163.com",
    "USER_ID": 1,
    "USER_NAME": "小明"
}

结束语:在网上找过一些springboot项目的创建过程,但是感觉麻烦,这里的一个简约版springboot项目算是对自己的一个小总结吧,如果对您也有帮助,那我很荣幸。


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)