• SpringBoot学习(5) —— SpringBoot框架集成MyBatis


    一. 添加MyBatis依赖

    在pom.xml中添加mybatis依赖和mysql驱动 

    1. <dependency>
    2. <groupId>org.mybatis.spring.bootgroupId>
    3. <artifactId>mybatis-spring-boot-starterartifactId>
    4. <version>2.2.2version>
    5. dependency>
    6. <dependency>
    7. <groupId>mysqlgroupId>
    8. <artifactId>mysql-connector-javaartifactId>
    9. <scope>runtimescope>
    10. dependency>

    二. 连接数据库

     在application.properties中添加以下信息:

    1. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    2. spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
    3. spring.datasource.username=root
    4. spring.datasource.password=123456

    注意:spring.datasource.url=jdbc:mysql://localhost:3306/springboot中springboot是数据库名称

    三. 引入mapper接口的映射文件

     1. 在pom.xml中引入src/main/resouces目录下所有xml文件: 

         mapper接口的映射文件通常存放在src/main/resouces目录下

    1. <build>
    2. <resources>
    3. <resource>
    4. <directory>src/main/resourcesdirectory>
    5. <includes>
    6. <include>**/*.*include>
    7. includes>
    8. resource>
    9. resources>

     2.  在application.properties中添加以下信息来指定映射文件存放在resources目录下的那个包内

         通常自定义创建一个包,用于存放映射文件。(这里在resources目录下创建了一个mapper包)

    mybatis.mapper-locations=classpath:mapper/*.xml

    3. 添加日志

     在application.properties中添加以下信息:

    mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

    四. 创建实体类对象

    创建数据库表所对应的实体类对象

    1. package com.xdu.studyspringboot.pojo;
    2. public class User {
    3. private Integer id;
    4. private String name;
    5. private Integer age;
    6. public Integer getId() {
    7. return id;
    8. }
    9. public void setId(Integer id) {
    10. this.id = id;
    11. }
    12. public String getName() {
    13. return name;
    14. }
    15. public void setName(String name) {
    16. this.name = name;
    17. }
    18. public Integer getAge() {
    19. return age;
    20. }
    21. public void setAge(Integer age) {
    22. this.age = age;
    23. }
    24. @Override
    25. public String toString() {
    26. return "User{" +
    27. "id=" + id +
    28. ", name='" + name + '\'' +
    29. ", age=" + age +
    30. '}';
    31. }
    32. }

    五. Dao层

    1. 创建Mapper接口 

    1. package com.xdu.studyspringboot.mapper;
    2. import com.xdu.studyspringboot.pojo.User;
    3. import org.apache.ibatis.annotations.Mapper;
    4. import org.apache.ibatis.annotations.Param;
    5. @Mapper
    6. public interface UserMapper {
    7. User selectById(@Param("id") Integer id);
    8. }

    2. 创建Mapper接口的映射文件

    在src/main/resources/mapper目录下创建与Mapper接口同名的xml文件,即UserMapper.xml

    1. "1.0" encoding="UTF-8" ?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.xdu.studyspringboot.mapper.UserMapper">
    6. <select id="selectById" resultType="com.xdu.studyspringboot.pojo.User">
    7. select * from user where id=#{id}
    8. select>
    9. mapper>

    六. Service层

    1. 创建Service接口

    1. package com.xdu.studyspringboot.service;
    2. import com.xdu.studyspringboot.pojo.User;
    3. public interface UserService {
    4. User queryUserById(Integer id);
    5. }

    2. 实现Service接口

    1. package com.xdu.studyspringboot.service.impl;
    2. import com.xdu.studyspringboot.mapper.UserMapper;
    3. import com.xdu.studyspringboot.pojo.User;
    4. import com.xdu.studyspringboot.service.UserService;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Service;
    7. @Service
    8. public class UserServiceImpl implements UserService {
    9. @Autowired
    10. private UserMapper userMapper;
    11. @Override
    12. public User queryUserById(Integer id) {
    13. return userMapper.selectById(id);
    14. }
    15. }

    七. 测试

    1. package com.xdu.studyspringboot.controller;
    2. import com.xdu.studyspringboot.pojo.User;
    3. import com.xdu.studyspringboot.service.UserService;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.stereotype.Controller;
    6. import org.springframework.web.bind.annotation.RequestMapping;
    7. import org.springframework.web.bind.annotation.ResponseBody;
    8. @Controller
    9. public class UserController {
    10. @Autowired
    11. private UserService userService;
    12. @RequestMapping("/user")
    13. @ResponseBody
    14. public String queryUser(){
    15. Integer id = 1;
    16. User user = userService.queryUserById(id);
    17. return user.toString();
    18. }
    19. }
  • 相关阅读:
    Linux Vim 进阶教程
    linux base64编码、解码
    挑战杯 大数据疫情分析及可视化系统
    数学建模介绍
    Java之JvisualVM简介
    【案例实践】HEC-RAS 1D/2D水动力与水环境模拟、HEC-RAS与HEC-FDA耦合、桥梁分析、泄洪道设定
    Facebook广告投放经常被问的几个问题
    前端HTML点击图片放大效果展示
    C# 判断电脑是否联网
    关于python中有关一个子类继承多个父类的实例属性绑定问题
  • 原文地址:https://blog.csdn.net/Archer__13/article/details/126871084