栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot03----------整合第三方技术

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot03----------整合第三方技术

SpringBoot整合JUnit

貌似不用手动整合,创建一个springboot模块就有,是通过@SpringBootTest这个注解定义测试类的

  • dao
public interface BookDao{
    public void save();
}
  • daoImpl
@Repository
public class BookDaoImpl implements BookDao {
    @Override
    public void save() {
        System.out.println("bookdao test");
    }
}
  • test类
@SpringBootTest
class Springboot0301JunitApplicationTests {
    @Autowired
    private BookDao bookDao;

    @Test
    void contextLoads() {
        bookDao.save();
    }

}

它不用整合的原因是创建工程的时候自带有测试对应的starter且存在@SpringBootTest注解修饰的测试类

  • 测试类的位置
    当测试类位于引导类所在的包或者子包下没有任何问题,但是如果不在就会爆错。解决方法:
    • 01:再SpringBoot注解添加classes属性@SpringBoot(classes = 引导类类名.class)
      原因spring整合junit需要RunWith和ContextConfiguration,分别用于设置运行器和指定配置文件/配置类。
    • 02:使用@ContextConfiguration(引导类.class)
SpringBoot整合MyBatis

创建新模块,勾选mybatis framework和sql Driver

  • mybatis相关的starter
    这里创建工程勾选相对应的模块会自动创建

            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.2
        
        
            mysql
            mysql-connector-java
            runtime
        
  • 数据库配置信息
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm_crud
    username: root
    password: "!Wzh2352186607"
  • 实体类封装数据
public class Dept {
    private Integer deptId;
    private String deptName;
    ..........
  • 映射配置
    注意整合时这个@Mapper必须写,原来没整合的时候写了@Select就不用写mapper注解了
@Mapper
public interface BookDao {
    @Select("select * from tbl_dept where dept_id=#{deptId}")
    public Dept getById(Integer deptId);
}
  • test
@SpringBootTest
class Springboot0302MyBatisApplicationTests {
    @Autowired
    private BookDao bookDao;

    @Test
    void contextLoads() {
        System.out.println(bookDao.getById(1));
    }
}

更改springboot的版本再次运行报错,说需要设置服务器的时区?(这是mysql5和8的区别?)

评论区有人说写成serverTimeZone=Asia/Shanghai

SpringBoot整合Mybatis-plus 创建新模块

找不到mybatis-plus只有mysql Driver。

  • 解决方法1:
    解决方法:创建模块的时候下载地址选择阿里云的https://start.aliyun.com
    勾选模块
  • 解决方法2:
    还是使用之前的方式创建,只导入mysql的驱动。
    搜索mp的starter的坐标,复制到pom


    com.baomidou
    mybatis-plus-boot-starter
    3.5.0


删除原来的boot的起步依赖

		
            org.springframework.boot
            spring-boot-starter
        
配置连接

注意表名需要配置前缀(因为表名和实体类名查了一个前缀)

mybatis-plus:
  global-config:
    db-config:
      table-prefix: tbl_
复制之前的实体类,dao

修改dao

@Mapper
public interface BookDao extends BaseMapper {
 
}

修改实体类

public class Dept{
    @TableId(value = "dept_id")
    private Integer deptId;
    @TableField(value = "dept_name")
    private String deptName;
    ..........

BaseMapper里面有很多增删改查的方法

测试
@SpringBootTest
class Springboot0303MpApplicationTests {

    @Autowired
    private BookDao bookDao;

    @Test
    void contextLoads() {
        System.out.println(bookDao.selectById(2));;
    }

}


SpringBoot整合Druid

创建模块勾选驱动和mybaits,

  • 手动导入druid的starter
  • 配置数据源(两种方式)

    …
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/945032.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号