1. SpringBoot入门

1.1 SpringHelloworld
1.1.1 新建maven项目

springboot基本使用
springboot基本使用
稍等会出现项目目录
springboot基本使用
在pom.xml中添加依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
1.1.2 编写controller类

springboot基本使用

1.1.3 编写springboot启动类

springboot基本使用

1.1.4 运行

在启动类中右键 run as – javaApplication
地址(无需输入项目名)

springboot基本使用

2. SpringBoot 项目属性配置

2.1 项目内置属性

建一个application.properties文件
springboot基本使用
在该文件中配置项目内置属性(如端口号和项目名称)
springboot基本使用
运行
springboot基本使用

2.2 自定义属性

在application.prpperties文件中配置自定义属性

helloWorld=spring daye nihao\u4F60\u597D
mysql.jdbcName=com.mysql.jdbc.Driver
mysql.dbUrl=jdbc:mysql://localhost:3306/db_boot
mysql.userName=root
mysql.password=123456

编写
springboot基本使用
重启后运行,可以看到自定义属性注入成功了
springboot基本使用

2.3 ConfigurationProperties 配置

根据application.prpperties文件中配置编写MysqlProperties数据库信息实体类(配置文件中前缀是mysql,因此这里prefix=”mysql”)
springboot基本使用
编写controller
springboot基本使用
重运行
springboot基本使用

3. SpringBoot之 MVC支持

3.1 @requestMapping配置url映射

pom.xml中添加依赖(有用到freemarker)

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

建controller
springboot基本使用
这controller返回的是hello 的页面,需新建一个名叫hello的html文件然后修改后缀为.ftl
springboot基本使用
运行
springboot基本使用

3.2 @RestController处理ajax请求

建一个页面
springboot基本使用
编写controller
springboot基本使用
运行
springboot基本使用

3.3 @PathVariable 获取 url 参数

编写controller
springboot基本使用
前段
springboot基本使用
运行
springboot基本使用
点击a标签
springboot基本使用

3.4 @RequestParam获取请求参数

前端
springboot基本使用
Controller层
springboot基本使用
页面
springboot基本使用
运行
springboot基本使用
注意:若页面获取不到后台传的值会报错,需这么处理才不会报错
springboot基本使用

4. SpringBoot之 SpringData Jpa支持

4.1 SpringDataJpa基本 crud实现
4.1.1 生成表

新建maven项目(与新建springboot-hello类似)
在pom.xml添加依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
		    <groupId>mysql</groupId>
		    <artifactId>mysql-connector-java</artifactId>
		    <version>5.1.24</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

建数据库
springboot基本使用
建实体类
springboot基本使用
数据源配置文件(配置时注意层次,以空格为一个层次)
springboot基本使用
Run as (运行)
springboot基本使用
运行完毕重启数据库,可以看到生成了一张t_book表
springboot基本使用

4.1.2 查询

新建dao
springboot基本使用
建controller
springboot基本使用
建页面
springboot基本使用
需在pom.xml中添加依赖

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

在表中插入一些数据后运行
springboot基本使用

4.1.3 新增、修改、删除

编写controller
springboot基本使用
编写页面:
新增页面
springboot基本使用
修改页面
springboot基本使用
运行完美

4.2 自定义查询@Query(自己写sql语句)
4.2.1 HQL语句

Dao
springboot基本使用
Controller
springboot基本使用
运行
springboot基本使用

4.2.2 SQL语句

Dao(需开启本地查询,否则运行出错)
springboot基本使用
Controller
springboot基本使用
运行(随机查出两条)
springboot基本使用

4.2.3 动态查询 Specification

Dao需在继承一个接口
springboot基本使用
Controller

@RequestMapping("/list2")
	public ModelAndView list2( final Book book){
		ModelAndView modelAndView=new ModelAndView();
		List<Book> bookList=bookDao.findAll(new Specification<Book>(){
			public Predicate toPredicate(Root<Book> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate=cb.conjunction();
				if(book!=null){
					if(book.getName()!=null && !"".equals(book.getName())){
						predicate.getExpressions().add(cb.like(root.get("name").as(String.class), "%"+book.getName()+"%"));
					}
					if(book.getAuthor()!=null && !"".equals(book.getAuthor())){
						predicate.getExpressions().add(cb.like(root.get("author").as(String.class), "%"+book.getAuthor()+"%"));
					}
				}
				return predicate;
			}
			
		});
		modelAndView.addObject("books", bookList);
		modelAndView.setViewName("bookList");
		return modelAndView;
	}

页面
springboot基本使用
运行
springboot基本使用
springboot基本使用

5. SpringBoot之事务管理

5.1 @Transactional
5.1.1 不出异常版

新建数据库db_bank和maven项目(与建springboot-hello类似)并配置application.yml数据源,(pom.xml文件与springboot-data的一致即可,不祥述)
springboot基本使用
建实体类
springboot基本使用
运行之后,数据库db_bank下生成一张t_account表
插入数据

springboot基本使用
Dao
springboot基本使用
service
springboot基本使用
springboot基本使用
Controller
springboot基本使用
运行
springboot基本使用
此时一点问题没有

5.1.2 弄出点异常来

将数据改回去
springboot基本使用
人为设个异常
springboot基本使用
再次运行
springboot基本使用
查看数据库
springboot基本使用
现id为1的账户少了199.99,但是id为2的账户却没有收到转账的金额

5.1.3 添加事务注解

将数据改回去
springboot基本使用
事务注解
springboot基本使用
再次运行
springboot基本使用
查看数据库
springboot基本使用
这回转账正常了
去掉人为异常试下
springboot基本使用
再次运行
springboot基本使用
查看数据库
springboot基本使用

6. Springboot表单验证

新建数据库
springboot基本使用
新建maven项目,application.yml文件
springboot基本使用
Pom.xml文件与springboot-data类似
实体类
springboot基本使用
运行生成表t_student
Dao
springboot基本使用
service
springboot基本使用
springboot基本使用
Controller
springboot基本使用
前段
springboot基本使用
运行,测试1
springboot基本使用
测试2
springboot基本使用
查看数据库
springboot基本使用

7. Springboot之AOP

在第6章基础上,建切面类
springboot基本使用
springboot基本使用
运行测试
springboot基本使用

相关文章: