MyBatis-Plus+多数据源+Druid配置及应用

本文介绍了如何在项目中配置MyBatis-Plus,包括无需XML配置的简单使用,Maven插件设置,以及如何在application.properties中配置数据源。此外,还详细讲解了多数据源插件的两种用法,一种是在properties文件中配置,另一种是动态管理数据库连接。最后,文章提到了Druid数据源的配置,包括Maven依赖、properties配置和监控器设置。
摘要由CSDN通过智能技术生成

Mybatis-Plus的配置

mybatis-plus可以在不配置xml和mapper的情况下进行数据存取(优于mybatis)
(只需要建一个空的mapper接口)

  1. Maven插件

velocity-engine-core是mybatis-plus自动生成代码所依赖的模板(不用自动生成代码功能可不用)

        <dependency>
    		<groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.6</version>
		</dependency>
		<dependency>
   			<groupId>org.apache.velocity</groupId>
    		<artifactId>velocity-engine-core</artifactId>
    		<version>2.2</version>
		</dependency>
  1. application.properties配置

和普通mysql配置相同,没有额外配置

#mysql 连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  1. Mapper相关注解

@MapperScan中直接mapper文件所在的package

@SpringBootApplication
@EnableTransactionManagement
@EnableEurekaClient
@MapperScan("com.example.demo.dao")
public class DemoApplication {
   

	public static void main(String[] args) {
   
		SpringApplication.run(DemoApplication.class, args);
	}
}
  1. 创建数据库表

  1. 创建JavaBean(推荐使用Lombok插件方式) - Model层
@Data
@TableName("area")
@ToString(callSuper=true, includeFieldNames=true)
@AllArgsConstructor
@NoArgsConstructor
public class Area implements Serializable{
   
	
	private static final long serialVersionUID = 1L;
	private String id;
	private String name;
	private int sort;
 
}
  1. 创建Mapper - DAO层

只有自定义SQL时,mapper中才有内容, 使用mybatis-plus自带CRUD语句或者构造器拼接语句时, mapper通常为空

public interface AreaMapper extends BaseMapper<Area> {
   
	
	//通过mybatis-plus提供的注解,直接自定义SQL
	@Select("select name from area where sort > ${sort}")
	List<String> getBySort2(@Param("sort")int sort);
}
  1. 创建逻辑接口 - Service层
public interface IAreaService {
   
	
	public Area getById(String id);
	
	public List<Area> selectAll();
	
	public int updateByPrimaryKeySelective(Area record);
	
	public int deleteByPrimaryKey(String id);
	
	public int insertSelective(Area record);
	
	public List<Area> getBySort();
	
	public List<String> getBySort2(int sort);

}
  1. 创建逻辑类 - Service层

Mybatis-Plus使用SQL的3种常见方法:
a. 使用mybatis-plus自带CRUD方法: 直接用mapper的自带方法
b. 使用mybatis-plus提供的条件构造器,拼接where条件
c. 使用@Select,@Update等注解, 自己写SQL+参数

@Service
public class AreaServiceImpl implements IAreaService {
   

	@Autowired
	public AreaMapper areaMapper;

	//使用mybatis-plus自带CRUD方法
	@Override
	public Area getById(String id) {
   
		return areaMapper.selectById(id);
	}	
	
	@Override
	public List<Area> selectAll() {
   
		return areaMapper.selectList(null);
	}
	
	@Override
	@Transactional
	public int updateByPrimaryKeySelective(Area record) {
   
		return areaMapper.updateById(record);
	}
	
	@Override
	public int deleteByPrimaryKey(String id) {
   
		return areaMapper.deleteById(id);
	}
	
	@Override
	public int insertSelective(Area record) {
   
		return areaMapper.insert(record);
	}
	
	//使用mybatis-plus提供的条件构造器,拼接条件
	@Override
	public List<Area> getBySort(){
   
		QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().gt(Area::getSort, 40)
							 .lt(Area::getSort, 80);
		return areaMapper.selectList(queryWrapper);
	}
	
	//通过mybatis-plus提供的注解,直接自定义SQL(定义在mapper中)
	@Override
	public List<String> getBySort2(int sort){
   
		return areaMapper.getBySort2(sort);
		
	}

}
  1. 创建接口 - Controller层
@RestController
@RequestMapping("/area")
public class AreaController {
   

	@Autowired
	private IAreaService areaService;
	
	@Autowired
	private DbpropertiesService dbproService;
	
	@Autowired
	private Area area;
	
	@Resource
	protected HttpServletRequest request;

	@RequestMapping(value = "/getAreaInfo",method=RequestMethod.GET)
	public Area getAreaInfo(@RequestParam(value="id") String id) {
   
		area = areaService.getById(id);
		return area;
	}
	
	@RequestMapping(value = "/getAreaAllInfo",method=RequestMethod.GET)
	public List<Area> getAreaInfo() {
   
		List<Area> arlist 
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
配置多数据: 1. 在pom.xml中添加mybatis-plusdruid依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency> ``` 2. 在application.yml中添加数据配置: ```yaml spring: datasource: druid: one: url: jdbc:mysql://localhost:3306/one?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver two: url: jdbc:mysql://localhost:3306/two?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource #默认数据 primary: one ``` 3. 在代码中使用@DS注解切换数据: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @DS("one") @Override public User getUserById(Long id) { return userMapper.selectById(id); } @DS("two") @Override public User getUserByName(String name) { return userMapper.selectOne(new QueryWrapper<User>().eq("name", name)); } } ``` 配置分页插件: 1. 在pom.xml中添加分页插件依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> ``` 2. 在配置类中配置分页插件: ```java @Configuration public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); return paginationInterceptor; } } ``` 3. 在controller层中使用分页: ```java @GetMapping("/users") public IPage<User> getUsers(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) { Page<User> page = new Page<>(pageNum, pageSize); QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("status", 1); return userService.page(page, queryWrapper); } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值