一、yaml文件格式:key-value形式;可以表示对象 集合

  1、语法:key:value 冒号后面必须跟一个空格再写value值

    key1:

      key2:

        key3:value

  2、属性取值:a、可以使用@Valu注解取值--@Value("${page.rows}")

          b、使用 ConfigurationProperties把属性的值批量绑定一个对象上

一、编写yaml格式文件,并配置数据库链接
#DB Configuration:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springboot
username: root
password: 169695
#JPA Configuration:
jpa:
database: mysql
show-sql: true
generate-ddl: true
page:
rows: 22
person:
name: 张无忌
age: 14
sex: 男
address: 光明顶
myAddress:
- "北京"
- "地球"
- "日本"
#myAddress: ["北京","地球","日本"]
二、编写person实体类;page:rows:22这个不用写实体类可以直接取直
package cn.zrf.entity; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; import java.util.List;
@Component
@ConfigurationProperties(prefix = "person")把属性的值批量绑定对象person上
public class Person {
private String name;
private String age;
private String sex;
private String address; private String[] myAddress;
// private List<String> myAddress; public String[] getMyAddress() {
return myAddress;
} public void setMyAddress(String[] myAddress) {
this.myAddress = myAddress;
} @Override
public String toString() {
return "Person{" +
"name=" + name + '\'' +
", age='" + age + '\'' +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} }
三、编写controller取出数据
@RestController
public class ShowUserController {
@Value("${page.rows}")
private String rows;
@Autowired
Person person;
//yml格式读取数据配置文件数据
@RequestMapping("/page/rows")
// @ResponseBody
public Map showRows(){
Map map = new HashMap();
map.put("rows",rows);
return map;
}
//yml格式配置文件读取(定义实体类法)
@RequestMapping("/person")
public Person showPerson(){
return person;
}
}

二、springBoot整合myBatis

  myBatis使用步骤:添加mybatis的起步依赖》》在配置文件中 配置数据源信息》》编写实体 类 、mapper接口、 mapper 映射文件》》手动配置mybatis包的扫描器:在启动类上加一个注解   @MapperScan(basePackages    = "cn.zrf.mapper")、还需要在pom.xml文件中添加build  标记和里面的内容》》编写controller

一、添加起步依赖在pom中
<!--mybatis 起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
二、插入build标记用来打包mapper接口的myBatis映射文件
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources> </build>
三、编写实体类并实现Serializable 序列化接口
package cn.zrf.entity; import java.io.Serializable; public class MyBatisUser implements Serializable {
private int id;
private String username;
private String sex; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} @Override
public String toString() {
return "MyBatisUser{" +
"id=" + id +
", username='" + username + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
四、编写myBatis的操作数据库接口(mapper包也就是原先的dao层)及映射文件
接口:
package cn.zrf.mapper; import cn.zrf.entity.MyBatisUser;
import org.springframework.stereotype.Repository; import java.util.List;
@Repository
public interface MyBatisUserMapper {
//查询所有
List<MyBatisUser> getUserList();
}
映射文件:sql语句
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.zrf.mapper.MyBatisUserMapper">
<select id="getUserList" resultType="cn.zrf.entity.MyBatisUser">
select * from user
</select>
</mapper>
五、编写controller
@Autowired
MyBatisUserMapper myBatisUserMapper;
//整合MyBatis查询所有
@RequestMapping("/mybatis/userList")
public List<MyBatisUser> myBatisUserList(){
List<MyBatisUser> userList = myBatisUserMapper.getUserList();
return userList;
}
六、编写启动器
package cn.zrf; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan(basePackages = "cn.zrf.mapper")
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class,args);
}
}

三、springBoot整合junit

  使用步骤:添加起步依赖》》创建一个测试类》》在测试类上添加注解:@SpringBootTest和@RunWith(SpringRunner.class)》》在测试类注入 需要使用的对象即可

一、添加起步依赖
<!--测试的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
二、实体类、操作数据库接口及配置文件使用上方的 service层写了个修改的方法也就是测试类内容,在测试类掉用直接修改;可在测试类直接书写
package cn.zrf.service; import cn.zrf.dao.UserDao;
import cn.zrf.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.Optional; @Service
public class UserService {
@Autowired
UserDao userDao; public void updateUsername(){
//根据ID查找用户;获得用户对象
Optional<User> optional = userDao.findById(2);
User user = optional.get();
//根据获得的用户对象修改姓名
user.setUsername("孙悟空");
userDao.save(user);
}
} 三、编写测试类
package cn.zrf; import cn.zrf.entity.MyBatisUser;
import cn.zrf.mapper.MyBatisUserMapper;
import cn.zrf.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import java.util.List; @RunWith(SpringRunner.class)
@SpringBootTest(classes = HelloApplication.class)
public class UserTest {
@Autowired
private MyBatisUserMapper myBatisUserMapper;
@Autowired
UserService userService; //查询所有
@Test
public void userListTest(){
List<MyBatisUser> userList = myBatisUserMapper.getUserList();
for (MyBatisUser myBatisUser:userList){
System.out.println(myBatisUser);
}
}
//修改
@Test
public void updateTest(){
userService.updateUsername();
}
}

四、springBoot使用技巧

  事物管理
​       在开启事物的方法上添加@Transactional  注解即可
​       如果springBoot版本是2.0以下
​       需要 在启动类上添加注解@EnableTransactionManagement

springBoot第二种配置文件yaml书写方式及读取数据、整合myBatis和整合junit的更多相关文章

  1. spring batch 以游标的方式 数据库读取数据 然后写入目标数据库

    前面关于Spring Batch的文章,讲述了SpringBatch对Flat.XML等文件的读写操作,本文将和大家一起讨论Spring Batch对DB的读写操作.Spring Batch对DB数据 ...

  2. Java开发学习(三十六)----SpringBoot三种配置文件解析

    一. 配置文件格式 我们现在启动服务器默认的端口号是 8080,访问路径可以书写为 http://localhost:8080/books/1 在线上环境我们还是希望将端口号改为 80,这样在访问的时 ...

  3. 10、一个action中处理多个方法的调用第二种方法method的方式

    在实际的项目中,经常采用现在的第二种方式在struct.xml中采用清单文件的方式 我们首先来看action package com.bjpowernode.struts2; import com.o ...

  4. springboot三种配置文件上传下载大小的配置

    配置文件为application.yml格式: spring: http: multipart: enabled: true max-file-size: 30MB max-request-size: ...

  5. React的第二种使用方法----脚手架方式

    一.React的第二种使用方法-----脚手架 1.前提:Node.js >8.10 2.下载全局脚手架工具 npm  i  -g  create-react-app 3.运行全局脚手架工具,创 ...

  6. SpringBoot几种定时任务的实现方式

    定时任务实现的几种方式: Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程序按照某一个频度执行, ...

  7. SpringBoot三种配置Dubbo的方式

    *必须首先导入dubbo-starter (1).使用SpringBoot配置文件(application.properties或application.yml) dubbo.application. ...

  8. SpringBoot几种定时任务的实现方式 和多线程执行任务

    定时任务实现的几种方式: Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程序按照某一个频度执行, ...

  9. 第二种BitBand操作的方式 - 让IDE来帮忙算地址

    要使用Bitband来訪问外设,一定要得出相应的映射地址.人工计算肯定是不靠谱的,并且也没人想这么干.因此能够通过Excel,拉个列表来计算.想想,这也是一个不错的招数.可是后来想想,还是嫌麻烦,毕竟 ...

随机推荐

  1. Springboot:员工管理之添加员工(十(7))

    构建员工添加请求 com\springboot\controller\EmployeeController.java /*调转到员工添加页 携带部门信息 restful风格*/ @GetMapping ...

  2. 三步教你如何在Github部署自己的简历

    相信铁子们有很多都是将找工作的小白(和小编一样!!嘿嘿)小编也和在座的大家一样,一个普通的不能再普通的二本学生(北华大学) <  单身!单身!单身!> 听很多人都说:像我们这个样子,害!放 ...

  3. 从Mac中完全删除office

    sudo sh -c "curl -s https://raw.githubusercontent.com/jimye/OfficeUninstall/master/office_unins ...

  4. thinkphp 5 一些常见问题

    ##  请求缓存 request_cache        

  5. Makefile: missing separator(did you mean TAB instead of 8 spaces?). Stop.

    通常我们会对vimrc文件加以配置(如将TAB键自动转换为4个空白键). 但正是由于将tab键转换为n个空白键,使得用vim编写的Makefile中不存在tab键(即“\t”)了.恰恰Makefile ...

  6. MAC攻击及缺陷

    MAC攻击及缺陷 MAC有好几种实现方式 对MAC的攻击 重放攻击 重放攻击的防护 密钥推测攻击 MAC算法的缺陷 第三方证明 防止否认 前面我们在讲HMAC的时候简单讲过了什么是MAC消息认证码. ...

  7. 徐州H

    #include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<=b;++i) #defi ...

  8. 记一次Pinpoint监控工具部署过程

    环境:Centos 7.4 X64IP:192.168.1.11 1.配置环境,先安装jdk 到Oracle官网下载安装JDK https://www.oracle.com/technetwork/j ...

  9. 对包含HttpContext.Current.Cache的代码进行单元测试

    假设我们如下代码调用了HttpContext.Current.Cache 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class CacheManager { ...

  10. 【万字长文】别再报班了,一篇文章带你入门Python

    本文始发于个人公众号:TechFlow,原创不易,求个关注 最近有许多小伙伴后台联系我,说目前想要学习Python,但是没有一份很好的资料入门.一方面的确现在市面上Python的资料过多,导致新手会不 ...