一.简介
springboot是什么?
用来进行框架整合
使用自动配置、整合依赖、嵌入式的容器
如何使用?
引入SpringBoot提供的依赖,只需要做很少的整合配置
SpringBoot的依赖以 spring-boot-starter 开头
父依赖:spring-boot-starter-parent ,作用:控制引入依赖包的版本
如果不知道依赖怎么写,可以到maven的仓库搜索
maven仓库地址:https://mvnrepository.com
springboot和spring的区别,以及解决了那些问题?
二.入门demo
1.搭建环境
创建工程,引入依赖
<!-- 父依赖 引入父依赖后,不会导入jar包,因为父依赖是POM文件-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<!--web服务依赖 引入web服务依赖后,会把Spring相关包引入-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在包的根目录下创建启动类
//在类上使用注解,:@SpringBootApplication(重点)
//创建main方法,并在main方法里使用:
//写本类的字节码【为的就是找到启动类上的注解】
//run方法里面初始化Spring框架
@SpringBootApplication
public class SpringBootRunner {
public static void main(String[] args) {
SpringApplication.run(SpringBootRunner.class,args);
}
}
运行,查询是否有错页面
这个就是springboot启动成功了
我们可以编写一个controller 类进行测试
@RestController
public class HelloController {
@RequestMapping("/hello")
public String helloworld(){
return "helloworld";
}
}
启动并访问
三.整合
1.springboot整合spring data jpa
需求:
使用 Spring Boot + Spring MVC + Spring Data JPA + EasyUI 框架组合实现部门列表查询,效果如下
编写持久类:
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "user_name")
private String userName;
private String password;
private String name;
编写UserDao的接口,并继承JpaResponry
@Repository
public interface IUserDao extends JpaRepository<User,Long> {
}
编写service层代码,和controller 层代码,然后编写application.properties文件,主要是存放数据库等连接信息
#DB Configuration:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot
spring.datasource.username=root
spring.datasource.password=root
#JPA Configuration:
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
添加easy-ui框架的静态文件进行测试
2.springboot整合mybatis
导入包
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
编写Mapper接口
@Mapper
public interface UserMapper {
@Select("select * from user where name like '%${value}%'")
public List<User> findByName(String name);
}
注意:Mapper是声明接口
编写controller 层内容,通过restful风格进行测试
//模糊查询姓名
@RequestMapping("list/{name}")
public List<User> findByName(@PathVariable String name){
List<User> userList = userService.findByName(name);
return userList;
}
3.spring boot 整合redis
导入包
<!--导入redis-->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
我们接着上面编写的两个方法进行测试,用缓存的注解
@Override
@Cacheable(value = "user" ,key = "'userName'")
public List<User> findAll() {
System.out.println("从mysql数据库中查");
List<User> list = userDao.findAll();
return list;
}
@Override
@CacheEvict(value = "user",key = "'userName'",allEntries = true)
public List<User> findByName(String name) {
System.out.println("缓存清理了");
return userMapper.findByName(name);
}
注意:key的命名规则 redis中的key 就是 value+":"+key
比如:
4.springboot整合junit
导包
<!--junit-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes =SpringBootRunner.class)
public class test {
@Autowired
private UserService userService;
@Test
public void findAll(){
List<User> list = userService.findAll();
for (User user : list) {
System.out.println(user.getName());
}
}
}
注意;springBootTest这个注解就是加载spring容器 获得ioc等一切信息
另一种配置形式:
注意:必须以application开头
四.springboot工程打包方式
在pom文件中添加如下:如果不添加则就是报错,找不到main方法
打包,我门通过java -jar 包名 就是直接加到jvm内从中
还有另一种就是把application.yml的配置文件放到我们jar的同级目录,默认是从外开始找配置文件,如果没有找到则是找jar包里面的文件