1.pom.xml文件导入相关依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--MybatisPlus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!--lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!--MySQL依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--阿里巴巴druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.19</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
2.准备实体类(User)
3.准备mapper类(UserMapper)
4.配置数据库驱动(在application.properties中配置)
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test(根据自己的库)?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
#开启日志
logging.level.root=info
logging.level.com.superliu.springboot.mapper=debug
5.准备测试类(TestMapper)
@SpringBootTest
public class TestMapper {
@Autowired
UserMapper userMapper;
/**
* 查询所有
*/
@Test
public void FindUserAll(){
List<User> users = userMapper.selectList(null);
users.forEach(user -> System.out.println("user ="+user));
}
/**
* 根据ID查询
*/
@Test
public void FindUserById(){
User user = userMapper.selectById(2);
System.out.println("user ="+user);
}
/**
* 条件查询
*/
@Test
public void FindByUser(){
QueryWrapper<User> qw = new QueryWrapper<User>();
qw.eq("age",18);//等值查询
qw.lt("age",15);//小于查询(less than)
qw.le("age",15);//小于等于查询(less equals)
qw.gt("age",15);//大于查询(greater than)
qw.ge("age",15);//大于等于查询(greater equals)
List<User> users = userMapper.selectList(qw);
users.forEach(user -> System.out.println("user ="+user));
}
/**
* 模糊查询
*/
@Test
public void FindUserLike(){
QueryWrapper<User> qw = new QueryWrapper<User>();
qw.like("u_name","a");//like ---> %?%(包含什么的查询) likeLeft ---> %?(以什么结尾的查询) likeRight ---> ?%(以什么开头的查询)
List<User> users = userMapper.selectList(qw);
users.forEach(user -> System.out.println("user "+user));
}
/**
* 新增
*/
@Test
public void UserSave(){
User user = new User();
user.setName("小刘").setAge(23).setBir(new Date()).setId("3");
userMapper.insert(user);
}
/**
* 根据Id更新
*/
@Test
public void UpdateById(){
User user = userMapper.selectById(3);
user.setName("刘xx").setAge(21);
userMapper.updateById(user);
}
/**
* 批量修改(将age=15时的用户名字改为`阿峰`)
*/
@Test
public void Update(){
User user = userMapper.selectById("3");
user.setName("阿峰");
user.setBir(null);
QueryWrapper<User> updateWrapper = new QueryWrapper<User>();
updateWrapper.eq("age",15);
userMapper.update(user,updateWrapper);
}
/**
* 根据Id删除
*/
@Test
public void DeleteById(){
userMapper.deleteById("4");
}
/**
* 根据条件批量删除
*/
@Test
public void DeleteByUser(){
QueryWrapper<User> qw = new QueryWrapper<User>();
qw.gt("age",15);
userMapper.delete(qw);
}
/**
* 分页查询使用
*/
@Test
public void FindPage(){
Page<User> page = new Page<>(1,2);//参数1:当前页 默认值1 参数2:每页显示记录数 默认值10
IPage<User> userIPage = userMapper.selectPage(page, null);
long total = userIPage.getTotal();
System.out.println("总记录数"+total);
userIPage.getRecords().forEach(user -> System.out.println("user =" +user));
}
/**
* 分页条件查询
*/
@Test
public void FindPageLike(){
QueryWrapper<User> qw = new QueryWrapper<User>();
qw.eq("age",23);
Page<User> page = new Page<>(1,5);//参数1:当前页 默认值1 参数2:每页显示记录数 默认值10
IPage<User> userIPage = userMapper.selectPage(page, qw);
long total = userIPage.getTotal();
System.out.println("总记录数"+total);
userIPage.getRecords().forEach(user -> System.out.println("user =" +user));
}
}
注意: 关于有关分页查询的使用,必须要配置分页拦截器对象(MybatisPlusConfig) 配置如下:
@EnableTransactionManagement
@Configuration
@MapperScan("com.superliu.springboot.mapper")//对应的Mapper类的位置
public class MybatisPlusConfig {
/**
* 分页拦截器对象
* @return
*/
//注意事项:目前分页查询仅仅支持单表查询,不能再表连接时使用分页插件
@Bean
public PaginationInterceptor paginationInterceptor(){
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}
对于 多数据源的配置 ,(可对数据库读写分离,起到更高的效率),对不同的库进行操作。
1.导入依赖:
<!--多数据源依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
2.配置数据库驱动(application.properties):
spring.datasource.primary=master #指定默认数据源
spring.datasource.dynamic.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.dynamic.datasource.master.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.dynamic.datasource.master.username=root
spring.datasource.dynamic.datasource.master.password=root
spring.datasource.dynamic.datasource.slave_1.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.dynamic.datasource.slave_1.url=jdbc:mysql://localhost:3306/test_01?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.dynamic.datasource.slave_1.username=root
spring.datasource.dynamic.datasource.slave_1.password=root
#开启日志
logging.level.root=info
logging.level.com.superliu.springboot.mapper=debug
注意:master与slave_1为数据源的名字,test与test_01为不同库的表名。
3.准备Service类(UserService):
4.准备ServiceImpl实现类(UserServiceImpl):
注意:@DS注解是指明对哪一个库进行操作,配置文件中spring.datasource.primary=master 指定默认数据源为master 。
5.准备测试类(TestService):
@SpringBootTest
public class TestService {
@Autowired
UserServiceImpl userService;
@Test
public void findAll(){
userService.findAll().forEach(user -> System.out.println("user = "+user));
}
@Test
public void addUser(){
User user = new User();
user.setAge(32).setId("5").setBir(new Date()).setName("ZZc");
userService.addUser(user);
}
}