使用IDEA搭建SpringBoot+SSM框架

前言

无论是SSH还是SSM,Spring必不可少。比较传统的模式都是以applicationContext.xml配置文件的形式存在,当然最难也是最繁琐的也是这些配置文件,特别容易出错,如何简单快速的上手spring新项目呢?Spring的组织对对spring框架的全系列组件进行了内部封装。对外只是提供maven(jar管理、项目打包工具)或者gradle(新兴jar管理、项目打包工具)的形式来进行引入parent.pom(maven配置文件)或者parent.gradle(gradle配置文件),让每一个spring项目都是以spring的子项目的形式来运行,这样开发人员不用再去注重配置文件的繁琐而是把精力放到业务逻辑以及更深层次的架构方面。

SpringBoot主要优点

  1. 为所有Spring开发者更快的入门
  2. 开箱即用,提供各种默认配置来简化项目配置,帮助开发者快速整合第三方框架(原理maven依赖特性)
  3. 内嵌式容器【web容器 tomcat】简化Web项目,完全不需要第三方服务器即可运行, 内置了第三方容器(tomcat/jetty/undertom) (原理:tomcat容器使用java开发的)
  4. 没有冗余代码生成和XML配置的要求,使用注解的方式来简化xml书写
  5. 提供一系列大型企业级项目的功能性特性(比如:安全、健康检测、外部化配置、数据库访问、restful搭建等)

SpringBoot特性

  1. 可以创建独立的Spring应用程序,并且基于其Maven或Gradle插件,可以创建可执行的JARs和WARs;
  2. 内嵌Tomcat或Jetty等Servlet容器;
  3. 提供自动配置的“starter”项目对象模型(POMS)以简化Maven配置;
  4. 尽可能自动配置Spring容器
  5. 提供准备好的特性,如指标、健康检查和外部化配置;
  6. 绝对没有代码生成,不需要XML配置。

SpringBootStater是什么

启动器是一套方便的依赖描述符,它可以放在自己的程序中。你可以一站式的获取你所需要的 Spring 和相关技术,而不需要依赖描述符的通过示例代码搜索和复制黏贴的负载。

例如,如果你想使用 Sping 和 JPA 访问数据库,只需要你的项目包含 spring-boot-starter-data-jpa 依赖项,你就可以完美进行。

初步使用IDEA建立一个SpringBoot

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
打开视图:
在这里插入图片描述
继续等,可能需要20分钟左右:这个时候可以做点别的事情.
写完相关代码后…
我们在入口程序类中右键->Run DemoApplication在控制台的输出如下所示,证明你已经成功的运行了springboot项目
在这里插入图片描述
在这里插入图片描述
启动成功,打开浏览器:
在这里插入图片描述

新建springboot +SSM框架项目:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

实体类、Dao、service

代码就不放了,注意点说下:

  1. @mapper或者@repository注解是在Dao层的应用;
    在这里插入图片描述

  2. @Mapper是mybatis自身带的注解; 使用@mapper后,不需要在spring配置中设置扫描地址,通过mapper.xml里面的namespace属性对应相关的mapper类,spring将动态的生成Bean后注入到Service层。

  3. @repository则需要在Spring中配置扫描包地址,然后生成dao层的bean,之后被注入到ServiceImpl中。
    也就是说,如果用了@repository,我们的测试类上(程序启动入口)需要加入
    在这里插入图片描述

  4. Dao层如果使用xml方式,需要在application.properties加上扫描路径

#指定Mybatis中配置的映射文件的地址
mybatis.mapper-locations=classpath:/mapper/*.xml
application.properties配置文件
# 修改tomcat的端口
server.port=8082

# 数据库连接配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3308/ticketdb?useUnicode=true&characterEncoding=utf8&serverTimezone=PRC
spring.datasource.password=root
spring.datasource.username=root

#指定Mybatis中配置的映射文件的地址
mybatis.mapper-locations=classpath:/mapper/*.xml

#spring boot已经集成logback日志,加入日志
# 日志 WARN级别输出
logging.level.com.ccp.demo.controller=WARN
#日志 DEBUG级别输出
logging.level.com.ccp.demo.dao=DEBUG
logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n
logging.pattern.file=%d{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n

#thymeleaf配置——类似视图解析器
#开发阶段关闭
spring.thymeleaf.cache=false  
spring.thymeleaf.suffix=.html
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.encoding=utf-8

DemoApplication.java
@SpringBootApplication
//这里加上扫描的包
@MapperScan("com.ccp.demo.dao")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
controller层
@Controller
public class UserController {

    @Autowired
    private Userservice userservice;

    @GetMapping("ccp/user/th")
    public ModelAndView toList() {
        ModelAndView mav = new ModelAndView("index");//页面路径
        List<User> list = userservice.getUsers();//查询全部的方法
        mav.addObject("list", list);
        return mav;
    }

}
测试类
@SpringBootTest
@MapperScan("com.ccp.demo.dao")
class DemoApplicationTests {

    @Autowired
    private Userservice userservice;

    @Test
    void contextLoads() {
    	//输出全部用户user
        System.out.println(userservice.getUsers());
    }
}
前端页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<!--    @标记,引入静态 css、js、jpg等;表单提交-->
<link rel="stylesheet" th:href="@{/bootstrap/css/bootstrap.css}">
</head>
<body>
<table class="container" border="1" cellspacing="0">
    <thead>
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>密码</th>
        <th>昵称</th>
        <th>头像</th>
    </tr>
    </thead>
    <tbody>
    <!--  -->
    <tr th:each="item,itemStat : ${list}">
        <td th:text="${item.userid}">1</td>
        <td th:text="${item.username}">2</td>
        <td th:text="${item.password}">3</td>
        <td th:text="${item.nickname}">4</td>
        <td th:text="${item.photo}">5</td>
    </tr>
    </tbody>
</table>
</body>
</html>

数据库User表:
在这里插入图片描述
页面展示
在这里插入图片描述

静态模板引擎使用

常用模板引擎

== Thymeleaf ==、jsp、Freemarker

操作步骤

引入templet依赖Thymeleaf

  1. 新建项目:
    在这里插入图片描述
    或者在现有项目的pom文件加入坐标依赖
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
		<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
  1. 工具生成实体类,dao接口和实现
    SSM中实体类(entity user),mapper(接口和xml映射文件), Mybatis Generator ,自己写sprintBootTest测试类

  2. 引入thymeleaf操作步骤

  • 导入thymeleaf依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  • 配置thymeleaf
## thymeleaf配置
spring.thymeleaf.cache=false
spring.thymeleaf.suffix=.html
spring.thymeleaf.prefix=classpath:/templates
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.content-type=text/html

控制器转发过来的的时候,不用写前缀了.; /templates

  • html静态页面放在路径classpath:/templates下
    在静态页面中导入thymeleaf的名称空间
  • 使用thymeleaf语法渲染数据即可
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF‐8"><title>Title</title></head>
<body><div th:text=“${user.username}”>欢迎你!</div></body>
</html>
  • 加入一个静态资源支持:
    Pom文件变化:
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
</plugins>
    <resources>
        <resource>
            <!--使用哪些静态资源-->
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.xml</include>
                <include>static/**</include>
                <include>**/*.properties</include>
                <include>**/*.html</include>
            </includes>
        </resource>

    </resources>
</build>
  • 复制css等文件到static目录下
    在这里插入图片描述

  • 页面上使用@来引入

<!--@标记 ,引入静态资源 css,js,jpg 等; 表单提交 -->
<link rel="stylesheet" th:href="@{/bootstrap/css/bootstrap.css}">

修改了thymeleaf页面的代码后,如果想看执行效果,我们先使用 ctrl + shift + F9来刷新(编译);
在这里插入图片描述

注:

@SpringBootAplication
@SpringBootAplication:用于标识spring boot应用程序,代表该类是一个spring boot启动类
Spring boot运行这个类的main方法时启动SpringBoot应用。
由6个注解组成
在这里插入图片描述
除了常见的4个注解:
@Target
@Rentention
@Documention
@Inherited
还有
@SpringBootConfiguration: Spring Boot的配置类。标注在类上表示是一个Spring Boot的配置类.
@Configuration:配置类上来标注这个注解。配置类相当于配置文件。配置类也是容器中的一个组件。@Component把组件(实体类)实例化到spring容器中。
@EnableAutoConfiguration:开启自动配置功能;

当我们需要Spring Boot帮我们自动配置所需要的配置,@EnableAutoConfiguration告诉Spring Boot开启自动配置功能,这样Spring Boot会自动配置好并使之生效。
@AutoConfigurationPackage:自动配置包
@Import(AutoConfigurationPackages.Registrar.class):
Spring的底层注解@Import,给容器中导入一个组件。导入的组件由AutoConfigurationPackages.Registrar.class。
将主配置类(标注@SpringBootApplication注解的类)的所在目录的包及下面所有子包里面的所有组件扫描到Spring容器。

日期输出

controller

@GetMapping("api/th01")
    public String th01(HttpSession session) {
        session.setAttribute("username", "thymeleaf");
        session.setAttribute("birthdate",new Date());
        return "date";
    }

页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div th:text="|${#session.getAttribute('username')}欢迎你!|"></div>
    <div th:text="${session.username}"></div>
    <div th:text="${#dates.format(session.birthdate,'yyy-MM-dd')}"></div>
</body>
</html>

在这里插入图片描述

自定义变量

未简化

                <tr th:each="item,itemStat : ${list}">
                    <td th:text="${item.userid}"></td>
                    <td th:text="${item.username}"></td>
                    <td th:text="${item.password}"></td>
                    <td th:text="${item.nickname}"></td>
                    <td th:text="${item.photo}"></td>
                </tr>

简化后

<!--当数据量比较多的时候,频繁的写gd.就会非常麻烦。因此,Thymeleaf提供了自定义变量来解决:-->
        <tr th:each="item,itemStat : ${list}" th:object="${item}">
            <td th:text="*{userid}"></td>
            <td th:text="*{username}"></td>
            <td th:text="*{password}"></td>
            <td th:text="*{nickname}"></td>
            <td th:text="*{photo}"></td>
        </tr>
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千鹤万象

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值