Mybatis----数据库连接池&三种常用查询方式

一.数据库连接池

<?xml version="1.0" encoding="UTF-8"?>
<Context>
	<Resource
		driverClassName="com.mysql.jdbc.Driver"
		url="jdbc:mysql://localhost:3306/ssm"
		username="root"
		password="root"
		maxActive="50"
		maxIdle="20"
		name="test"
		auth="Container"
		maxWait="10000"
		type="javax.sql.DataSource"
	/>
</Context>

二.3种常用查询方式

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用MyBatis-Plus(以下简称MP)时,连接据库的方式与使用普通的MyBatis是一样的,需要在项目中引入据库驱动和连接池的依赖,比如MySQL的话可以使用以下依赖: ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.22</version> </dependency> ``` 在application.properties或application.yml中配置据源信息,例如: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8 username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver ``` 在使用MP时,需要先创建实体类和对应的Mapper接口,实体类中的属性名需要与据库中的列名保持一致,Mapper接口需要继承BaseMapper,例如: ```java public interface UserMapper extends BaseMapper<User> {} ``` 在Service层中,可以使用MP提供的IService接口,它提供了一些常用的CRUD方法,例如: ```java public interface IUserService extends IService<User> {} ``` 在Controller层中,可以注入Service层的接口,然后调用其中的方法,例如: ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private IUserService userService; @GetMapping("/{id}") public User getById(@PathVariable Long id) { return userService.getById(id); } @PostMapping public boolean save(@RequestBody User user) { return userService.save(user); } // ... } ``` 这样就完成了一个简单的使用MP的CRUD操作,当然还有更多高级用法可以探索。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值