位置: IT常识 - 正文
推荐整理分享SpringBoot【基础篇】---- 基于SpringBoot实现SSMP整合(springboot基本结构),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:springboot详细讲解,springboot系列教程,springboot基本结构,springboot基本概念,springboot基础教程,springboot基础教程,springboot基本概念,springboot基础教程,内容如对您有帮助,希望把文章链接给更多的朋友!
简化方式
@SpringBootTest(classes = Springboot04JunitApplication.class)class Springboot04JunitApplicationTests {}原始配置方式
@SpringBootTest@ContextConfiguration(classes = Springboot04JunitApplication.class)class Springboot04JunitApplicationTests {}3. 总结导入测试对应的 starter测试类使用 @SpringBootTest 修饰使用自动装配的形式添加要测试的对象测试类如果存在与引导类所在包或子包中无需指定引导类测试类如果不存在于引导类所在包或子包中需要通过 classes 属性指定引导类2. 整合MyBatis核心配置:数据库连接相关信息(连什么?谁连?什么权限)映射配置:SQL 映射(XML / 注解)1. 导入对应的 starter<dependencies> <!--1.导入对应的starter--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency></dependencies>2. 配置数据源相关信息#2.配置相关信息spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/ssm_db username: root password: root3. 实体类public class Book { private Integer id; private String type; private String name; private String description;}4. 映射接口(Dao)@Mapperpublic interface BookDao { @Select("select * from tbl_book where id = #{id}") public Book getById(Integer id);}注意:数据库 SQL 映射时需要添加 @Mapper 才能被容器识别
5. 测试类@SpringBootTestclass Springboot05MybatisApplicationTests { @Autowired private BookDao bookDao; @Test void contextLoads() { System.out.println(bookDao.getById(1)); }}注意:MySql 驱动升级到 8 以后会强制要求配置时区,如果不设置会出现问题。解决办法很简单,在 MySQL 的 url 上面添加上对应的设置即可。
3. 整合MyBatis-Plus、1. 导入对应的 starter<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3</version></dependency>2. 配置数据源相关信息#2.配置相关信息spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/ssm_db username: root password: root3. 映射接口(Dao)@Mapperpublic interface BookDao extends BaseMapper<Book> {}mybatis-plus 的核心在于 Dao 接口继承了一个 BaseMapper 的接口,这个接口中帮助开发者预定了若干个常用的 API 接口,简化了通用 API 接口的开发工作。
4. 整合Druid前面整合MyBatis和MP的时候,使用的数据源对象都是SpringBoot默认的数据源对象,下面我们手工控制一下,自己指定了一个数据源对象,Druid。
1. 导入对应的 starter<dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency></dependencies>2. 修改配置spring: datasource: druid: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC username: root password: root上一篇:立体匹配入门指南(8):视差图、深度图、点云(立体匹配原理)
友情链接: 武汉网站建设