Spring Boot整合

JUnit

导入坐标

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

测试类注解 

@SpringBootTest
class Springboot07JunitApplicationTests {
    @Autowired
    private BookService bookService;
    @Test
    public void testSave(){
        bookService.save();
    }
}

添加启动类

@SpringBootTest(classes = Springboot05JUnitApplication.class)
class Springboot07JUnitApplicationTests {}

Mybatis

设置数据源

spring:
datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm_db
    username: root
    password: root

Mybatis Plus

中央仓库:Maven Repository: com.baomidou » mybatis-plus-boot-starter (mvnrepository.com)icon-default.png?t=N7T8https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter

添加坐标

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3</version>
</dependency>

配置 

mybatis-plus:
  global-config:
    db-config:
      table-prefix:
      id-type: auto
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

  定义数据层接口与映射配置,继承BaseMapper

@Mapper
public interface UserDao extends BaseMapper<User> {
}

分页

分页操作是在MyBatisPlus的常规操作基础上增强得到,内部是动态的拼写SQL语句,因此需要增强对应的功能,使用MyBatisPlus拦截器实现

    @Configuration
    public class MpConfig {
        @Bean
        public MybatisPlusInterceptor mpInterceptor() {
            //1.定义Mp拦截器
            MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();
            //2.添加具体的拦截器
            mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
            return mpInterceptor;
        }
    }

使用IPage封装分页数据

使用QueryWrapper对象封装查询条件,推荐使用LambdaQueryWrapper对象,所有查询操作封装成方法调用

    @Test
    void testGetByCondition(){
        IPage page = new Page(1,10);
        LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<Book>();
        lqw.like(Book::getName,"Spring");
        bookDao.selectPage(page,lqw);
    }

支持动态拼写查询条件

    @Test
    void testGetByCondition(){
        String name = "Spring";
        IPage page = new Page(1,10);
        LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<Book>();
        lqw.like(Strings.isNotEmpty(name),Book::getName,"Spring");
        bookDao.selectPage(page,lqw);
    }

 快速开发

使用MyBatisPlus提供业务层通用接口(ISerivce<T>)与业务层通用实现类(ServiceImpl<M,T>)

public interface IBookService extends IService<Book> {
}
public interface IBookService extends IService<Book> {
    //追加的操作与原始操作通过名称区分,功能类似
    Boolean delete(Integer id);
    Boolean insert(Book book); 
    Boolean modify(Book book);
    Book get(Integer id);
}
    @Service
    public class BookServiceImpl2 extends ServiceImpl<BookDao,Book> implements IBookService {
        @Autowired
        private BookDao bookDao;
        public Boolean insert(Book book) {
            return bookDao.insert(book) > 0;
        }
        public Boolean modify(Book book) {
            return bookDao.updateById(book) > 0;
        }
        public Boolean delete(Integer id) {
            return bookDao.deleteById(id) > 0;
        }
        public Book get(Integer id) {
            return bookDao.selectById(id);
        }
    }

Druid

导入坐标

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.6</version>
</dependency>

变更配置方式

spring:
    datasource:
        druid:
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://localhost:3306/database_name?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
            username: username
            password: password

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值