SpringBoot+mybatis plus 框架搭建
1、引入引用
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.ymn</groupId> <artifactId>mybatis003</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mybatis003</name> <description>mybatis003</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.4</version> </dependency> <!-- 导入mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、配置数据库连接地址
# DataSource Config server: port: 8099 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatisplus02?autoRe connect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&nullCatalogMeansCurrent=true username: root password: 545456546546
3、创建mapper
@Component public interface UserMapper extends BaseMapper<User> { }
4、创建service访问
@Component public interface UserMapper extends BaseMapper<User> { }
5、单元测试测试
package com.ymn.mybatis003;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ymn.mybatis003.entitys.User;
import com.ymn.mybatis003.mappers.UserMapper;
import com.ymn.mybatis003.services.UserService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
//有了@RunWith(SpringRunner.class)这些类才能实例化到spring容器中,自动注入才能生效,
@RunWith(SpringRunner.class)
@SpringBootTest
class Mybatis003ApplicationTests {
@Autowired
private UserMapper userMapper;
@Autowired
private UserService userService;
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<User> userList = userMapper.selectList(null);
//Assert.assertEquals(5, userList.size());
userList.forEach(System.out::println);
userList.get(0).setName("王五77");
//Update name by id
userMapper.updateById(userList.get(0));
//query user by id
User user = userMapper.selectById(4);
System.out.println("编号为4的用户:"+user.getName());
//use wrapper query
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id",5);
User user5 = userMapper.selectOne(queryWrapper);
System.out.println("第五名用户");
System.out.println(user5);
//query count
QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
queryWrapper1.gt("age",10);
Long count = userMapper.selectCount(queryWrapper1);
System.out.println("大于10岁的用户数:"+count);
User user3 = userService.getById(1);
System.out.println("使用service获取对象");
System.out.println(user3);
LambdaQueryWrapper<User> lamadaQueryWrapper = new LambdaQueryWrapper<>();
lamadaQueryWrapper.eq(User::getId,9);
User user9 = userService.getOne(lamadaQueryWrapper);
System.out.println("第九个用户的名称:"+user9.getName());
}
}
6、sql语句
posted on 2021-12-26 20:16 topguntopgun 阅读(273) 评论(0) 编辑 收藏 举报