mybatis集成mybatis-plus

mybatis集成mybatis-plus

1.添加注解

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

2.修改配置信息

原本的mybatis 修改成 mybatis-plus

3.修改配置文件

1.注释或删除,原本的MyBatisConfig文件
2.添加MybatisPlusConfig配置文件

import cn.hutool.core.net.NetUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
* @author xxx
* @date 2023/4/17
* @copyright ©2023/4/17
* @description MybatisPlus 配置
*/
@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
public class MybatisPlusConfig
{
   @Bean
   public MybatisPlusInterceptor mybatisPlusInterceptor()
   {
       MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
       // 分页插件
       interceptor.addInnerInterceptor(paginationInnerInterceptor());
       // 乐观锁插件
       interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
       // 阻断插件
       interceptor.addInnerInterceptor(blockAttackInnerInterceptor());
       return interceptor;
   }

   /**
    * 分页插件,自动识别数据库类型 https://baomidou.com/guide/interceptor-pagination.html
    */
   public PaginationInnerInterceptor paginationInnerInterceptor()
   {
       PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
       // 设置数据库类型为mysql
       paginationInnerInterceptor.setDbType(DbType.MYSQL);
       // 设置最大单页限制数量,默认 500 条,-1 不受限制
       paginationInnerInterceptor.setMaxLimit(-1L);
       return paginationInnerInterceptor;
   }

   /**
    * 乐观锁插件 https://baomidou.com/guide/interceptor-optimistic-locker.html
    */
   public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor()
   {
       return new OptimisticLockerInnerInterceptor();
   }

   /**
    * 使用网卡信息绑定雪花生成器
    * 防止集群雪花ID重复
    */
   @Bean
   public IdentifierGenerator idGenerator() {
       return new DefaultIdentifierGenerator(NetUtil.getLocalhost());
   }

   /**
    * 如果是对全表的删除或更新操作,就会终止该操作 https://baomidou.com/guide/interceptor-block-attack.html
    */
   public BlockAttackInnerInterceptor blockAttackInnerInterceptor()
   {
       return new BlockAttackInnerInterceptor();
   }
}

4.测试

1、继承BaseMapper

public interface SysNoticeMapper extends BaseMapper<SysNotice>
{
}

2.引用

 public int insertNotice(SysNotice notice)
   {
       return noticeMapper.insert(notice);
   }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis-Plus并没有直接集成Neo4j JDBC驱动,但是可以通过MyBatis-Plus提供的自定义SQL执行器来使用Neo4j JDBC驱动。 首先需要在项目中引入Neo4j JDBC驱动的依赖,可以在Maven中添加以下依赖: ```xml <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-jdbc-driver</artifactId> <version>4.3.1</version> </dependency> ``` 然后在MyBatis-Plus的配置类中配置自定义SQL执行器,示例如下: ```java @Configuration public class MybatisPlusConfig { @Bean public ConfigurationCustomizer configurationCustomizer() { return configuration -> { // 添加自定义SQL执行器 configuration.addInterceptor(new MyNeo4jJdbcInterceptor()); }; } /** * 自定义SQL执行器,使用Neo4j JDBC驱动执行SQL */ private static class MyNeo4jJdbcInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { // 获取MappedStatement和参数 MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; Object parameter = invocation.getArgs()[1]; // 获取SQL语句 BoundSql boundSql = mappedStatement.getBoundSql(parameter); String sql = boundSql.getSql(); // 使用Neo4j JDBC驱动执行SQL try (Connection connection = DriverManager.getConnection("jdbc:neo4j:bolt://localhost:7687", "neo4j", "password"); PreparedStatement statement = connection.prepareStatement(sql)) { // 设置参数 ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, parameter, boundSql); parameterHandler.setParameters(statement); // 执行SQL if (mappedStatement.getResultSetType() == ResultSetType.DEFAULT) { statement.execute(); } else { throw new RuntimeException("Unsupported result set type: " + mappedStatement.getResultSetType()); } // 返回结果 ResultSet resultSet = statement.getResultSet(); return new MyResultSet(resultSet); } } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { // do nothing } } /** * 自定义ResultSet,用于包装Neo4j JDBC驱动返回的ResultSet */ private static class MyResultSet implements ResultSet { private final ResultSet resultSet; public MyResultSet(ResultSet resultSet) { this.resultSet = resultSet; } // 实现ResultSet接口的方法,用于包装Neo4j JDBC驱动返回的ResultSet // ... } } ``` 以上代码中,`MyNeo4jJdbcInterceptor`是自定义的SQL执行器,使用Neo4j JDBC驱动执行SQL。在`intercept`方法中,获取SQL语句和参数,使用Neo4j JDBC驱动执行SQL,并将返回的`ResultSet`包装为`MyResultSet`返回。 需要注意的是,自定义SQL执行器需要添加到MyBatis-Plus的配置中,这里使用了`ConfigurationCustomizer`实现。同时,由于Neo4j JDBC驱动返回的`ResultSet`和MyBatis-Plus默认的`ResultSet`不同,需要自定义`MyResultSet`实现`ResultSet`接口,用于包装Neo4j JDBC驱动返回的`ResultSet`。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值