springBoot第二种配置文件yaml书写方式及读取数据、整合myBatis和整合junit
一、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使用技巧
springBoot第二种配置文件yaml书写方式及读取数据、整合myBatis和整合junit的更多相关文章
- spring batch 以游标的方式 数据库读取数据 然后写入目标数据库
前面关于Spring Batch的文章,讲述了SpringBatch对Flat.XML等文件的读写操作,本文将和大家一起讨论Spring Batch对DB的读写操作.Spring Batch对DB数据 ...
- Java开发学习(三十六)----SpringBoot三种配置文件解析
一. 配置文件格式 我们现在启动服务器默认的端口号是 8080,访问路径可以书写为 http://localhost:8080/books/1 在线上环境我们还是希望将端口号改为 80,这样在访问的时 ...
- 10、一个action中处理多个方法的调用第二种方法method的方式
在实际的项目中,经常采用现在的第二种方式在struct.xml中采用清单文件的方式 我们首先来看action package com.bjpowernode.struts2; import com.o ...
- springboot三种配置文件上传下载大小的配置
配置文件为application.yml格式: spring: http: multipart: enabled: true max-file-size: 30MB max-request-size: ...
- React的第二种使用方法----脚手架方式
一.React的第二种使用方法-----脚手架 1.前提:Node.js >8.10 2.下载全局脚手架工具 npm i -g create-react-app 3.运行全局脚手架工具,创 ...
- SpringBoot几种定时任务的实现方式
定时任务实现的几种方式: Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程序按照某一个频度执行, ...
- SpringBoot三种配置Dubbo的方式
*必须首先导入dubbo-starter (1).使用SpringBoot配置文件(application.properties或application.yml) dubbo.application. ...
- SpringBoot几种定时任务的实现方式 和多线程执行任务
定时任务实现的几种方式: Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程序按照某一个频度执行, ...
- 第二种BitBand操作的方式 - 让IDE来帮忙算地址
要使用Bitband来訪问外设,一定要得出相应的映射地址.人工计算肯定是不靠谱的,并且也没人想这么干.因此能够通过Excel,拉个列表来计算.想想,这也是一个不错的招数.可是后来想想,还是嫌麻烦,毕竟 ...
随机推荐
- 1. jquery插件手机
1. http://jqtjs.com/preview/demos/main/index.html#home2. jquery weUI ===== 插件:https://blog.csdn.net/ ...
- webstorm tslint配置
webstorm设置 settings >> TypeScript >> TSLint, 勾选 Enable ,选取 tslint包路径...npm\node_modules\ ...
- tensorflow1.0 构建lstm做图片分类
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #this is data mni ...
- qa问答机器人pysparnn问题的召回
""" 构造召回的模型 """ from sklearn.feature_extraction.text import TfidfVecto ...
- Java中Double保留小数位
1.能四舍五入 double d = 114.145; d = (double) Math.round(d * 100) / 100; System.out.println(d); 2. BigDec ...
- 科学计算包Numpy
Numpy 用于科学计算的python模块,提供了Python中没有的数组对象,支持N维数组运算.处理大型矩阵.成熟的广播函数库.矢量运算.线性代数.傅里叶变换以及随机数生成等功能,并可与C++.FO ...
- 敏捷与OKR实践(如何让OKR与敏捷计划共存)
僵化的详细长期计划(根据消耗的预算跟踪进度)正在敏捷组织中迅速成为对过去的褪色怀旧记忆,这由预测和非静态路线图代替.定期在这些可视化文件前聚会,您将能够学习.共享并触发重要的对话,解决依赖性并邀请服务 ...
- 13206抢票代码 py
抢票代码 https://github.com/Bingshuli/12306Python 谷歌驱动 http://chromedriver.storage.googleapis.com/index. ...
- mysql 之 函数
聚合函数 avg()函数 - 计算一组值或表达式的平均值. count()函数 - 计算表中的行数. instr()函数 - 返回子字符串在字符串中第一次出现的位置. sum()函数 - 计算一组值或 ...
- vue2.x学习笔记(三十二)
接着前面的内容:https://www.cnblogs.com/yanggb/p/12684060.html. 深入响应式原理 vue最独特的特性之一,是其非侵入式(耦合度低)的响应式系统:数据模型仅 ...