MyBatis-Plus和MyBatis可以共存在一个项目中,具体的使用方法如下:
1. 引入依赖
- 在项目中引入MyBatis-Plus的依赖:
com.baomidou mybatis-plus-boot-starter 3.3.2.RELEASE - 在项目中引入MyBatis的依赖:
org.mybatis mybatis 3.5.5
2. 配置文件
- MyBatis-Plus的配置文件:
# Mybatis-Plus配置 mybatis-plus: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.example.entity
- MyBatis的配置文件:
# Mybatis配置 mybatis: config-location: classpath:mybatis-config.xml mapper-locations: classpath:mapper/*.xml
3. 实现接口
- MyBatis-Plus实现的接口:
public interface UserMapper extends BaseMapper
{ //自定义方法 } - MyBatis实现的接口:
public interface UserMapper { //自定义方法 }
4. 实现Mapper
- MyBatis-Plus实现的Mapper:
@Mapper public interface UserMapper extends BaseMapper
{ //自定义方法 } - MyBatis实现的Mapper:
@Mapper public interface UserMapper { //自定义方法 }
5. 配置Mapper
- MyBatis-Plus的配置:
@Configuration public class MyBatisPlusConfig { @Bean public UserMapper userMapper() { return new UserMapper(); } } - MyBatis的配置:
@Configuration public class MyBatisConfig { @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); return sqlSessionFactoryBean.getObject(); } @Bean public UserMapper userMapper(SqlSessionFactory sqlSessionFactory) throws Exception { SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory); return sqlSessionTemplate.getMapper(UserMapper.class); } }
6. 使用Mapper
- MyBatis-Plus的使用:
@Autowired private UserMapper userMapper; public void test() { User user = userMapper.selectById(1); System.out.println(user); } - MyBatis的使用:
@Autowired private UserMapper userMapper; public void test() { User user = userMapper.getUserById(1); System.out.println(user); }
以上就是MyBatis-Plus与MyBatis共存一个项目的使用方法。在实际开发中,可以根据实际情况,选择MyBatis-Plus或MyBatis来实现相应的功能。