Springboot来简化Spring应用开发的一个框架,约定大于配置
注意点:
【简化spring 不是简化 mybatis】
【Spring干的事 springboot都可以干 但替代不了Spring】
【对spring的优化上进行了封装 】
- 可以快速的构建独立运行的Spring项目
- 框架内有Servlet容器,无需依赖外部,所以不需要打成war包
【springboot内置tomcat(tomcat不需要了)】 - 极力去掉重复恶心的xml配置
【springboot简化了繁琐的xml】
reources 配置文件里:
static : 保存所有静态资源 js,css,images;
templates : 保存所有的模板页面; (默认不支持jsp)
application.properties :SprinBoot应用的配置文件
注意点: SpringBoot不会和jsp一起用
4.SpringBoot-HelloWorld(1)快速向导创建SpringBoot工程
New Project - Spring Initializr初始化架构
(2)配置工程名
注意点:
- SprinBoot 可能高版本的idea 解决不了。
- 如果出不来第二个页面 就退回去再点进去
- 如果还是成功不了 改custom 为阿里云的配置http://start.aliyun.com
或者改http://start.spring.io 去掉s,因为https是受保护的。
其中里面的工程名Artifact不大写
最后的包Package不写工程名只写包名
Java Version 为 8
(3) 配置Web选项
加个Web - Spring web
(4)进入工程后
先进行下载一段时间
注意点:
如果springboot里面报错在 pom.xml加
org.apache.maven.plugins maven-compiler-plugin utf-8 1.8 1.8
(5)创建controller/controller类
package cn.zjj.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@RequestMapping("/index.do")
public String index(){
return "hello world!";
}
}
注意点:
-
@RestController等价于@Controller + @ResponseBody
一般@RestController在springboot使用 -
还有springboot集成了tomcat 不用配了
(6)测试
1.Springboot1223Application类(启动函数)里执行main
package cn.zjj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Springboot1223Application {
public static void main(String[] args) {
SpringApplication.run(Springboot1223Application.class, args);
}
}
@SpringBootApplication :
是Sprnig Boot项目的核心注解,目的是开启自动配置
2.http://localhost:8080/index.do
hello world!
【打jar包到linux,不打war包】
1.双击package
然后等一段时间就会出现下面的画面:
2. 右击springboot_1223-0.0.1-SNAPSHOT.jar
File Path 弹到对应的目录
springboot_1223-0.0.1-SNAPSHOT
3.路径干掉 打cmd 弹出 命令提示窗口
》 java -jar springboot_1223-0.0.1-SNAPSHOT.jar
jar打包 不是根据idea来打开。
4.http://localhost:8080/index.do
hello world!
题外话:(摘录)
1.war 包是一种打包格式
Java web工程,都是打成war包,进行发布,打成war包的好处是不会缺少目录,并且只管理好一个发布文件就好,并且tomcat服务器能够自动识别,将war包放在tomcat容器的webapps下,启动服务,即可运行该项目,该war包会自动解压出一个同名的文件夹。
在创建web 项目的时候要选择打包的方式,一般都是热部署(war explode)
2.JAR(Java Archive,Java 归档文件)
它是与平台无关的文件格式,它允许将许多文件组合成一个压缩文件。
JavaSE程序可以打包成Jar包(J其实可以理解为Java了)。
(0). 引导:
修改tomcat端口号
application.properties 主配置文件
server.port=8081
结果:
http://localhost:8081/index.do
hello world!
由于yml 格式要求及其严格 父子级 更清晰 层次
【一般不用application.properties改成application.yml】
(1)基本语法:
k:(空格)v:表示一对键值对(空格必须有)
用空格的缩进来控制层级关系,
只要是左对齐的一列数据,都是同一个层级的
(2)实战:
application.yml
server: port: 8083
测试:
http://localhost:8083/index.do
hello world!
7. SpringBoot整合Mybatis【(springboot集成mybatis)】
(1)创建springboot工程,
勾选mysql和mybatis的插件
Web - Spring Web
SQL - MyBatis framework和Mysql Driver
记得最开始创建工程的时候添加上面三个
作用: 引入依赖 不用生敲了
其中mabatis依赖 是集成 springboot的
**注意:**修改pom中mysql版本
mysql mysql-connector-java 8.0.21
(2).配置引入相关配置
resources/application.yml
数据库的相关配置:
spring:
datasource:
url: jdbc:mysql://localhost:3306/dm_user?serverTimezone=Asia/Shanghai
username: root
password: ok
driver-class-name: com.mysql.jdbc.Driver
mybatis的相关配置
(其中在resources下面建一个包 mapper)
mybatis: mapper-locations: classpath:mapper/*Mapper.xml
说明:
1.mapper包下的所有xml
【识别 xml文件自动映射】
2.classpath可以不写 Mapper必须大写
3.mapper-locations是一个定义mapper位置的属性,
在xxx.yml或xxx.properties下配置,作用是实现mapper接口配置
注意点:
当mapper接口和mapper接口对应的配置文件在 命名上相同 、
所在的路径相同,则mapper-locations可以不用配置,配置也不会生效。
如果mapper接口和mapper接口对应的配置文件在命名上不同或
所在的路径不同,需要配置mapper-locations才能实现接口的绑定
(3).编写mapper接口
java/cn.zjj.mapper
[@Mapper :将mapper接口交给spring管理]
package cn.zjj.mapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper {
//查询
Integer findCount();
}
(5).编写mapper.xml
reosources/mapper/UserMapper.xml
select count(*) from t_user
(6).编写service,serviceImpl
java/cn.zjj.service
A.service
public interface UserService {
//查询
Integer findCount();
}
B.serviceImpl
import cn.zjj.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
// 报错不用管 idea的问题 不能识别@autowired 改成@Resource
@Override
public Integer findCount() {
return userMapper.findCount();
}
}
(7).编写controller
java/cn.zjj.controller/UserController
import cn.zjj.service.UserService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
// set注入
private UserService userService;
@RequestMapping("/findAll")
public String findCount(){
return userService.findCount().toString();
}
}
@ResponseBody不用写了有@RestController 》传数据
但如果是@Controller 要加@ResponseBody
(8)在启动函数上添加扫描包
@SpringBootApplication
@MapperScan("cn.zjj.mapper")
public class SpringDemo03Application {
public static void main(String[] args) {
SpringApplication.run(SpringDemo03Application.class, args);
}
}
注意点:
-
在启动函数里面加注解 @MapperScan(“cn.kgc.mapper”)
扫mapper包,就可以不加@mapper -
@Mapper作用:(摘录)
在接口类上添加了@Mapper,在编译之后会生成相应的接口实现类
如果想要每个接口都要变成实现类,那么需要在每个接口类上加上@Mapper注解,比较麻烦,解决这个问题用@MapperScan。 -
@MapperScan作用:(摘录)
指定要变成实现类的接口所在的包,(指定要扫描Mapper类的包的路径)然后包下面的所有接口在编译之后都会生成相应的实现类一般是在Springboot启动类上面添加Springboot122301Application 中写
(9)测试
http://localhost:8080/findCount
4
(10)分页
10.1 依赖
com.github.pagehelper pagehelper-spring-boot-starter 1.4.1
注意点:
-
pagehelper 用在javaweb (ssm)
pagehelper-spring-boot-starter用在springboot -
springboot-pagehelper(摘录)
启动PageHelperAutoConfiguration自循环依赖问题
2.1 使用分页版本1.4.1 或1.0.0
2.2 其他版本可能会报错,或者把springboot的版本改一下改成2.5.5
10.2 yml
pagehelper: helper-dialect: mysql reasonable: true
说明:
1.reasonable: true最后一页的时候
点击下一页会跳到第一页并且有数据,首页点上一页 不管用
2.如果是false则跳到第一页不会有数据,再点一次才会有
首页点上一页会跳到无数据的然后再上一页就不管用了
10.3 entity
public class User {
private Integer uid;
private String uname;
private String upwd;
// 省略getter和setter方法
}
10.4 UserMapper
@Select("select * from t_user")
List findAll();
// 有了@Select就不用写Usermapper.xml了
10.5 Service包
UserService接口
PageInfofindAll(Integer pageNo);//内部封装了分页 自动limit
UserServiceImpl实现类
@Override public PageInfofindAll(Integer pageNo) { PageHelper.startPage(pageNo,2); return new PageInfo<>(userMapper.findAll()); }
10.6 UserController
@RequestMapping("/findAll")
public PageInfo findAll(@RequestParam(defaultValue = "1",required = false) Integer pageNo){
return userService.findAll(pageNo);
}
10.7 测试
http://localhost:8080/findAll
{“total”:4,“list”:
[{“uid”:1,“uname”:“as”,“upwd”:“123”},
{“uid”:2,“uname”:“qw”,“upwd”:“456”}],
“pageNum”:1,“pageSize”:2,“size”:2,“startRow”:1,“endRow”:2,
“pages”:2,“prePage”:0,“nextPage”:2,“isFirstPage”:true,
“isLastPage”:false,“hasPreviousPage”:false,“hasNextPage”:true,
“navigatePages”:8,“navigatepageNums”:[1,2],“navigateFirstPage”:1,“navigateLastPage”:2}
----2021.12.22&12.23&12.24



