Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
本文使用的开发工具为Idea2017、tomcat8.0、Java1.8、mysql5.6、maven3.5.3
源码:https://github.com/gdhzlee/springbootssmtest
一、新建Java web项目
(一)通过maven模板搭建Java web项目。
(二)构建Java web项目架构
1、通过maven模板新建好项目后,我们发现当前的项目架构还不完整。
2、需要通过手动添加一些必要的package。
- 注意:每个包的颜色和标志是不同的。
- 例如,java包存放的是源码,类型为Source root;resource存放的是资源,类型为Resource root
- 可以通过对包右键,Mark Directory As,进行设置。
3、Mark Directory As
这个配置,主要是为了部门告诉tomcat那些是源码目录,那些是资源。
一、Source roots (or source folders)
通过这个类指定一个文件夹,你告诉IntelliJ IDEA,这个文件夹及其子文件夹中包含的源代码,可以编译为构建过程的一部分。
2. Test source roots (or test source folders; shown as rootTest)
这些根类似于源根,但用于用于测试的代码(例如用于单元测试)。测试源文件夹允许您将与测试相关的代码与生产代码分开。
通常,源和测试源的编译结果被放置在不同的文件夹中。
3. Resource roots
用于应用程序中的资源文件(图像、各种配置XML和属性文件等)。
在构建过程中,资源文件夹的所有内容都复制到输出文件夹中,如下所示。
类似于源,您可以指定生成资源。您还可以指定输出文件夹中的文件夹,您的资源应该复制到。
4. Test resource roots
(或测试资源文件夹;如roottestresourceij;只有在java模块)是资源文件与您的测试源有关。在所有其他方面,这些文件夹类似于资源文件夹。
(三)通过maven导入必要的jar包
通过maven公库去获取的jar包,现在我们暂时只需要以下jar包。
- 注意:这里使用的是继承了 spring-boot-starter-parent 来获取合理的配置,这样在dependencies里spring-boot-starter-web可以不用填写version信息,会从spring-boot-dependencies里得到继承。
- 当然,也可以不使用这种方法。可以通过dependencyManager来管理spring-boot-starter-parent 依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
(四)配置运行环境
1、配置tomcat
①点击菜单栏 Run - > Run Configurations 就可以看到以下界面
②添加Tomcat Server,如果之前没有配置tomcat,则需要Configure tomcat的安装路径。设置访问端口为8080
2、配置部署
- ①点击Deployment
② 选择部署方式
③填写项目部署Application context
war模式:将WEB工程以包的形式上传到服务器 ;
war exploded模式:将WEB工程以当前文件夹的位置关系上传到服务器;
(1)war模式这种可以称之为是发布模式,看名字也知道,这是先打成war包,再发布;
(2)war exploded模式是直接把文件夹、jsp页面 、classes等等移到Tomcat 部署文件夹里面,进行加载部署。因此这种方式支持热部署,一般在开发的时候也是用这种方式。
(3)在平时开发的时候,使用热部署的话,应该对Tomcat进行相应的设置,这样的话修改的jsp界面什么的东西才可以及时的显示出来。
二、设计数据库和连接数据库
(一)设计数据库,添加一些数据
1、在这里设计一个用户信息表。
2、添加数据
(二)springboot配置mybatis
在springboot配置文件application.yml, 配置数据源和映射文件路径。
(三)编写实体类
package org.springboot.ssm.test.entity;
import java.sql.Timestamp;
/**
* 用户基本信息
*
*/
public class UserInformation {
private String userId;
private String userName;
private String number;
private String email;
private String sex;
private String organization;
private String department;
private String position;
private String role;
private String status;
private String note;
private String avatarUrl;
private Timestamp createTime;
private Timestamp updateTime;
//getter/setter
三、编写代码
到这里已经配置完成了,代码也很简单,典型的MVC代码。
(一)Dao层代码
1、dao接口
package org.springboot.ssm.test.dao;
import org.apache.ibatis.annotations.Mapper;
import org.springboot.ssm.test.entity.UserInformation;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserInformationDao {
List<UserInformation> selectAll();
}
2、映射文件
<?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="org.springboot.ssm.test.dao.UserInformationDao">
<resultMap id="AllColumnMap" type="org.springboot.ssm.test.entity.UserInformation">
<result column="user_id" property="userId"/>
<result column="user_name" property="userName"/>
</resultMap>
<select id="selectAll" resultMap="AllColumnMap">
SELECT * FROM user_information
</select>
</mapper>
(二)Service层代码
1、service层接口
package org.springboot.ssm.test.service;
public interface IUserInformationService {
Object selectAll();
}
2、service层实现类
package org.springboot.ssm.test.service.impl;
import org.springboot.ssm.test.dao.UserInformationDao;
import org.springboot.ssm.test.service.IUserInformationService;
import org.springframework.beans.factory.annotation.Autowired;
public class UserInformationService implements IUserInformationService {
@Autowired
UserInformationDao userInformationDao;
@Override
public Object selectAll() {
return userInformationDao.selectAll();
}
}
(三)controller层代码
package org.springboot.ssm.test.controller;
import org.springboot.ssm.test.service.IUserInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class UserInformationController {
@Autowired
IUserInformationService userInformationService;
@RequestMapping("/users")
@ResponseBody
public Object selectAll(){
return userInformationService.selectAll();
}
}
(四)配置快速启动类
package org.springboot.ssm.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer{
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
}
四、运行
运行tomcat,访问接口就可以看到从数据库中获得的数据啦。
可以看到的是,使用springboot来搭建一个完整项目框架是很简单的,几乎是0配置。
来源:CSDN
作者:沈君
链接:https://blog.csdn.net/qq1131410679/article/details/79958441