Spring-boot-starter-web内置了Spring和Spring MVC,Tomcat等: 如果想用Netty可以用Spring-boot-starter-webflux.
或者:
@SpringBootApplication public class Run { public static void main(String[] args) { SpringApplication.run(Run.class,args); } }直接在main方法中启动,而不需要配置Tomcat来启动 启动成功: 直接用localhost:8080访问
Spring Boot持久层提供了Spring-jdbc,mybatis,hibernate等的支持,此处使用mybatis.
pom.xml引入mybatis-spring-boot-starter依赖:
<!--载入mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> </dependencies>经过实测1.3.2兼容spring-boot-starter-web的2.1.5.RELEASE版本,其他有些版本不能兼容导致不能启动 mybatis-spring-boot-starter包含了mybatis:
Spring Boot的配置文件是固定的,为application.properties或application.yml 创建application.properties: 内容: properties:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/travel spring.datasource.username=root spring.datasource.password=188278 mybatis.type-aliases-package=com.test.domain mybatis.mapperLocations=classpath:daoMappers/*.xmlyml: yml要注意缩进和":"后要有空格
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/travel username: root password: 1234 mybatis: type-aliases-package: com.test.domain mapperLocations: classpath:daoMappers/*.xmlmybatis.mapperLocations为mybatis映射文件位置
如果不想写get,set可以用lombok.
package com.test.domain; import java.io.Serializable; /** * 用户实体类 */ public class User implements Serializable { private int uid;//用户id private String username;//用户名,账号 private String password;//密码 private String name;//真实姓名 private String birthday;//出生日期 private String sex;//男或女 private String telephone;//手机号 private String email;//邮箱 private String status;//激活状态,Y代表激活,N代表未激活 private String code;//激活码(要求唯一) /** * 无参构造方法 */ public User() { } /** * 有参构方法 * @param uid * @param username * @param password * @param name * @param birthday * @param sex * @param telephone * @param email * @param status * @param code */ public User(int uid, String username, String password, String name, String birthday, String sex, String telephone, String email, String status, String code) { this.uid = uid; this.username = username; this.password = password; this.name = name; this.birthday = birthday; this.sex = sex; this.telephone = telephone; this.email = email; this.status = status; this.code = code; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } }pom.xml引入依赖:
<!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> <version>1.16.18</version> </dependency>@Data 自动生成所有属性的get,set方法 @AllArgsConstructor 自动生成所有参数的构造方法 @NoArgsConstructor 自动生成无参构造方法.
/** * 用户实体类 */ @Data //自动生成所有属性的get,set方法 @AllArgsConstructor @NoArgsConstructor public class User implements Serializable { private int uid;//用户id private String username;//用户名,账号 private String password;//密码 private String name;//真实姓名 private String birthday;//出生日期 private String sex;//男或女 private String telephone;//手机号 private String email;//邮箱 private String status;//激活状态,Y代表激活,N代表未激活 private String code;//激活码(要求唯一) }@Sl4j还可以打印日志.用log.info()方法. 如:
@RestController @Slf4j public class TestController { @Autowired private UserService userService; @RequestMapping("/test") public String test(){ userService.checkPwd("aa","111111"); log.info("****this is test"); return "test"; } }创建映射文件: 编写映射文件:
<?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.test.dao.UserDao"> <select id="findById" resultType="com.test.domain.User" parameterType="int"> select * from tab_user where uid = #{id} </select> </mapper>pom引入依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>application.properties配置文件中配置thymaleaf配置:
spring.thymeleaf.cache = false spring.thymeleaf.encoding = UTF-8开发时关闭缓存
Thymeleaf默认静态文件位置是resources/static,默认模板文件位置是resources/templates,默认后缀是.html,也可以自己配置:
#spring.thymeleaf.mode = HTML5 spring.thymeleaf.prefix = classpath:/mytemplates/ spring.thymeleaf.suffix = .htmlspring.thymeleaf.prefix = classpath:/mytemplates/指定了模板页面位置.
模板页面不能直接访问,需要通过控制类跳转。
页面引入thymeleaf:
<html xmlns:th="http://www.thymeleaf.org">将默认从resources/static/login/css/signin.css中寻找该文件.
<form class="form-signin" th:action="@{/Login}">将请求Login
和EL表达式类似
至于h1里面的原有的值只是为了给前端开发时做展示用的.这样的话很好的做到了前后端分离。
th:text="*{firstName}等价于th:text="${session.user.firstName}
th:text="|Welcome to our application, ${user.name}!|“相当于th:text=”'Welcome to our application, ’ + ${user.name} + ‘!’" 但只能包含表达式变量,而不能有条件判断等
th:οnclick="‘delUser(’+${user.id}+’);’"
If,如果成立则显示,否则不显示标签. th:if="${hello==‘Hell’}"
?:,相当于if-else。 th:text="${hello==‘Hello’?‘Hello’:‘Hi’}+’!请登录’"
打包成jar包 Pom.xml中配置:
<packaging>jar</packaging>还要指定main方法所在的类:
<plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.itheima.BlogSystemApplication</mainClass> </configuration> </plugin> </plugins> </build>