springBoot第⼆种配置⽂件yaml书写⽅式及读取数据、整合
myBatis和整合junit
⼀、yaml⽂件格式:key-value形式;可以表⽰对象集合
1、语法:key:value 冒号后⾯必须跟⼀个空格再写value值
key1:
key2:
key3:value
2、属性取值:a、可以使⽤@Valu注解取值--@Value("${ws}")
b、使⽤ ConfigurationProperties把属性的值批量绑定⼀个对象上
⼀、编写yaml格式⽂件,并配置数据库链接
#DB Configuration:
spring:
datasource:
driver-class-name: sql.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这个不⽤写实体类可以直接取直
f.entity;
import org.t.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) {
}
@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("${ws}")
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")、还需要在l⽂件中添加build 标记和⾥⾯的内容》》编写controller
⼀、添加起步依赖在pom中
<!--mybatis 起步依赖-->
<dependency>
<groupId&batis.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>spring怎么读多个文件
<includes>
<include>**/*.*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
三、编写实体类并实现Serializable 序列化接⼝
f.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层)及映射⽂件接⼝:
f.mapper;
f.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 "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.zrf.mapper.MyBatisUserMapper">
<select id="getUserList" resultType="ity.MyBatisUser">
select * from user
</select>
</mapper>
五、编写controller
@Autowired
MyBatisUserMapper myBatisUserMapper;
//整合MyBatis查询所有
@RequestMapping("/mybatis/userList")
public List<MyBatisUser> myBatisUserList(){
List<MyBatisUser> userList = UserList();
return userList;
}
六、编写启动器
f;
batis.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层写了个修改的⽅法也就是测试类内容,在测试类掉⽤直接修改;可在测试类直接书写
f.service;
f.dao.UserDao;
f.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 = ();
//根据获得的⽤户对象修改姓名
user.setUsername("孙悟空");
userDao.save(user);
}
}
三、编写测试类
f;
f.entity.MyBatisUser;
f.mapper.MyBatisUserMapper;
f.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.st.context.SpringBootTest;
import st.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 = UserList();
for (MyBatisUser myBatisUser:userList){
System.out.println(myBatisUser);
}
}
//修改
@Test
public void updateTest(){
userService.updateUsername();
}
}
四、springBoot使⽤技巧
事物管理
在开启事物的⽅法上添加@Transactional 注解即可
如果springBoot版本是2.0以下
需要在启动类上添加注解@EnableTransactionManagement
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论