启动概述
什么是引导
使用Boot有什么好处
回顾我们之前的SSM项目,构建过程还是比较繁琐的,需要:
而使用Boot开发项目只需要进行一些配置即可构建Web项目,并且可以使用IDEA自动生成。
启动快速构建
第 1 步:创建一个新项目
选择,然后选择默认url,点击【下一步】
修改群组及信息,点击【下一步】
检查网页模板:
这与你选择版本无关,只是为了方便给你提供几个比较典型的版本。 你首先随机选择一个版本,构建项目后,只需将 pom.xml 中的版本更改为你想要的版本即可。 比如你一开始选择的1.5.4版本:
org.springframework.boot
spring-boot-starter-parent
1.5.4.RELEASE
只要改成你想要的,maven就会帮你重新导入依赖。
选择项目所在位置,点击【】
创建时间较长,耐心等待即可。 创建完成后,项目结构如下:
项目结构看起来还是蛮清爽的,缺少很多配置文件。 我们看一下默认生成的内容:
第二步:
在[com..]包下新建[]:
package com.cherry.springboot;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "Hello Spring Boot!";
}
}
第三步:使用IDEA启动启动
我们回到 n 类并运行:
注:上面项目中我们之所以没有手动配置服务器是因为Boot内置了
可以看到我们正在8080端口上运行,让我们访问“/hello”地址并尝试:
可以看到页面成功显示了我们返回的信息。
解析Boot项目
解析pom.xml文件
我们可以看到一个比较陌生的标签
,这个标签是配置Boot的父级依赖:
org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
这样,当前项目就是Boot项目,而-boot--是一个特殊的项目,用于提供相关的Maven默认依赖。 使用它后,常用的包依赖都可以保存标签。
关于Boot提供的jar包的依赖关系,我们可以查看本地Maven仓库:\\org\\boot\-boot-\2.0.1.\-boot--2.0.1..pom文件来查看,相当长...
应用入口类
Boot项目通常有一个名为*的入口类,入口类中有一个main方法。 这个main方法实际上是一个标准的Javay应用程序入口方法。
@n是Boot的核心注解。 它是一个组合注解,组合了:@、@ion、@; 如果不使用@n注解,也可以使用这三个注解代替。
启动配置文件
Boot 使用全局配置文件。 或者.yml,放在类路径下的[src/main/]目录或者/下。
Boot不仅支持常规配置文件,还支持yaml语言的配置文件。 yaml 是一种以数据为中心的语言,在配置数据时具有面向对象的特性。
Boot的全局配置文件的作用就是修改一些默认配置的配置值。
当我们也将默认端口设置为8080,并将默认访问路径从“/”修改为“/hello”时,该文件与yml文件的区别如上所示。
我们直接删除后缀名为. 后缀,使用.yml文件进行简单配置,然后使用@Value获取配置属性:
重启Boot并查看结果
注意:我们没有在yml文件中指明属性的类型,而是在使用的时候定义了。
您还可以使用配置文件中的当前配置:
仍然得到正确的结果:
封装配置信息
我们可以将配置信息封装到一个类中,首先在我们的名字和年龄之前添加一个用户前缀,然后创建一个新的类来封装这些信息,并使用两个注解:
@:表示当前类是一个Java Bean @ies( = "user"):表示获取以user为前缀的配置信息
这样我们就可以在控制器中使用并重启就可以得到正确的信息了:
注意这里,它不能配成user,它会读取你电脑的配置信息,最后改成users
启动热部署
在当前的Boot项目中,当发生任何修改时,我们都需要重新启动才能得到正确的效果,这会有点麻烦。 Boot提供了热部署方式。 当发现任何类发生变化时,就会通过JVM类加载方式将最新的类加载到虚拟机中,这样无需重启就可以看到修改后的效果。
我们可以向 pom.xml 添加依赖项:
org.springframework.boot
spring-boot-devtools
true
开机使用
上面已经完成了Boot项目的简单搭建,我们只需要进行一些简单的设置,写一个直接运行就可以了,不要太简单了……接下来我们深入了解一下Boot的使用。
开机支持JSP
Boot默认的视图支持是模板引擎,但是我们对此并不熟悉,如果我们还想使用JSP怎么办?
javax.servlet
javax.servlet-api
provided
javax.servlet
jstl
org.apache.tomcat.embed
tomcat-embed-jasper
provided
修改 .yml 文件,将我们的 JSP 文件重定向到 /WEB-INF/views/ 目录:
将@注解修改为@,然后将hello方法修改为:
在[src/main]目录下依次创建、WEB-INF、views目录,并创建hello.jsp文件:
因为我们部署了热部署功能,所以只需要等待控制台重启信息完成然后刷新网页就可以看到正确的效果
集成【无配置文件的注解版本】
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
mysql
mysql-connector-java
5.1.21
在[com..]下新建一个[]包,然后在其下创建一个类:
package com.cherry.springboot.entity;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.io.Serializable;
@Getter
@Setter
@ToString
public class Student implements Serializable {
private static final long serialVersionUID = 1671658130062704119L;
private Integer id;
private String name;
private String sex;
private Double grade;
}
在[com..]下新建一个[dao]包,然后在其下创建一个:
package com.cherry.springboot.dao;
import com.cherry.springboot.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface IStudentDao {
@Select("SELECT * FROM student_info")
List selectAll();
}
在[com..]下新建一个[]包,然后在其下创建一个:
package com.cherry.springboot.controller;
import com.cherry.springboot.dao.IStudentDao;
import com.cherry.springboot.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class StudentController {
@Autowired
private IStudentDao iStudentDao;
@RequestMapping("/student")
public String listStudent(Model model) {
List students = iStudentDao.selectAll();
model.addAttribute("students", students);
return "student";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
'1' cellspacing='0'>
id
name
sex
grade
${s.id}
${s.name}
${s.sex}
${s.grade}
集成【配置文件注释版本】
package com.cherry.springboot.entity;
import java.io.Serializable;
@Getter
@Setter
public class User implements Serializable {
private Integer id;
private String name;
private String sex;
}
package com.cherry.springboot.controller;
import com.cherry.springboot.entity.User;
import com.cherry.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/selectByPrimaryKey")
public String selectUserById(Model model) {
User user = userService.selectByPrimaryKey(1);
model.addAttribute("user", user);
return "user";
}
}
package com.cherry.springboot.service;
import com.cherry.springboot.entity.User;
public interface IUserService {
User selectByPrimaryKey(Integer id);
}
package com.cherry.springboot.service.impl;
import com.cherry.springboot.dao.IUserDao;
import com.cherry.springboot.entity.User;
import com.cherry.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceIpml implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public User selectByPrimaryKey(Integer id) {
return userDao.selectByPrimaryKey(id);
}
}
package com.cherry.springboot.dao;
import com.cherry.springboot.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface IUserDao {
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
id, name, sex
delete from user_info
where id = #{id,jdbcType=INTEGER}
insert into user_info (id, name, sex
)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}
)
insert into user_info
id,
name,
sex,
#{id,jdbcType=INTEGER},
#{name,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR},
update user_info
name = #{name,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR},
where id = #{id,jdbcType=INTEGER}
update user_info
set name = #{name,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
'1' cellspacing='0'>
id
name
sex
${user.id}
${user.name}
${user.sex}
package com.cherry.springboot;
import com.cherry.springboot.entity.User;
import com.cherry.springboot.service.IUserService;
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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootApplicationTests {
@Autowired
private IUserService userService;
@Test
public void contextLoads() {
User user = userService.selectByPrimaryKey(1);
System.out.println(user);
}
}
集成【配置文件注释版】中的实体类和文件由-代码自动生成工具生成。 具体的使用方法我会在下一篇中写出来,以提高大家以后的开发效率。
以上就是项目的创建以及整个jsp和jsp。 这是一份比较完整的文档,希望对大家有所帮助。