springboot入门使用
一、什么是springboot,有什么用
springboot是一个开发框架,其出现的目的利用约定大于配置的思想来让开发者摆脱spring繁琐的配置,简化开发。其不是spring框架的替代品,是spring框架的另外一种使用形式。
二、springboot的快速使用
1.登录到spring官网 找到如下页面,然后选择自己想要的开发环境和版本,
2.解压压缩包并导入到eclipse,就可以启动项目了,下面是程序的入口
package com.example.demo; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
三、springboot使用
1.配置文件properties的加载
#myconfig.properties配置文件
com.kyle.name=邓zm
com.kyle.age=27
/**
* 自定义配置文件的加载,需要加上下面的注解,并导入相关的包
* 如果是主配置文件application.properties加载,则只需要@Value就能获取数据
*/
@Component
@ConfigurationProperties
@PropertySource("classpath:myconfig.properties")
public class UserInfo { @Value("${com.kyle.name}")
private String name; @Value("${com.kyle.age}")
private int age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
}
2.springboot集成mybatis(只说和spring框架的差别)
<!-- 引入jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
#application.properties文件中引入配置文件和映射文件,
#没有配置文件也可以不引入
mybatis.config-locations=classpath:mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/*.xml
3.springboot集成redis集群
<!--引入相关jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
#redis集群配置 redis.properties
spring.redis.cluster.nodes=111.230.239.152:7000,111.230.239.152:7001,111.230.239.152:7002,111.230.239.152:8000,111.230.239.152:8001,111.230.239.152:8002
spring.redis.password=12345
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.timeout=1000
spring.redis.commandTimeout=5000
@Component
@Configuration
@PropertySource("classpath:redis.properties")
public class RedisConfig { /**
* 设置数据存入redis 的序列化方式
*</br>redisTemplate序列化默认使用的jdkSerializeable,存储二进制字节码,导致key会出现乱码,所以自定义
*序列化类
*/
@Bean
@SuppressWarnings({ "rawtypes", "unchecked" })
public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)throws UnknownHostException { RedisTemplate<Object,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer serializer =new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper =new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(objectMapper); redisTemplate.setValueSerializer(serializer);
redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate;
} }
@Service
public class UserServiceImpl implements UserService { @Autowired
private UserMapper userMapper; @Resource
private RedisTemplate<String,Object> redisTemplate; @Override
public User getUserInfoById(Long id) {
User user = (User)redisTemplate.opsForValue().get("user:"+id);
if(user == null ){
user = userMapper.getUserInfoById(id);
redisTemplate.opsForValue().set("user:"+id, user);
}
return user;
}
}
4.springboot开启定时任务
@MapperScan("com.example.demo.dao")
@SpringBootApplication
@EnableScheduling //开启定时任务
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@Component
public class SchedulerTask { private int count=0; @Scheduled(cron="*/1 * * * * ?")
private void process(){ System.out.println("this is scheduler task runing "+(count++));
} }
5.springboot的事务
@MapperScan("com.example.demo.dao")
@SpringBootApplication
@EnableScheduling
@EnableTransactionManagement //事务管理
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@Override
@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
public void updateUser(User user) { User user1 = new User();
user1.setUserId(1);
user1.setName("yao1"); User user2 = new User();
user2.setUserId(2);
user2.setName("Xiaoxiao1"); userMapper.updateUser(user1);
int i = 1/0;
userMapper.updateUser(user2);
}
springboot入门使用的更多相关文章
- SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1
在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...
- SpringBoot入门基础
目录 SpringBoot入门 (一) HelloWorld. 2 一 什么是springboot 1 二 入门实例... 1 SpringBoot入门 (二) 属性文件读取... 16 一 自定义属 ...
- SpringBoot入门示例
SpringBoot入门Demo SpringBoot可以说是Spring的简化版.配置简单.使用方便.主要有以下几种特点: 创建独立的Spring应用程序 嵌入的Tomcat,无需部署WAR文件 简 ...
- Spring全家桶系列–[SpringBoot入门到跑路]
//本文作者:cuifuan Spring全家桶————[SpringBoot入门到跑路] 对于之前的Spring框架的使用,各种配置文件XML.properties一旦出错之后错误难寻,这也是为什么 ...
- springboot入门之一:环境搭建(续)
在上篇博客中从springboot的入门到运行一个springboot项目进行了简单讲述,详情请查看“springboot入门之一”.下面继续对springboot做讲述. 开发springboot测 ...
- 【Java】SpringBoot入门学习及基本使用
SpringBoot入门及基本使用 SpringBoot的介绍我就不多说了,核心的就是"约定大于配置",接下来直接上干货吧! 本文的实例: github-LPCloud,欢迎sta ...
- SpringBoot入门(三)——入口类解析
本文来自网易云社区 上一篇介绍了起步依赖,这篇我们先来看下SpringBoot项目是如何启动的. 入口类 再次观察工程的Maven配置文件,可以看到工程的默认打包方式是jar格式的. <pack ...
- SpringBoot入门(五)——自定义配置
本文来自网易云社区 大部分比萨店也提供某种形式的自动配置.你可以点荤比萨.素比萨.香辣意大利比萨,或者是自动配置比萨中的极品--至尊比萨.在下单时,你并没有指定具体的辅料,你所点的比萨种类决定了所用的 ...
- SpringBoot入门(四)——自动配置
本文来自网易云社区 SpringBoot之所以能够快速构建项目,得益于它的2个新特性,一个是起步依赖前面已经介绍过,另外一个则是自动配置.起步依赖用于降低项目依赖的复杂度,自动配置负责减少人工配置的工 ...
- SpringBoot入门(二)——起步依赖
本文来自网易云社区 在前一篇我们通过简单几步操作就生成了一个可以直接运行的Web程序,这是因为SpringBoot代替我们做了许多工作,概括来讲可以分为起步依赖和自动配置.这一篇先来看看起步依赖. 项 ...
随机推荐
- Android 永久保存简单数据
转载: http://blog.csdn.net/xzlawin/article/details/45959033 方法1: 存数据: SharedPreferences userInfo = thi ...
- Codeforces1023F Mobile Phone Network 【并查集】【最小生成树】
题目大意: 给一些没安排权值的边和安排了权值的边,没被安排的边全要被选入最小生成树,问你最大能把它们的权值和安排成多少.题目分析:假设建好了树,那么树边与剩下的每一条边都能构成一个环,并且非树边的权值 ...
- [洛谷P2627] 修剪草坪
传送门:>Here< 题意:不能有连续超过$k$个奶牛的一段,求最大的和 思路分析 Dp还是容易看出来的. 我的第一感觉是一维,$f[i]$表示前i头奶牛的最大效率.其实这也是可以解的,具 ...
- Going Home POJ - 2195(费用流)
就是一个简单题 四个月前a的一道题,今天又看到了,再a一遍吧. 好吧 我想多了 用了bfs求最短路 其实不用的 因为没有障碍物 #include <iostream> #include ...
- 【BZOJ4325】【NOIP2015】斗地主 搜索
题目描述 就是给你一副牌,问你最少几次能出完. 详细规则见规则 \(n\leq 23\) 题解 NOIP的数据非常水,错误一大堆的程序都能AC. 因为顺子对答案的影响最大,所以先枚举顺子进行搜索. 接 ...
- MySQL 练习题 附答案,未完
综合练习题 表结构 整合一下方便查看 teacher student course scors 练习题 1.自行创建测试数据 create table student( sid int prima ...
- PHP获取网络图片并保存在本地目录
PHP获取网络图片并保存在本地目录思路: 代码如下: function file_exists_S3($url) { $state = @file_get_contents($url,0,null,0 ...
- 脚本监控web服务器,工作不正常发邮件提醒
背景介绍公司有多个web网站,没有配置监控服务,每天都需要定时检查服务器是否工作正常.低效耗时. 代码片段 #!/bin/bash # Author Jerry.huang (Email:Jerry. ...
- 简单聊聊WebSocket
一.概述 上一篇文章<浅析一次HTTP请求>我们分析了简单的一次 HTTP 请求具体是怎么样完成的,分析了 HTTP 协议的数据结构,如何连接,如何断开,又是如何多路复用的,那么今天我们来 ...
- 【arc073e】Ball Coloring(线段树,贪心)
[arc073e]Ball Coloring(线段树,贪心) 题面 AtCoder 洛谷 题解 大型翻车现场,菊队完美压中男神的模拟题 首先钦定全局最小值为红色,剩下的袋子按照其中较大值排序. 枚举前 ...