SpringBoot整合Mybatis(注解方式)

本是古典 何须时尚 2023-06-02 09:22 125阅读 0赞

SpringBoot整合Mybatis通常有两种方式,一种是以xml映射文件的,一种是以注解的方式实现的,这里主要是以注解的方式来实现

项目目录结构如下:

  1. src
  2. ----main
  3. --------java
  4. ------------com.example.app
  5. ----------------controller //控制器
  6. --------------------UserController.java
  7. ----------------entity //实体类
  8. --------------------User.java
  9. ----------------mapper //映射接口
  10. --------------------UserRepository.java
  11. ----------------service //业务
  12. --------------------UserService.java
  13. ----resource
  14. --------application.properties

首先先建一个表

  1. CREATE TABLE `stus` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `username` varchar(50) DEFAULT NULL,
  4. `password` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
  5. PRIMARY KEY (`id`)
  6. ) ENGINE=InnoDB AUTO_INCREMENT=42 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

然后随便插几条数据即可

application.properties配置如下:

  1. #端口配置
  2. server.port = 8080
  3. #数据库配置
  4. spring.datasource.url = jdbc:mysql://localhost:3306/数据库名?useSSL=false
  5. #用户名
  6. spring.datasource.username =
  7. #密码
  8. spring.datasource.password =
  9. #驱动
  10. spring.datasource.driver-class-name = com.mysql.jdbc.Driver

还是和以往一样先建一个实体类(User.java)

  1. package com.example.app.entity;
  2. /** * @author Woo_home * @create by 2019/10/4 */
  3. public class User {
  4. private int id;
  5. private String username;
  6. private String password;
  7. public int getId() {
  8. return id;
  9. }
  10. public void setId(int id) {
  11. this.id = id;
  12. }
  13. public String getUsername() {
  14. return username;
  15. }
  16. public void setUsername(String username) {
  17. this.username = username;
  18. }
  19. public String getPassword() {
  20. return password;
  21. }
  22. public void setPassword(String password) {
  23. this.password = password;
  24. }
  25. @Override
  26. public String toString() {
  27. return "User{" +
  28. "id=" + id +
  29. ", username='" + username + '\'' +
  30. ", password='" + password + '\'' +
  31. '}';
  32. }
  33. }

接着写一个mapper接口(UserRepository .java)

  1. package com.example.app.mapper;
  2. import com.example.app.entity.User;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import org.apache.ibatis.annotations.Select;
  5. import org.springframework.stereotype.Repository;
  6. import java.util.List;
  7. /** * @author Woo_home * @create by 2019/10/4 */
  8. @Mapper
  9. public interface UserRepository {
  10. @Select("select * from stus")
  11. List<User> getAllUser(); //查询所有用户
  12. }

接着就是业务的编写

  1. package com.example.app.service;
  2. import com.example.app.entity.User;
  3. import com.example.app.mapper.UserRepository;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import java.util.List;
  7. /** * @author Woo_home * @create by 2019/10/4 */
  8. @Service
  9. public class UserService {
  10. @Autowired
  11. private UserRepository userRepository;
  12. public List<User> getAllUser(){
  13. return userRepository.getAllUser();
  14. }
  15. }

控制器代码(UserController.java)

  1. package com.example.app.controller;
  2. import com.example.app.entity.User;
  3. import com.example.app.service.UserService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.stereotype.Repository;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import javax.annotation.Resource;
  10. import java.util.List;
  11. /** * @author Woo_home * @create by 2019/10/4 */
  12. @Controller
  13. public class UserController {
  14. @Autowired
  15. private UserService userService;
  16. @RequestMapping("/userList")
  17. @ResponseBody
  18. public List<User> userList(){
  19. return userService.getAllUser();
  20. }
  21. }

启动类
@MapperScan是在插件包中定义的注解,该注解的参数是一个包名字符串,只要在@MapperScan中配置需要扫描的Mybatis接口的包路径,Spring就会自动扫描包下面的java接口,把这些类注册为Spring的bean

  1. package com.example.app;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication
  6. @MapperScan("com.example.app.mapper")
  7. public class AppApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(AppApplication.class, args);
  10. }
  11. }

发表评论

表情:
评论列表 (有 0 条评论,125人围观)

还没有评论,来说两句吧...

相关阅读