整合SpringBoot2

Mybatis

配置模式一

• 全局配置文件
• SqlSessionFactory: 自动配置好了
• SqlSession:自动配置了 SqlSessionTemplate 组合了SqlSession@Import(AutoConfiguredMapperScannerRegistrar.class);
• Mapper: 只要我们写的操作MyBatis的接口标准了 @Mapper 就会被自动扫描进来

yaml中配置mybatis规则

放在了resources下mybatis/mybatis-config.xml

//配置mybatis规则
mybatis:
//config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml  */前面的(* /)为添加的
 
  configuration:
    map-underscore-to-camel-case: true
    
 可以不写全局;配置文件,所有全局配置文件的配置都放在configuration配置项中即可
• 导入mybatis官方starter
• 编写mapper接口。标准@Mapper注解
• 编写sql映射文件并绑定mapper接口
• 在application.yaml中指定Mapper配置文件的位置,以及指定全局配置文件的信息 (建议;配置在mybatis.configuration)

配置模式二(建议)

最佳实战:
• 引入mybatis-starter
• 配置application.yaml中,指定mapper-location位置即可
• 编写Mapper接口并标注@Mapper注解
• 简单方法直接注解方式
• 复杂方法编写mapper.xml进行绑定映射
• @MapperScan("admin.mapper") 简化,其他的接口就可以不用标注@Mapper注解

整合 MyBatis-Plus

自动配置
• MybatisPlusAutoConfiguration 配置类,MybatisPlusProperties 配置项绑定。mybatis-plus:xxx 就是对mybatis-plus的定制
• SqlSessionFactory 自动配置好。底层是容器中默认的数据源
• mapperLocations 自动配置好的。有默认值。classpath*:/mapper/**/*.xml;任意包的类路径下的所有mapper文件夹下任意路径下的所有xml都是sql映射文件。  建议以后sql映射文件,放在 mapper下
• 容器中也自动配置好了 SqlSessionTemplate@Mapper 标注的接口也会被自动扫描;建议直接 @MapperScan("admin.mapper") 批量扫描就行

接口创建

  1. ***Mapper接口
public interface UserMapper  extends BaseMapper<User> {
}
  1. ***Service接口
public interface UserService extends IService<User> {
}
  1. ***ServiceImpl实现类
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
}

分页功能

Controller

@Autowired
    UserService userService;
    @GetMapping("/dynamic")
    public String dynamic(@RequestParam(value = "pn" ,defaultValue = "1")Integer pn, Model model) {

        Page<User> userPage = new Page<>(pn, 2);

        Page<User> page = userService.page(userPage, null);

        model.addAttribute("page",page);

        return "table/dynamic_table";
    }

Mybatis分页插件

  • 配置类
@Configuration
public class MybatisConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.H2);
        //循环功能,末页点击下一页后返回第一页,首页点击上一页不会跳转至末页
        paginationInnerInterceptor.setOverflow(true);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }
}
//前端数据展示,此时操作的是page对象中的records
    <tr class="gradeX" th:each="user:${page.records}">
              <td th:text="${user.id}">Trident</td>
              <td th:text="${user.name}">Trident</td>
              <td th:text="${user.age}">Trident</td>
              <td th:text="${user.email}">Trident</td>
//分页功能展示
  <ul>
   <li ><a th:href="@{/dynamic(pn=${page.getCurrent()}-1)}">← Previous</a></li>
   <li th:class="${num==page.getCurrent()?'active':''}" 
   th:each="num:${#numbers.sequence(1,page.pages)}"><a
   th:href="@{/dynamic(pn=${num})}">[[${num}]]</a>
   </li>
   <li class="next"><a
   th:href="@{/dynamic(pn=${page.getCurrent()}+1)}">Next → </a></li>
   </ul>

Junit5

参数化测试

public class ParameterTest {
    @ParameterizedTest
    @ValueSource(ints = {1,2,3,4,5})
    void testParameterized(int i){
        System.out.println(i);
    }
    @ParameterizedTest
    @MethodSource("stringStream")
    void testParameterized2(String i){
        System.out.println(i);
    }
    static Stream<String> stringStream(){
        return Stream.of(  "1,2,3,4,5","apple","huawei","Tencent");

    }
}

如何使用actuator

• 引入场景
• 访问 http://localhost:8080/actuator/**
• 暴露所有监控信息为HTTP

management:
  endpoints:
    enabled-by-default: true #暴露所有端点信息
    web:
      exposure:
        include: '*'  #以web方式暴露

• 测试
http://localhost:8080/actuator/beans
http://localhost:8080/actuator/configprops
http://localhost:8080/actuator/metrics
http://localhost:8080/actuator/metrics/jvm.gc.pause
http://localhost:8080/actuator/endpointName/detailPath

Profile环境切换

不同配置环境生效

配置类

  • Controller类
@Controller
public class HelloController {
    @Autowired
   private Person person;
   //未指定spring.profiles.active=myprod时生效
    @Value("${person.name:李四}")
    private String name;
    @GetMapping(value = {"/"})
  @ResponseBody
    public Person person(){
        System.out.println(name);
        return person;
    }
}

  • Config类中
@Configuration
public class MyConfig {
    @Profile("prod")
    @Bean
    public Color red(){
        return new Color();
    }
    @Profile("test")
    @Bean
    public Color yellow(){
        return new Color();
    }
}

实现类

public interface Person {
String getName();
Integer getAge();
}
@Profile("prod")
@Component
@Data
@ConfigurationProperties("person")
public class Boss implements Person {
    private   String name;
    private   Integer age;
}

@Profile("test")
@Data
@Component
@ConfigurationProperties("person")
public class Worker implements Person {
    private   String name;
    private   Integer age;
}

配置环境分组

  • application.properties中
spring.profiles.active=myprod
spring.profiles.group.myprod[0]=ppd
spring.profiles.group.myprod[1]=prod
  • ppd.yaml中
person:
  age: 18
  name: prod-王五
server:
  port: 8800
  • prod.yaml中
person:
  name: prod-张三 
  #配置在后面的内容会覆盖掉前面的内容
server:
  port: 8000

配置文件查找位置

 1. classpath 根路径
 2. classpath 根路径下config目录
 3. jar包当前目录
 4. jar包当前目录的config目录
 5. /config子目录的直接子目录

配置文件加载顺序

指定环境优先,外部优先,后面的可以覆盖前面的同名配置项

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值