您的位置:首页 > 编程语言 > Java开发

Spring boot 入门系列(一):快速搭建一个简单web系统(简单的SSM框架)

2017-10-18 20:16 801 查看
       前言:本文所用jdk版本为:jdk1.8.0_111版本;Spring boot1.5.8版本。

一、快速搭建项目

        1.1. 使用Spring Initializr快速生产基础工程,具体步骤如下:

            1.1.1. Spring Initializr工具访问地址为:https://start.spring.io/ ;

            1.1.2.选择项目构建信息以及Spring boot版本,填写Group和Artifact的信息,具体信息如下图;

            1.1.3.点击Generate Project 按钮后,就会下载一个生成好的工程压缩包。



            1.1.4. 将压缩包解压后导入到IDE中,如果没有报错的话,该工程已经可以直接启动。

        1.2. 这里有三个文件我们重点讲一下:

BasisApplication :这是Spring boot的启动入口,其内部包含一个main方法,类由@SpringBootApplication注解;
application.properties : 这是Spring boot默认加载的配置文件,也可使用yml或yaml文件;
BasisApplicationTest : 这是一个junit单元测试文件,刚刚生成时其内部还没有具体的功能。

        1.3. pom.xml依赖解析

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

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

</dependencies>
         通过生成的pom.xml我们可以看到上面的几块内容,首先<parent>是用了maven的继承,继承自spring-boot-starter-parent,但是实际应用中往往公司里本身就会有顶层项目,我们知道,一个pom里只能有一个parent,所以我们可以参照《Spring
Boot 不使用默认的 parent,改用自己的项目的 parent》该方法来构建项目。

spring-boot-starter :该模块为核心模块,包含了自动配置、日志和YAML;
spring-boot-starter-test : 支持常规的测试依赖,包含Junit、Hamcrest、Mockito以及Spring-test模块。

二、引入Web模块

        2.1.  添加pom依赖

        如下,该模块支持全栈式web开发,包含了Tomcat和Spring-webmvc

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
        到此时,我们的web系统已经可以开发web服务了。

        2.2. 编写一个Hello World测试web服务

        2.2.1. 创建一个路径为com.aierlice.basis.web的package;
        2.2.2. 创建一个HelloController.java文件,内容如下:
package com.aierlice.basis.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

@GetMapping("hello")
public String hello(){

return "Hello World";
}

}
        2.2.3.  启动工程主程序BasisApplication中的main方法,打开浏览器输入 http://localhost:8080/hello ,就可以看到浏览器页面中输出的 "Hello World"了。

三、 引入Mybatis模块

        3.1 添加pom依赖 (注:本文数据库采用的是mysql)

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

        3.2 在application.properties中添加数据源配置与mybatis配置

#datasource数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/basis?useUnicode=true&characterEncoding=utf8&useSSL=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#mybatis属性配置
mybatis.configuration.mapUnderscoreToCamelCase=true
        对于mybatis的属性配置,相信使用过的人都不陌生,这里只配置了开启驼峰命名法,具体的属性配置可按需添加。

        3.3 创建数据库表,并添加一条数据待测试

CREATE TABLE `user` (
`USER_ID` varchar(32) NOT NULL,
`USER_NAME` varchar(255) DEFAULT NULL,
`USER_SEX` varchar(10) DEFAULT NULL,
PRIMARY KEY (`USER_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `user` VALUES ('1', '张三', '男');


        3.4 创建java文件

         3.4.1 创建实体映射类文件User.java

package com.aierlice.basis.entity;

public class User {

private String userId;

private String userName;

private String userSex;

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getUserSex() {
return userSex;
}

public void setUserSex(String userSex) {
this.userSex = userSex;
}

@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName + ", userSex=" + userSex + "]";
}
}
        3.4.2 创建实体映射操作类文件UserDao.java

package com.aierlice.basis.dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import com.aierlice.basis.entity.User;

@Mapper
public interface UserDao {

@Select("select USER_ID,USER_NAME,USER_SEX from USER where USER_ID = #{userId}")
User getUserById(String userId);
}
        3.4.3 创建测试文件UserDaoTest
package com.aierlice.basis.dao;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import junit.framework.TestCase;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserDaoTest {

@Autowired
UserDao userDao;

@Test
public void getUserByIdTest(){
TestCase.assertEquals("张三", userDao.getUserById("1").getUserName());
}
}  


        测试运行test,运行结果为true,则证明本次简单的web系统框架已经搭建完成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: