基于SpringBoot实现SSMP整合

基于SpringBoot实现SSMP整合

整合JUnit

  1. 导入测试对应的starter
<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>
    </dependencies>
  1. 测试类使用@SpringBootTest修饰
  2. 使用自动装配的形式添加要测试的对象
  3. 测试类如果存在于引导类所在包或子包中无需指定引导类
@SpringBootTest
class Springboot04JunitApplicationTests {
   
    //1.注入你要测试的对象
    @Autowired
    private BookDao bookDao;

    @Test
    void contextLoads() {
   
        //2.执行要测试的对象对应的方法
        bookDao.save();
         System.out.println("two...");
    }
}
  1. 测试类如果不存在于引导类所在的包或子包中需要通过classes属性指定引导类
@SpringBootTest(classes = Springboot04JunitApplication
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面为您介绍一个简单的Spring Boot + Mybatis Plus(SSMP)整合案例。 1. 首先,在pom.xml文件中添加以下依赖: ```xml <!--Spring Boot 依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Mybatis Plus 依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> <!--MySQL 驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> ``` 2. 在application.properties中配置数据源: ```properties # 数据库配置 spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 3. 创建一个实体类User,用于映射数据库中的user表: ```java @Data public class User { private Long id; private String name; private Integer age; private String email; } ``` 4. 创建UserMapper接口,用于定义数据库操作: ```java public interface UserMapper extends BaseMapper<User> { } ``` 5. 创建UserService类,用于定义业务逻辑: ```java @Service public class UserService { @Autowired private UserMapper userMapper; public List<User> selectList() { return userMapper.selectList(null); } public User selectById(Long id) { return userMapper.selectById(id); } public int insert(User user) { return userMapper.insert(user); } public int update(User user) { return userMapper.updateById(user); } public int delete(Long id) { return userMapper.deleteById(id); } } ``` 6. 创建UserController类,用于处理HTTP请求: ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/list") public List<User> selectList() { return userService.selectList(); } @GetMapping("/{id}") public User selectById(@PathVariable Long id) { return userService.selectById(id); } @PostMapping public int insert(@RequestBody User user) { return userService.insert(user); } @PutMapping public int update(@RequestBody User user) { return userService.update(user); } @DeleteMapping("/{id}") public int delete(@PathVariable Long id) { return userService.delete(id); } } ``` 7. 启动应用程序,访问http://localhost:8080/user/list可以获取所有用户的列表,访问http://localhost:8080/user/1可以获取id为1的用户的详细信息。 以上就是一个简单的Spring Boot + Mybatis Plus(SSMP)整合案例,希望能帮助到您。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值