一、是什么

spring boot来简化spring应用开发,约定大于配置,去繁从简,just run就能创建一个独立的,产品级别的应用;spring有控制反转和面向切面编程两种思想;AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果; IOC中最基本的技术就是“反射(Reflection)”编程,通俗来讲就是根据给出的类名(字符串方式)来动态地生成对象。这种编程方式可以让对象在生成时才决定到底是哪一种对象。
– 官方介绍

Spring Boot帮助您创建可运行的独立的,基于生产级的基于Spring的应用程序。我们对Spring平台和第三方库持固执己见的观点,这样您就可以以最小的麻烦开始使用。大多数Spring Boot应用程序只需要很少的Spring配置。您可以使用Spring Boot创建Java应用程序,可以通过使用java -jar或更传统的战争部署来启动Java应用程序。我们还提供了一个运行“ spring脚本”的命令行工具。

我们的主要目标是:

  • 为所有Spring开发提供根本上更快且可广泛访问的入门体验。

  • 开箱即用,但由于需求开始与默认值有所出入,因此很快就会摆脱困境。

  • 提供一系列大型项目通用的非功能性功能(例如嵌入式服务器,安全性,指标,运行状况检查和外部化配置)。

  • 完全没有代码生成,也不需要XML配置。这一切都是借助于条件注解完成的

二、怎么用

一个用springboot写的web项目是怎么配置的

  1. pom.xml(此处略去头部信息,参考下需要导入的依赖)
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
            <scope>runtime</scope>
        </dependency>
        <!--添加fastjson依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.41</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
  1. application.properties
server.servlet.context-path=/shopping
spring.datasource.url=jdbc:mysql://localhost:3306/smbms
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#日志
logging.level.com.wq.supermarket0817.dao=debug
#thymelea模板配置
#声明thymeleaf使用非严格的html。

spring.thymeleaf.prefix=classpath:/templates/
#是否在呈现模板之前检查模板是否存在
spring.thymeleaf.check-template=true
#检查模板位置是否正确(默认值:true)
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=utf-8
#热部署文件,页面不产生缓存,及时更新
spring.thymeleaf.cache=false
#5. 启用不严谨 检查HTML  LEGACYHTML5
spring.thymeleaf.mode=HTML5
# 导入jar包
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.wq.supermarket0817.pojo

常用注解

  1. @SpringBootApplication 启动注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration主要用于加载配置文件
@EnableAutoConfiguration开启自动配置功能
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),主要用于组件扫描和自动装配
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {}
  1. @Controller

控制器,处理http请求。
3. @RequestMapping

这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上,配置url映射,用于方法和controller类上。
4. @Autowired

可以用来装配bean. 都可以写在字段上,或写在setter方法上。
5. @GetMapping

用于将HTTP get请求映射到特定处理程序的方法注解
6. @PostMapping

用于将HTTP post请求映射到特定处理程序的方法注解
7. @Repository

DAO层注解,DAO层中接口继承JpaRepository<T,ID extends Serializable>,需要在build.gradle中引入相关jpa的一个jar自动加载。

  1. @Mapper

作用:在接口类上添加了@Mapper,在编译之后会生成相应的接口实现类

  1. @Service

@Service是@Component注解的一个特例,作用在类上
@Service注解作用域默认为单例
使用注解配置和类路径扫描时,被@Service注解标注的类会被Spring扫描并注册为Bean
@Service用于标注服务层组件,表示定义一个bean
@Service使用时没有传参数,Bean名称默认为当前类的类名,首字母小写
@Service(“serviceBeanId”)或@Service(value=”serviceBeanId”)使用时传参数,使用value作为Bean名字

代码实例

1.进行这个web项目前首先明确下都有哪些数据,因为这就涉及数据库中表的结构和内容。
2.确定好数据内容后开始编写实体类,务必保证实体类的属性与数据库字段对应。
3.紧接着就得确定业务逻辑,比如这个网站后台管理系统就涉及登陆验证的问题,那就牵扯用户名和密码都要嫩在数据库匹配到,那么就需要dao层去解决了。
UserMapper.xml

<?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.wq.supermarket0817.dao.UserMapper" >
    <resultMap id="BaseResultMap" type="User" >
        <constructor >
            <idArg column="id" jdbcType="BIGINT" javaType="java.lang.Integer" />
            <arg column="userCode" jdbcType="VARCHAR" javaType="java.lang.String" />
            <arg column="userName" jdbcType="VARCHAR" javaType="java.lang.String" />
        </constructor>
    </resultMap>
    <!--登陆验证-->
    <select id="loginCheck" parameterType="User" resultType="User">
        select * from smbms_user where userCode=#{userCode} and userPassword=${userPassword}
    </select>
</mapper>

dao层

@Repository
@Mapper
public interface UserMapper {
    User loginCheck(User user);
}

4.service层

public interface UserService {
    User loginCheck(User user);
}

实现类

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User loginCheck(User user) {
        return userMapper.loginCheck(user);
    }
}

5.controller层负责与页面进行交互,拿数据传数据。

@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserService userService;

    @GetMapping("/loginck")
    public String login(Model model) {
        model.addAttribute("user", new User());
        return "login";
    }
    @PostMapping("/loginck")
    public String logins(@ModelAttribute User user, HttpSession session, Model model) {
        User userck = userService.loginCheck(user);
        if (userck != null) {
            System.out.println(userck);
           session.setAttribute("userck",userck);
            model.addAttribute("user", userck);
            return "main";
        } else {
            return "login";
        }
    }
}

thymeleaf

简单说,Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。相较与其他的模板引擎,它有如下三个极吸引人的特点:

  • 1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 Thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。

  • 2.Thymeleaf 开箱即用的特性。它提供标准和 Spring 标准两种方言,可以直接套用模板实现 JSTL、 OGNL表达式效果,避免每天套模板、改 Jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。

  • 3.Thymeleaf 提供 Spring 标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

    常用标签

    1、常用语法

    ①、赋值、字符串拼接

    <p th:text="{userName}">neo</p>
    
    <span th:text="'Welcome to our application, ' +{userName} + '!'"></span>
    
    <span th:text="|Welcome to our application, ${userName}!|"></span>
    

②、条件判断 If/Unless

Thymeleaf 中使用 th:if 和 th:unless 属性进行条件判断,下面的例子中,标签只有在 th:if 中条件成立时才显示

<a th:if="{flag == 'yes'}"  th:href="@{/home}"> home </a>

<a th:unless="{flag != 'no'}" th:href="@{http://www.ityouknow.com/}" >ityouknow</a>

th:unless 与 th:if 恰好相反,只有表达式中的条件不成立,才会显示其内容。

也可以使用 (if) ? (then) : (else) 这种语法来判断显示的内容

③、for 循环

<table>
    <tr  th:each="user,iterStat : {users}">
        <td th:text="{user.name}">neo</td>
        <td th:text="${user.pass}">213</td>
    </tr>
</table>

iterStat 称作状态变量,属性有

  • index:当前迭代对象的 index(从 0 开始计算)

  • count:当前迭代对象的 index(从 1 开始计算)

  • size:被迭代对象的大小

  • current:当前迭代变量

  • even/odd:布尔值,当前循环是否是偶数/奇数(从 0 开始计算)

  • first:布尔值,当前循环是否是第一个

  • last:布尔值,当前循环是否是最后一个

    关键字 功能介绍 案例
    th:id 替换id th:id=”‘xxx’ + ${collect.id}”
    th:text 文本替换 th:text=”${collect.description}”
    th:utext 支html的文本替换 th:utext=”${htmlcontent}”>conten
    th:object 替换对象 th:object=”${session.user}”
    th:value 属性赋值 th:value=”${user.name}”
    th:with 变量赋值运算 th:with=”isEven=${prodStat.count}%20″>
    th:style 设置样式 th:style=”‘display:’ + @{(${sitrue} ? ‘none’ : ‘inline-block’)} + ””
    th:onclick 点击事件 th:οnclick=”‘getCollect()'”
    th:each 属性赋值 th:each=”media:${allmedia}”
    th:if 判断条件 th:if=”${userId collect.userId}”
    th:unless 和th:if判断相反 th:href=”@{/login}” th:unless=${session.user != null}
    th:href 链接地址 th:href=”@{/login}”>Login />
    th:switch 多路选择 配合th:case 使用 th:switch=”${user.role}”
    th:case th:switch的一个分支 th:case=”1″
    th:fragment 布局标签,定义一个代码片段,方便其它地方引用 th:fragment=”alert”
    th:include 布局标签,通常用来引用外部页面公共的部分 th:include=”left.html”
    th:replace 布局标签,替换整个标签到引入的文件 th:replace=”fragments/header :: title”
    th:selected selected选择框 选中 th:selected=”(${xxx.id} ${configObj.dd})”
    th:src 图片类地址引入 th:src=”@{/img/logo.png}”
    th:inline 定义js脚本可以使用变量 type=”text/javascript” th:inline=”javascript”
    th:action 表单提交的地址 th:action=”@{/subscribe}”
    th:remove 删除某个属性 th:remove=”all”

核心问题

get和post的区别