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使用技巧

  事物管理
​       在开启事物的方法上添加@Transactional  注解即可
​       如果springBoot版本是2.0以下
​       需要 在启动类上添加注解@EnableTransactionManagement
posted @ 2019-08-23 17:31  怕黑,可是却恋上了夜  阅读(1566)  评论(0编辑  收藏  举报