Java持久层框架Mybatis入门

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 MyBatis是什么 MyBatis是Java的持久层框架,GitHub的star数高达15.8k,是Java技术栈中最热门的ORM框架之一。它支持自定义SQL、存储过程以及高级映射,可以通过XML或注解来配置和映射原始类型、接口和Java POJOs为数据库中的记录。 POJOs,Plain Old Java Objects,纯的传统意义的Java对象,最基本的Java Bean只有属性加上属性的get和set方法。 安装 Maven pom.xml: <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>x.x.x</version> </dependency> 版本查询:https://mvnrepository.com/artifact/org.mybatis/mybatis SqlSessionFactory SqlSessionFactory的实例是每个MyBatis应用的核心,通过SqlSessionFactoryBuilder创建,能基于XML配置,也能使用Configuration类。 基于XML配置: String resource = "org/mybatis/example/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 实际项目中不会手动设置XML文件路径,而是直接读取classpath去找XML。 使用Configuration类: DataSource dataSource = BlogDataSourceFactory.getBlogDataSource(); TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("development", transactionFactory, dataSource); Configuration configuration = new Configuration(environment); configuration.addMapper(BlogMapper.class); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); 由于Java注解的一些限制以及某些MyBatis映射的复杂性,MyBatis会优先选择XML配置。在上面的示例中,MyBatis会根据classpath和BlogMapper.class去找BlogMapper.xml。 ...

跟着官方文档学Maven构建生命周期

在IntelliJ IDEA中,显示了Maven的Lifecycle:

对IoC容器和Bean的学习笔记

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 What We Mean by “Spring” The term “Spring” means different things in different contexts. It can be used to refer to the Spring Framework project itself, which is where it all started. Over time, other Spring projects have been built on top of the Spring Framework. Most often, when people say “Spring”, they mean the entire family of projects. This reference documentation focuses on the foundation: the Spring Framework itself. ...

SpringBoot官方笔记8其他

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Container Images FROM eclipse-temurin:17-jre as builder WORKDIR application ARG JAR_FILE=target/*.jar COPY ${JAR_FILE} application.jar RUN java -Djarmode=layertools -jar application.jar extract FROM eclipse-temurin:17-jre WORKDIR application COPY --from=builder application/dependencies/ ./ COPY --from=builder application/spring-boot-loader/ ./ COPY --from=builder application/snapshot-dependencies/ ./ COPY --from=builder application/application/ ./ ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"] $ docker build --build-arg JAR_FILE=path/to/myapp.jar . Production-ready Features The spring-boot-actuator module provides all of Spring Boot’s production-ready features. <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> Endpoints Actuator endpoints let you monitor and interact with your application. Spring Boot includes a number of built-in endpoints and lets you add your own. For example, the health endpoint provides basic application health information. ...

SpringBoot官方笔记7IO

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Caching Spring Boot auto-configures the cache infrastructure as long as caching support is enabled by using the @EnableCaching annotation. import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; @Component public class MyMathService { @Cacheable("piDecimals") public int computePiDecimal(int precision) { ... } } Before invoking computePiDecimal, the abstraction looks for an entry in the piDecimals cache that matches the i argument. If an entry is found, the content in the cache is immediately returned to the caller, and the method is not invoked. Otherwise, the method is invoked, and the cache is updated before returning the value. ...

SpringBoot官方笔记6消息

The Spring Framework provides extensive support for integrating with messaging systems, from simplified use of the JMS API using JmsTemplate to a complete infrastructure to receive messages asynchro

SpringBoot官方笔记5Data

Spring Boot integrates with a number of data technologies, both SQL and NoSQL.

SpringBoot官方笔记4Web

Most web applications use the spring-boot-starter-web module to get up and running quickly. You can also choose to build reactive web applications by using the spring-boot-starter-webflux module.

SpringBoot官方笔记3核心

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 SpringApplication By default, INFO logging messages are shown, including some relevant startup details, such as the user that launched the application. Lazy Initialization When lazy initialization is enabled, beans are created as they are needed rather than during application startup. spring.main.lazy-initialization=true @Lazy(false) annotation:disable lazy initialization for certain beans Customizing the Banner adding a banner.txt file to your classpath or by setting the spring.banner.location property to the location of such a file. If the file has an encoding other than UTF-8, you can set spring.banner.charset. ...

SpringBoot官方笔记2使用

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Build Systems 选择Maven or Gradle,而不要Ant(not particularly well supported) In practice, you do not need to provide a version for any of these dependencies (spring-boot-dependencies) in your build configuration, as Spring Boot manages that for you. When you upgrade Spring Boot itself, these dependencies are upgraded as well in a consistent way. Each release of Spring Boot is associated with a base version of the Spring Framework. Starters are a set of convenient dependency descriptors that you can include in your application. ...