- 交流或更多内容请关注我的公众号:nezha_blog
- 我的技术博客:
1. Spring Boot学习笔记--全注解方式
2. pring boot中文帮助文档
---说明:本文档翻译的版本:1.4.1.RELEASE。
3. 常用注解
@RestController
返回json形式的结果,便于前后端分离。是@ResponseBody 和 @Controller的组合体@RequestMapping
配置url映射@EnableAutoConfiguration
@Configuration
@ComponentScan
@SpringBootApplication
很多Spring Boot开发者总是使用 @Configuration
, @EnableAutoConfiguration
和 @ComponentScan
注解他们的main类。由于这些注解被如此频繁地一块使用(特别是你遵循以上最佳实践时),Spring Boot提供一个方便的 @SpringBootApplication
选择。
该 @SpringBootApplication
注解等价于以默认属性使用 @Configuration
, @EnableAutoConfiguration
和 @ComponentScan
。
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScanpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}复制代码
@ConfigurationProperties
属性注入,prefix指代注入的配置来自connection的对象
@Component@ConfigurationProperties(prefix="connection")public class ConnectionSettings { private String username; private InetAddress remoteAddress; // ... getters and setters}复制代码
connection: name: 赵武 age: 18 job: Java研发工程师复制代码
为了使用@ConfigurationProperties
beans,你可以使用与其他任何bean相同的方式注入它们
@Servicepublic class MyService { @Autowired private ConnectionSettings connection; //... @PostConstruct public void openConnection() { Server server = new Server(); this.connection.configure(server); }}复制代码
@EnableConfigurationProperties
@Component
和@Bean
@Bean
主要被用在方法上,来显式声明要用生成的类@Profiles
Spring Profiles提供了一种隔离应用程序配置的方式,并让这些配置只能在特定的环境下生效。
spring: profiles: active: dev复制代码
@Value
用于获取配置文件下的配置项
people: name: 赵武 age: 18 job: Java研发工程师复制代码
@Value("${people.name}") private String name;复制代码
@Controller
@PathVariable
,@RequestParam
@GetMapping
,@PostMapping
:Get 或Post方式的请求,组合模式
@PathVariable
的使用,获取请求参数
@RequestMapping(value="/hello/{id}",method = RequestMethod.GET)public String sayhello(@PathVariable("id")Integer myid){ return "id:"+myid;}复制代码
@RequestParam
的使用,获取传统方式的参数
@RequestMapping(value="/hi",method = RequestMethod.GET)public String sayhi(@RequestParam(value = "id",required = false,defaultValue = "100")Integer myid){ return "id:"+myid;}复制代码
4. spring data JPA -- 单数据源
具体的实现代码demo:
定义了对象持久化的标准,主要是对Hibernate的整合
现阶段发现和mybatis的直观操作很一致,都是可能集中管理数据库连接与释放,JPA想比较于mybatis可以自动建表,不知道算不算一种优势,在我看来不算是。毕竟表结构基本上数据库工程师都搞定了的事。
1.加入依赖
org.springframework.data spring-boot-starter-data-jpa 复制代码 mysql mysql-connector-java
2.配置数据库和JPA
ddl-auto:创建的方式
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/coder username: root password: root jpa: hibernate: ddl-auto: create show-sql: true复制代码
3.创建Mapper对象
@Entity: 持久化实例
@Table: 自定义表的名称
@Entity//@Table(name = "programmer")public class Coder { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; private String name; private Integer age; public Coder(){ } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}复制代码
4.集成JpaRepository
主要是基于Hibernate的
public interface CoderRepository extends JpaRepository{ //这个是扩展开来的查询方式 public List findByAge(Integer age);}复制代码
5.实现一个CoderController,实现增删改查。
@RestControllerpublic class CoderController { @Autowired private CoderRepository coderRepository; //1.Get方式请求,查询所有程序员信息 @GetMapping(value = "/coders") public ListcoderList(){ return coderRepository.findAll(); } //2.Post方式,增加程序员 @PostMapping(value = "/coders") public Coder CoderAdd(@RequestParam("name")String name,@RequestParam("age")Integer age){ Coder coder = new Coder(); coder.setAge(age); coder.setName(name); return coderRepository.save(coder); } //3.通过id查询一个人 @GetMapping(value = "/coders/{id}") public Coder CoderFindOne(@PathVariable("id")Integer id){ return coderRepository.findOne(id); } //4.通过id删除一个人 @DeleteMapping(value = "/coders/{id}") public void CoderDelete(@PathVariable("id")Integer id){ coderRepository.delete(id); } //5.更新一个人,用Postman模拟的时候注意:用x-www-form-urlencoded @PutMapping(value = "/coders/{id}") public Coder CoderUpdateOne(@PathVariable("id")Integer id, @RequestParam("name") String name, @RequestParam("age")Integer age){ Coder coder = new Coder(); coder.setId(id); coder.setName(name); coder.setAge(age); return coderRepository.save(coder); } //6.扩展Jpa接口,按照年龄查找 @GetMapping(value = "/coder/age/{age}") public List CoderFindAll(@PathVariable("age")Integer age){ return coderRepository.findByAge(age); }}复制代码
6.实现mysql的事务
- 首先新建一个Service类:CoderService
@Servicepublic class CoderService { @Autowired CoderRepository coderRepository; @Transactional public void insertTwo(){ Coder coderA = new Coder(); coderA.setAge(101); coderA.setName("1"); coderRepository.save(coderA); Coder coderB = new Coder(); coderB.setAge(102); coderB.setName("102"); coderRepository.save(coderB); }}复制代码
- 在CoderController中自动载入coderService
@Autowiredprivate CoderService coderService;复制代码
- 在CoderController调用service。
//7.使用事务,同时插入两个人的数据@PostMapping(value = "coder/two")public void coderTwo(){ coderService.insertTwo();}复制代码
7.使用@Query
实现自定义sql查询
- 在
CoderRepository
实现下面代码
@Query("select p from Coder p where p.id = (select max(p2.id) from Coder p2)") Coder getMaxIdCoder();复制代码
- 在
CoderController
中使用getMaxIdCoder
方法
//8.自定义sql语句查询@GetMapping(value = "/coder/find")public Coder CoderFindByTask(){ return coderRepository.getMaxIdCoder();}复制代码
5. Spring Boot MyBatis -- 单数据源
基于注解方式的Mybatis其实和JPA很类似,不过mybatis不提供自动创建表的操作。这点上jpa更好些。
我的demo程序,在我的github上:
引用博客: --简书 FlySheep_ly
程序员DD:
[Spring Boot中使用MyBatis注解配置详解](http://blog.didispace.com/mybatisinfo/)复制代码
1.引入依赖
org.mybatis.spring.boot mybatis-spring-boot-starter 1.2.0 复制代码 mysql mysql-connector-java 5.1.41
2.application.properties中配置mysql的连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/test01spring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driver复制代码
3.创建映射对象User
关于序列化的实现,最好还是实现一下。
import java.io.Serializable;public class User implements Serializable{ private static final long serialVersionUID = -5554561712056198940L; private Long id; private String name; private Integer age; public User(){ } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}复制代码
4.创建User映射的操作UserMapper
关于Mapper的更多操作,请参考mybatis官网
/** * Created by nezha on 2017/4/26. */@Mapperpublic interface UserMapper { /** * 添加操作,返回新增元素的 ID * @param User */ @Insert("insert into person(name,age) values(#{name},#{age})") @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id") void insert(User user); /** * 查询所有 * @return */ @Select("select id,name,age from person") ListselectAll(); @Select("SELECT * FROM USER WHERE NAME = #{name}") User findByName(@Param("name") String name);}复制代码
5.调用测试
发现Restful风格真是好习惯,很好用很实用。
@EnableTransactionManagement@RestControllerpublic class TestController { @Autowired private UserMapper userMapper; @GetMapping(value = "/test/{name}") public User findOne(@PathVariable("name")String name){ return userMapper.findByName(name); }}复制代码
可能出现的问题:
mybatis+spring boot, mapper 提示Could not autowire. 一直提示不能加载
- 解决方案
修改idea配置,将spring 的severity的值设置为"warning", 如下:
如果想进一步缩小修改范围的话:
Alt + Enter quick fix or change settingsSettings - Editor - Inspections - Spring - Spring Core - Code - Autowiring for Bean Class - warning复制代码
关于多源配置的问题:
1.
2.
6. Spring Boot RabbitMQ
我的demo程序:
--这篇文章写得不错,关于RabbitMQ的三种Exchange方式讲的很好。不过代码不是很简洁。
--
安装与配置
RabbitMQ的三种Exchange方式
1.Direct Exchange
如果 routing key 匹配, 那么Message就会被传递到相应的queue中。其实在queue创建时,它会自动的以queue的名字作为routing key来绑定那个exchange。
2.Fanout Exchange
只需要简单的将队列绑定到交换机上。一个发送到交换机的消息都会被转发到与该交换机绑定的所有队列上。很像子网广播,每台子网内的主机都获得了一份复制的消息。Fanout交换机转发消息是最快的。
3.Topic Exchange
将路由键和某模式进行匹配。此时队列需要绑定要一个模式上。符号“#”匹配一个或多个词,符号“”匹配不多不少一个词。因此“audit.#”能够匹配到“audit.irs.corporate”,但是“audit.”
实例讲解
三种方式最主要的文件就三个:
1.sender的配置
2.receiver的配置
3.rabbitConfig的配置
下面,我们通过在Spring Boot应用中整合RabbitMQ,并实现一个简单的发送、接收消息的例子来对RabbitMQ有一个直观的感受和理解。
在Spring Boot中整合RabbitMQ是一件非常容易的事,因为之前我们已经介绍过Starter POMs,其中的AMQP模块就可以很好的支持RabbitMQ,下面我们就来详细说说整合过程:
1.pom依赖引入
复制代码 org.springframework.boot spring-boot-starter-amqp
2.连接配置
spring.application.name=rabbitmq-hellospring.rabbitmq.host=localhostspring.rabbitmq.port=5672spring.rabbitmq.username=guestspring.rabbitmq.password=guest复制代码
3.创建消息生产者Sender
创建消息生产者Sender。通过注入AmqpTemplate接口的实例来实现消息的发送,AmqpTemplate接口定义了一套针对AMQP协议的基础操作。在Spring Boot中会根据配置来注入其具体实现。在该生产者,我们会产生一个字符串,并发送到名为hello的队列中。
@Componentpublic class Sender { @Autowired AmqpTemplate amqpTemplate; public void send() { String context = "hello " + new Date(); System.out.println("Sender : " + context); this.amqpTemplate.convertAndSend("hello", context); }}复制代码
4.创建消息消费者Receiver
创建消息消费者Receiver。通过@RabbitListener注解定义该类对hello队列的监听,并用@RabbitHandler注解来指定对消息的处理方法。所以,该消费者实现了对hello队列的消费,消费操作为输出消息的字符串内容。
@Component@RabbitListener(queues = "hello")public class Receiver { @RabbitHandler public void process(String hello) { System.out.println("Receiver1 : " + hello); }}复制代码
5.创建RabbitMQ的配置类RabbitConfig
创建RabbitMQ的配置类RabbitConfig,用来配置队列、交换器、路由等高级信息。这里我们以入门为主,先以最小化的配置来定义,以完成一个基本的生产和消费过程。
@Configurationpublic class RabbitConfig { //1.配置一个名为hello的一对一的消息队列 @Bean public Queue helloQueue() { return new Queue("hello"); }}复制代码
5.单元测试:
@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = HelloApplication.class)public class HelloApplicationTests { @Autowired private Sender sender; @Test public void hello() throws Exception { sender.send(); }}复制代码
7. Spring Boot mongodb
spring boot mongodb 的demo程序:
安装与配置
- mongodb的安装与配置:
- 用户管理参考:
- Mac下的mongodb安装与配置:
1、进入mongodb的shell : mongo
2、切换数据库: use admin
3、添加用户,指定用户的角色和数据库:
db.createUser( { user: "admin", customData:{description:"superuser"}, pwd: "admin", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } ) user字段,为新用户的名字;pwd字段,用户的密码;cusomData字段,为任意内容,例如可以为用户全名介绍;roles字段,指定用户的角色,可以用一个空数组给新用户设定空角色。在roles字段,可以指定内置角色和用户定义的角色。复制代码
4、查看创建的用户 : show users 或 db.system.users.find()
5、启用用户权限:
修改配置文件,增加配置:
$ vim /etc/mongod.confsecurity: authorization: enabled复制代码
6.重新启动mongodb
sudo service mongod restart复制代码
7.使用用户管理账户登录认证
use admindb.auth('admin', 'admin')复制代码
8.远程登陆
mongo 123.xxx.xxx.xxx:27017/amdin -uadmin -padmin复制代码
第一个admin:指代数据库 第二个admin:指代用户名 第三个admin:指代密码
spring boot 集成 mongodb
1.引入依赖
复制代码 org.springframework.boot spring-boot-starter-data-mongodb
2.创建存储的实体
创建要存储的User实体,包含属性:id、username、age
public class User { @Id private String id; private String username; private Integer age; public User(String username, Integer age) {// this.id = id; this.username = username; this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "username:"+username+"--age:"+age; }}复制代码
3.实现User的数据访问对象
实现User的数据访问对象:UserRepository
public interface UserRepository extends MongoRepository{ User findByUsername(String username);}复制代码
4.配置mongodb的连接
#mongodb3.X的配置spring.data.mongodb.uri=mongodb://admin:admin@123.206.xxx.xxx:27017/test#mongodb2.x的配置spring.data.mongodb.host=localhost spring.data.mongodb.port=27017复制代码
5.在单元测试中调用
@RunWith(SpringRunner.class)@SpringBootTestpublic class Test05ApplicationTests{ @Autowired private UserRepository userRepository; @Test public void test() { userRepository.deleteAll(); // 创建三个User,并验证User总数 userRepository.save(new User("didi", 30)); userRepository.save(new User("mama", 40)); userRepository.save(new User("kaka", 50)); Assert.assertEquals(3, userRepository.findAll().size()); // 删除一个User,再验证User总数 User u = userRepository.findByUsername("didi"); System.out.println(u.toString()); userRepository.delete(u); Assert.assertEquals(2, userRepository.findAll().size()); }}复制代码
出现的问题
1.发现运行成功,但是查不到数据
主要是自己理解错误,mongodb数据库下还有collection(相当于表).然后针对每一个Database下可能有多个collection
8. Spring Boot redis
redis的demo程序:
- redis的配置:
- redis开启远程连接
- redis远程连接
redis-cli -h 123.206.xxx.xxx -p 6379
1.引入依赖
复制代码 org.springframework.boot spring-boot-starter-redis
2.参数配置
# REDIS (RedisProperties)# Redis数据库索引(默认为0)spring.redis.database=0# Redis服务器地址spring.redis.host=123.206.xxx.xxx# Redis服务器连接端口spring.redis.port=6379# Redis服务器连接密码(默认为空)spring.redis.password=# 连接池最大连接数(使用负值表示没有限制)spring.redis.pool.max-active=-1# 连接池最大阻塞等待时间(使用负值表示没有限制)spring.redis.pool.max-wait=-1# 连接池中的最大空闲连接spring.redis.pool.max-idle=16# 连接池中的最小空闲连接spring.redis.pool.min-idle=0# 连接超时时间(毫秒)spring.redis.timeout=0复制代码
3.测试访问
@RunWith(SpringRunner.class)@SpringBootTestpublic class Test04ApplicationTests { @Autowired StringRedisTemplate stringRedisTemplate; @Test public void test() throws Exception { // 保存字符串 stringRedisTemplate.opsForValue().set("aaa", "111"); Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa")); }}复制代码
9. Spring Boot定时任务
定时任务demo程序:
1.pom包配置
这部分基本的spring boot启动项就行,没有什么特别的依赖包
2.启动类启用定时
@EnableScheduling@SpringBootApplicationpublic class ScheduleApplication { public static void main(String[] args) { SpringApplication.run(ScheduleApplication.class, args); }}复制代码
3.创建定时任务实现类
定时任务一
/** * Created by nezha on 2017/4/28. */@Componentpublic class SchedulerTask { private int count = 0; @Scheduled(cron="*/6 * * * * ?") private void process(){ System.out.println("this is scheduler task runing "+(count++)); }}复制代码
定时任务二
/** * Created by nezha on 2017/4/28. */@Componentpublic class SchedulerTask2 { private static SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 6000) public void reportCurrentTime() { System.out.println("现在时间:" + dateFormat.format(new Date())); }}复制代码
4.参数说明
@Scheduled
参数可以接受两种定时的设置,一种是我们常用的cron="*/6 * * * * ?"
,一种是 fixedRate = 6000
,两种都表示每隔六秒打印一下内容。
fixedRate 说明
@Scheduled(fixedRate = 6000)
:上一次开始执行时间点之后6秒再执行@Scheduled(fixedDelay = 6000)
:上一次执行完毕时间点之后6秒再执行@Scheduled(initialDelay=1000, fixedRate=6000)
:第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次
10. Spring Boot相关技术
自定义图标
---自定义图标
部署spring项目
1.IntelliJ Idea中直接运行
2.mvn spring-boot:run
3.mvn install >>> 会生成jar文件,执行jar文件。
spring boot测试的时候,怎么单元测试
1.使用@SpringBootTest
文件主要实在test目录下
@RunWith(SpringRunner.class)@SpringBootTestpublic class Test05ApplicationTests{ @Autowired private UserRepository userRepository; @Test public void test() { userRepository.deleteAll(); // 创建三个User,并验证User总数 userRepository.save(new User("didi", 30)); userRepository.save(new User("mama", 40)); userRepository.save(new User("kaka", 50)); }}复制代码
2.使用@SpringBootApplication
在主目录下com.nezha/
@SpringBootApplicationpublic class Application implements CommandLineRunner { @Autowired private CustomerRepository repository; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) throws Exception { repository.deleteAll(); // save a couple of customers repository.save(new Customer("Alice", "Smith")); repository.save(new Customer("Bob", "Smith")); }}复制代码
3.使用RestController
这种方式很方便,很好用,有些时候我会用。