一、什么是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入门使用的更多相关文章

  1. SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1

    在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...

  2. SpringBoot入门基础

    目录 SpringBoot入门 (一) HelloWorld. 2 一 什么是springboot 1 二 入门实例... 1 SpringBoot入门 (二) 属性文件读取... 16 一 自定义属 ...

  3. SpringBoot入门示例

    SpringBoot入门Demo SpringBoot可以说是Spring的简化版.配置简单.使用方便.主要有以下几种特点: 创建独立的Spring应用程序 嵌入的Tomcat,无需部署WAR文件 简 ...

  4. Spring全家桶系列–[SpringBoot入门到跑路]

    //本文作者:cuifuan Spring全家桶————[SpringBoot入门到跑路] 对于之前的Spring框架的使用,各种配置文件XML.properties一旦出错之后错误难寻,这也是为什么 ...

  5. springboot入门之一:环境搭建(续)

    在上篇博客中从springboot的入门到运行一个springboot项目进行了简单讲述,详情请查看“springboot入门之一”.下面继续对springboot做讲述. 开发springboot测 ...

  6. 【Java】SpringBoot入门学习及基本使用

    SpringBoot入门及基本使用 SpringBoot的介绍我就不多说了,核心的就是"约定大于配置",接下来直接上干货吧! 本文的实例: github-LPCloud,欢迎sta ...

  7. SpringBoot入门(三)——入口类解析

    本文来自网易云社区 上一篇介绍了起步依赖,这篇我们先来看下SpringBoot项目是如何启动的. 入口类 再次观察工程的Maven配置文件,可以看到工程的默认打包方式是jar格式的. <pack ...

  8. SpringBoot入门(五)——自定义配置

    本文来自网易云社区 大部分比萨店也提供某种形式的自动配置.你可以点荤比萨.素比萨.香辣意大利比萨,或者是自动配置比萨中的极品--至尊比萨.在下单时,你并没有指定具体的辅料,你所点的比萨种类决定了所用的 ...

  9. SpringBoot入门(四)——自动配置

    本文来自网易云社区 SpringBoot之所以能够快速构建项目,得益于它的2个新特性,一个是起步依赖前面已经介绍过,另外一个则是自动配置.起步依赖用于降低项目依赖的复杂度,自动配置负责减少人工配置的工 ...

  10. SpringBoot入门(二)——起步依赖

    本文来自网易云社区 在前一篇我们通过简单几步操作就生成了一个可以直接运行的Web程序,这是因为SpringBoot代替我们做了许多工作,概括来讲可以分为起步依赖和自动配置.这一篇先来看看起步依赖. 项 ...

随机推荐

  1. P1035 调和级数

    两种解法如下: 1.模拟 这种做法的思路是枚举n从1开始,直到Sn>k结束,只需要一个循环即可实现. 代码: #include<cstdio> int main() { ; scan ...

  2. springMVC整理01--搭建普通的工程

    1. Spring MVC 简介Spring MVC 是 Spring 基于 MVC 设计理念提供的一个表现层的 Web 框架.是目前主流的MVC 框架之一.Spring MVC 通过一套 MVC 注 ...

  3. CH0805 防线(算竞进阶习题)

    二分 一道藏的很深的二分题... 题目保证只有一个点有奇数个防具,这个是突破口. 因为 奇数+偶数=偶数,我们假设某个点x,如果有奇数点的防具在x的左边,那么x的左边的防具总数一定是奇数,右边就是偶数 ...

  4. 用贝叶斯定理解决三门问题并用Python进行模拟(Bayes' Rule Monty Hall Problem Simulation Python)

    三门问题(Monty Hall problem)也称为蒙提霍尔问题或蒙提霍尔悖论,出自美国的电视游戏节目<Let’s Make a Deal>.问题名字来自该节目的主持人蒙提·霍尔(Mon ...

  5. 【XSY2519】神经元 prufer序列 DP

    题目描述 有\(n\)点,每个点有度数限制,\(\forall i(1\leq i\leq n)\),让你选出\(i\)个点,再构造一棵生成树,要求每个点的度数不超过度数限制.问你有多少种方案. \( ...

  6. Day24-ModelForm操作及验证

    Day23内容回顾--缺失,遗憾成狗. 一:Model(2个功能) -数据库操作: -验证,只有一个clean方法可以作为钩子. 二:Form(专门来做验证的)--------根据form里面写的类, ...

  7. 【 HDU4773 】Problem of Apollonius (圆的反演)

    BUPT2017 wintertraining(15) #5G HDU - 4773 - 2013 Asia Hangzhou Regional Contest problem D 题意 给定两个相离 ...

  8. 【BZOJ1023】仙人掌图(仙人掌,动态规划)

    [BZOJ1023]仙人掌图(仙人掌,动态规划) 题面 BZOJ 求仙人掌的直径(两点之间最短路径最大值) 题解 一开始看错题了,以为是求仙人掌中的最长路径... 后来发现看错题了一下就改过来了.. ...

  9. default.conf

    1./etc/nginx/conf.d/ 下设置一个 default.conf,server_name 设置为 localhost,如果有其他非法域名 A 记录到该机器上,则返回默认的 Nginx 页 ...

  10. 分离式部署LNMP

    -------Nginx----------PHP+NFS------------MySql------192.168.56.202 192.168.56.201 192.168.56.200安装My ...