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

springboot整合mybatis-plus

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

springboot整合mybatis-plus

springboot整合mybatis-plus 简介

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

实战 1.引入依赖
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.2
        
        
            com.baomidou
            mybatis-plus
            3.4.2
        
2.使用插件

以下配置可根据项目需要配置,初学者学习可跳过本步骤。

@Configuration
//主要是扫描到mapper包下的接口交给spring管理,我这里在启动类上添加过了,这里就不加了
public class MybatisPlusDatasourceConfig {


    
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 新的分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        // 乐观锁插件
        
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        // 防全表更新与删除插件
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        return interceptor;
    }

    @Bean
    @Primary
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("dataSource") DataSource dataSource)
            throws Exception {
        MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);

        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.addInterceptor(mybatisPlusInterceptor());
        sessionFactory.setConfiguration(configuration);

        //mybatis 数据库字段与实体类属性驼峰映射配置
        sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        return sessionFactory.getObject();
    }

}
3.创建实体

@TableName(value = “user_info”)对应数据库中的表名

@Data
@TableName(value = "user_info")
public class UserInfo {

    private String id;

    private String name;

    private String sex;

    private String email;

}

4.Mapper 包下编写 UserMapper接口

@Repository
public interface UserMapper extends BaseMapper {
}

5.启动类上配置扫描Mapper包的注解

@MapperScan(“com.tomato.tomatocenter.mapper”)为UserMapper的包路径

@SpringBootApplication
@MapperScan("com.tomato.tomatocenter.mapper")
public class TomatoCenterApplication {

    public static void main(String[] args) {
        SpringApplication.run(TomatoCenterApplication.class, args);
    }

}
6.编写测试类测试
@SpringBootTest
class TomatoCenterApplicationTests {

    @Autowired
    private UserMapper userMapper;


    @Test
    void contextLoads() {
        System.out.println(("----- 测试方法 ------"));
        UserInfo userInfo = userMapper.selectById("**");
        System.out.println("----- 测试结果 ------"+userInfo);
    }

}

测试结果:

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _    
( ( )___ | '_ | '_| | '_ / _` |    
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |___, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.9)

2022-07-19 11:39:56.093  INFO 15800 --- [           main] c.t.t.TomatoCenterApplicationTests       : Starting TomatoCenterApplicationTests using Java 1.8.0_181 on ISS490002001977 with PID 15800 (started by issuser in D:clonetomato-center)
2022-07-19 11:39:56.095  INFO 15800 --- [           main] c.t.t.TomatoCenterApplicationTests       : The following 1 profile is active: "dev"
 _ _   |_  _ _|_. ___ _ |    _ 
| | |/|_)(_| | |_  |_)||_|_ 
     /               |         
                        3.4.2 
2022-07-19 11:40:00.317  INFO 15800 --- [           main] c.t.t.TomatoCenterApplicationTests       : Started TomatoCenterApplicationTests in 4.604 seconds (JVM running for 6.109)
----- 测试方法 ------
2022-07-19 11:40:00.804  INFO 15800 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-07-19 11:40:01.407  INFO 15800 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
----- 测试结果 ------UserInfo(id=1001211111000020001, name=abc, sex=null, email=u.fjnglrqmo@tmtwbafh.gi)
2022-07-19 11:40:01.556  INFO 15800 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2022-07-19 11:40:01.839  INFO 15800 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
Disconnected from the target VM, address: '127.0.0.1:63590', transport: 'socket'

Process finished with exit code 0

注:详情请参考 mybatis-plus使用指南

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

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

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