栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合mybatis快速入门

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合mybatis快速入门

本文采用springboot整合orm框架mybatis,对数据库进行增删改查操作。

一、目录结构

给出springboot整合mybatis框架的项目目录,目录大致分为4层,controller层(控制层)、service(业务层),mapper(数据操作层),persist(实体层)

二、引入依赖

在相应的pom.xml里面引入以下依赖:web依赖、mysql驱动依赖、mybatis框架依赖、lombok依赖。其中,lombok可以简化代码,在对应实体类上加入@Data注解,可以省略属性get、set方法。


        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            mysql
            mysql-connector-java
            runtime
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.0
        

        
        
            org.projectlombok
            lombok
            1.18.8
            provided
        
    
三、使用mybatis进行操作(以添加为例)         1.application.yml

        mybatis需要设置mapper的加载位置,使用mapper-locations来进行设置,使用map-underscore-to-camel-case来设置是否开启驼峰模式。

        驼峰模式指的是:将下划线后的第一个字母变成大写,即user_name->userName

        spring下的datasource是对数据源进行配置。

server:
  port: 8080
#mybatis
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=true&allowMultiQueries=true
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  #mapper
  mapper-locations: classpath:mapper
    private String username;

    
    private String password;
}
        3.UserController.class

        用户控制器:@RestController注解由@Controller和@ResponseBody两个注解组成,其中@ResponseBody可以使返回的结果变成json字符串,而不是视图model。

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/create")
    public String create(@RequestBody User user){
        userService.create(user);
        return "success";
    }
}
        4.UserService.class

        用户业务层:对于业务层来讲,不需要try catch方法,由于SpringBoot可以使用@ControllerAdvice和@ExceptionHandler注解来进行全局异常处理,因此,这里不需要进行try catch方法,对异常进行捕捉。

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public void create(User user) {
        userMapper.create(user);
    }
}
        5.UserMapper.interface
@Repository
public interface UserMapper {

    
    int createUser(User user);
}
        6.user-mapper.xml

         这里的namespace需要手动进行设置,由于本项目UserMapper所在全路径为cn.com.springboot.mapper.UserMapper,因此,namespace设置为这个。mybatis可以使用${XXXX}和#{XXXX}两种方式对属性进行识别,但是为了防止SQL注入,尽量使用#{XXXX}来对属性进行注入。





    
        insert into user(username,password) values(#{username},#{password})
    


        7.Main.class

        这里,在主函数里面需要加入@MapperScan注解,对mapper下的UserMapper.interface来进行扫描

@SpringBootApplication
@MapperScan(basePackages = "cn.com.springboot")
public class Main {

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}
        8.user表(以mysql为例)

四、使用postman工具来进行测试

        请求方法:post

        请求路径: http://localhost:8080/user/create

        请求体:

                        {

                            "username":"admin",

                            "password":"123456"

                        }

        请求结果,可见请求成功,查看数据库:

        查看数据库可得,插入成功:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/785386.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号