springBoot学习(二)
1.首先是pom文件:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
比较普通的Maven项目,有一些不同,上面有一个陌生的标签 <parent>
,这个标签是配置在SpringBoot的父级依赖,加上这个父级依赖,就不用再再下面的依赖中配置版本号。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2.应用入口类
我的项目中的DemoApplication就是一个应用入口类,其中main()方法是应用入口方法,仔细观察,这个类的标记上面还有一个形似电源键的标志。在这个类中有一个注解:@SpringBootApplication
@SpringBootApplication
是一个复合注解包括@ComponentScan
,和@SpringBootConfiguration
,@EnableAutoConfiguration
。
-
@SpringBootConfiguration
继承自@Configuration
,二者功能也一致,标注当前类是配置类,并会将当前类内声明的一个或多个以@Bean
注解标记的方法的实例纳入到srping
容器中,并且实例名就是方法名。 -
@EnableAutoConfiguration
的作用启动自动的配置,@EnableAutoConfiguration
注解的意思就是Springboot
根据你添加的jar包来配置你项目的默认配置,比如根据spring-boot-starter-web
,来判断你的项目是否需要添加了webmvc
和tomcat
,就会自动的帮你配置web项目中所需要的默认配置。 -
@ComponentScan
,扫描当前包及其子包下被@Component
,@Controller
,@Service
,@Repository
注解标记的类并纳入到spring容器中进行管理。是以前的<context:component-scan>
(以前使用在xml中使用的标签,用来扫描包配置的平行支持)
小建议:应用入口类就单独的放置在项目一创建好自动放的位置,不要再移动进其他包中了。
3.SpringBoot的配置文件
Spring Boot 的全局配置文件的作用是对一些默认配置的配置值进行修改。
4.Spring Boot 热部署
在目前的 Spring Boot 项目中,当发生了任何修改之后我们都需要重新启动才能够正确的得到效果,这样会略显麻烦,Spring Boot 提供了热部署的方式,当发现任何类发生了改变,就会通过 JVM 类加载的方式,加载最新的类到虚拟机中,这样就不需要重新启动也能看到修改后的效果了。
用法:在pom文件中添加一个依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
5.Spring Boot 支持 JSP
Spring Boot 的默认视图支持是 Thymeleaf 模板引擎,但是可以通过修改pom文件达到支持jsp的目的
(1)添加依赖:
<!-- servlet依赖. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- tomcat的支持.-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
(2)配置jsp文件的位置,这个配置信息是在application.properties中进行配置的
(3)修改 @RestController 注解为 @Controller,并在里面编写一些代码(下面图片中的是显示当前时间)
(4)由于可以在jsp页面访问了,现在要新建对应的jsp文件了。左侧是新建的webapp及其子目录,右侧是timeNow里面的内容。
运行结果如下所示:由于之前布置了热部署,因此可以在不用重新启动的情况下,达到刷新当前时间的目的。(棒棒哒~!)
6.集成mybatis
(1) 修改 pom.xml 增加对 mysql和 mybatis 的依赖
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
(2)连接数据库,在application.properties中添加下面的东西,添加上这些东西,就能连接数据库了。
spring.datasource.url=jdbc:mysql://localhost:3306/student?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
(3)接下来是entity,controller,mapper包的建立及其内容,首先上个截图。
这是目前为止的项目结构,下面是里面的内容
one:Student类里面的内容
package com.example.demo.entity;
public class Student {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
two:Mapper接口中的内容(这里面只写了一个查询所有信息的方法)
package com.example.demo.Mapper;
import com.example.demo.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface StudentMapper {
@Select("SELECT * FROM student")
List<Student> findAll();
}
three:StudentController里面的内容
package com.example.demo.Controller;
import com.example.demo.Mapper.StudentMapper;
import com.example.demo.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
StudentMapper studentMapper;
@RequestMapping("/list")
public String listStudent(Model model) {
List<Student> students = studentMapper.findAll();
model.addAttribute("students", students);
return "list";
}
}
内部的逻辑结构已经写好了,现在就差一个现实信息的jsp页面了
(4)建立jsp页面,起名为list首先还是截图奉上:
其中右边是新建的jsp页面放的位置,左面是对应的代码,,为了大家复制粘贴方便:附上代码块:
<%--
Created by IntelliJ IDEA.
User: dell
Date: 2018/11/12
Time: 18:38
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
</tr>
<c:forEach items="${students}" var="student">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
</tr>
</c:forEach>
</table>
(5)建立数据库中的表,我的数据库是mysql
(6)重新启动服务器,查看运行的结果
在地址栏输入http://localhost:8080/list 即可得到数据库里面的内容,截图是我打开浏览器后输入地址显示的信息。
到此。整合mybatis就结束了,,俊杰加油!