mybatis-plus,hutool,lombok
parent
2187059ea8
commit
f524e83bac
15
pom.xml
15
pom.xml
|
|
@ -30,6 +30,8 @@
|
|||
<poi.version>4.1.2</poi.version>
|
||||
<velocity.version>2.3</velocity.version>
|
||||
<jwt.version>0.9.1</jwt.version>
|
||||
<mybatisplus.version>3.5.10.1</mybatisplus.version>
|
||||
<hutool.version>5.8.36</hutool.version>
|
||||
<!-- override dependency version -->
|
||||
<tomcat.version>9.0.98</tomcat.version>
|
||||
<logback.version>1.2.13</logback.version>
|
||||
|
|
@ -107,6 +109,13 @@
|
|||
<version>${druid.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- mybatisplus -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>${mybatisplus.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 解析客户端操作系统、浏览器等 -->
|
||||
<dependency>
|
||||
<groupId>eu.bitwalker</groupId>
|
||||
|
|
@ -218,6 +227,12 @@
|
|||
<version>${ruoyi.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>${hutool.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
|
|
|||
|
|
@ -101,13 +101,27 @@ token:
|
|||
expireTime: 30
|
||||
|
||||
# MyBatis配置
|
||||
mybatis:
|
||||
# 搜索指定包别名
|
||||
typeAliasesPackage: com.ruoyi.**.domain
|
||||
# 配置mapper的扫描,找到所有的mapper.xml映射文件
|
||||
mapperLocations: classpath*:mapper/**/*Mapper.xml
|
||||
# 加载全局的配置文件
|
||||
configLocation: classpath:mybatis/mybatis-config.xml
|
||||
#mybatis:
|
||||
# # 搜索指定包别名
|
||||
# typeAliasesPackage: com.ruoyi.**.domain
|
||||
# # 配置mapper的扫描,找到所有的mapper.xml映射文件
|
||||
# mapperLocations: classpath*:mapper/**/*Mapper.xml
|
||||
# # 加载全局的配置文件
|
||||
# configLocation: classpath:mybatis/mybatis-config.xml
|
||||
|
||||
# mybatis-plus配置
|
||||
mybatis-plus:
|
||||
# MyBatis 配置
|
||||
type-aliases-package: com.ruoyi.**.domain
|
||||
mapper-locations: classpath*:mapper/**/*Mapper.xml
|
||||
config-location: classpath:mybatis/mybatis-config.xml
|
||||
map-underscore-to-camel-case: true
|
||||
# 全局配置
|
||||
global-config:
|
||||
db-config:
|
||||
# 数据库配置
|
||||
id-type: auto
|
||||
|
||||
|
||||
# PageHelper分页插件
|
||||
pagehelper:
|
||||
|
|
|
|||
|
|
@ -16,6 +16,22 @@
|
|||
</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- mybatis-plus -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring框架基本的核心工具 -->
|
||||
<dependency>
|
||||
|
|
|
|||
|
|
@ -113,20 +113,20 @@ public class MyBatisConfig
|
|||
return resources.toArray(new Resource[resources.size()]);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception
|
||||
{
|
||||
String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
|
||||
String mapperLocations = env.getProperty("mybatis.mapperLocations");
|
||||
String configLocation = env.getProperty("mybatis.configLocation");
|
||||
typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
|
||||
VFS.addImplClass(SpringBootVFS.class);
|
||||
|
||||
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
|
||||
sessionFactory.setDataSource(dataSource);
|
||||
sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
|
||||
sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
|
||||
sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
|
||||
return sessionFactory.getObject();
|
||||
}
|
||||
// @Bean
|
||||
// public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception
|
||||
// {
|
||||
// String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
|
||||
// String mapperLocations = env.getProperty("mybatis.mapperLocations");
|
||||
// String configLocation = env.getProperty("mybatis.configLocation");
|
||||
// typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
|
||||
// VFS.addImplClass(SpringBootVFS.class);
|
||||
//
|
||||
// final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
|
||||
// sessionFactory.setDataSource(dataSource);
|
||||
// sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
|
||||
// sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
|
||||
// sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
|
||||
// return sessionFactory.getObject();
|
||||
// }
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package com.ruoyi.framework.config;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author liangyq
|
||||
* @date 2025-03-18 15:16
|
||||
*/
|
||||
@Configuration
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
/**
|
||||
* 乐观锁{@link com.baomidou.mybatisplus.annotation.Version}
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -17,6 +17,12 @@
|
|||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- 通用工具-->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
package com.ruoyi.system.domain;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.annotation.Version;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author liangyq
|
||||
* @date 2025-03-18 15:26
|
||||
*/
|
||||
@Data
|
||||
@TableName("test")
|
||||
public class TestDO {
|
||||
|
||||
@TableId
|
||||
private Long id;
|
||||
|
||||
private String description;
|
||||
|
||||
@Version
|
||||
private Long version;
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package com.ruoyi.system.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.ruoyi.system.domain.TestDO;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* @author liangyq
|
||||
* @date 2025-03-18 16:05
|
||||
*/
|
||||
@Repository
|
||||
public interface TestMapper extends BaseMapper<TestDO> {
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package com.ruoyi.system.service;
|
||||
|
||||
/**
|
||||
* @author liangyq
|
||||
* @date 2025-03-18 16:07
|
||||
*/
|
||||
public interface ITestService {
|
||||
|
||||
void insertTest();
|
||||
|
||||
void updateTest();
|
||||
|
||||
void listTest();
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
package com.ruoyi.system.service.impl;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.ruoyi.system.domain.TestDO;
|
||||
import com.ruoyi.system.mapper.TestMapper;
|
||||
import com.ruoyi.system.service.ITestService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author liangyq
|
||||
* @date 2025-03-18 16:08
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class TestService implements ITestService {
|
||||
@Autowired
|
||||
private TestMapper testMapper;
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public void insertTest() {
|
||||
TestDO testDO = new TestDO();
|
||||
testDO.setDescription("测试插入" + DateUtil.now());
|
||||
testDO.setVersion(0L);
|
||||
testMapper.insert(testDO);
|
||||
log.info("插入:{}", testDO);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public void updateTest() {
|
||||
TestDO testDO = new TestDO();
|
||||
testDO.setId(1L);
|
||||
testDO.setDescription("测试修改" + DateUtil.now());
|
||||
testDO.setVersion(1L);
|
||||
log.info("修改:{}", testMapper.updateById(testDO));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void listTest() {
|
||||
List<TestDO> testDOList = testMapper.selectList(Wrappers.lambdaQuery(TestDO.class).eq(TestDO::getId, 1));
|
||||
log.info("查询:{}", testDOList);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue