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代替我们做了许多工作,概括来讲可以分为起步依赖和自动配置.这一篇先来看看起步依赖. 项 ...
随机推荐
- 修改iptables后重启返回错误
在防火墙添加规则后我是这样改的vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ...
- myclipse里有感叹号的问题,希望可以帮到各位
今天,我在myeclipse中导入一个项目的时候就发现一个问题:项目有红色感叹号,并且项目有红色错误提示.我首先看了这个红色错误提示的地方,发现这个根本不应该报错,想必是这个红色感叹号的原因. 于 ...
- Sublime Text3 如何开启Debug
打开setting-user 首选项——>Package Settings——>Package Control——>settings-user 添加"debug" ...
- Bomb Game HDU - 3622(二分最小值最大化)
题意: 就是给出n对坐标,每对只能选一个,以选出来的点为圆心,半径自定义,画圆,而这些圆不能覆盖,求半径最小的圆的最大值 解析: 看到最x值最x化,那二分变为判定性问题,然后...然后我就没想到... ...
- 【XSY2772】数列 特征多项式 数学
题目描述 给你一个数列: \[ f_n=\begin{cases} a^n&1\leq n\leq k\\ \sum_{i=1}^k(a-1)f_{n-i}&n>k \end{c ...
- Java将Excel中科学计数法解析成数字
需要注意的是一般的科学表达式是1.8E12 1.8E-12 而在Excel中的科学表达式是1.8E+12 1.8E-12 我写的科学计数法的正则表达式是(-?\d+\.?\d*)[Ee]{1}[\+- ...
- 从快感到成就感:多巴胺vs内啡肽
从快感到成就感:多巴胺vs内啡肽 来源 https://zhuanlan.zhihu.com/p/24697188 作者:朱良 编辑于 2017-06-20 努力不一定成功,但不努力一定 ...
- phpcms 手机门户配置注意事项
设置域名解析后,服务器apache,iis,nginx等,设置虚拟服务器时, 如下,只设置index.php为默认入口文件: 默认pc站为index.html为默认访问文件! pc与wap站,绑定目录 ...
- QML 用QSortFilterProxyModel实现搜索功能
搞了一晚上终于实现了,写个博客纪念一下吧. c++部分的代码: #include <QQmlApplicationEngine> #include <QQmlContext> ...
- k短路模板(洛谷P2483 [SDOI2010]魔法猪学院)(k短路,最短路,左偏树,priority_queue)
你谷数据够强了,以前的A*应该差不多死掉了. 所以,小伙伴们快来一起把YL顶上去把!戳这里! 俞鼎力的课件 需要掌握的内容: Dijkstra构建最短路径树. 可持久化堆(使用左偏树,因其有二叉树结构 ...