• 14.在SpringBoot框架集成MyBatis(mapper、mapperscan、xml和dao分离)


    通过 SpringBoot +MyBatis 实现对数据库学生表的查询操作。

    一、首先新建mysql数据库

     添加数据库数据

    二、新建springboot项目选择3这三个依赖

    项目结构

    1. mybatis起步依赖 : 完成mybatis对象自动配置, 对象放在容器中

    2. pom.xml 指定把src/main/java目录中的xml文件包含到classpath中

    详细的pom.xml文件

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.7.3version>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.itgroupId>
    12. <artifactId>017-springboot-mapperartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>017-springboot-mappername>
    15. <description>Demo project for Spring Bootdescription>
    16. <properties>
    17. <java.version>17java.version>
    18. properties>
    19. <dependencies>
    20. <dependency>
    21. <groupId>org.springframework.bootgroupId>
    22. <artifactId>spring-boot-starter-webartifactId>
    23. dependency>
    24. <dependency>
    25. <groupId>org.mybatis.spring.bootgroupId>
    26. <artifactId>mybatis-spring-boot-starterartifactId>
    27. <version>2.2.2version>
    28. dependency>
    29. <dependency>
    30. <groupId>mysqlgroupId>
    31. <artifactId>mysql-connector-javaartifactId>
    32. <scope>runtimescope>
    33. dependency>
    34. <dependency>
    35. <groupId>org.springframework.bootgroupId>
    36. <artifactId>spring-boot-starter-testartifactId>
    37. <scope>testscope>
    38. dependency>
    39. dependencies>
    40. <build>
    41. <resources>
    42. <resource>
    43. <directory>src/main/javadirectory>
    44. <includes>
    45. <include>**/*.xmlinclude>
    46. includes>
    47. resource>
    48. resources>
    49. <plugins>
    50. <plugin>
    51. <groupId>org.springframework.bootgroupId>
    52. <artifactId>spring-boot-maven-pluginartifactId>
    53. plugin>
    54. plugins>
    55. build>
    56. project>

    3. 创建实体类Student

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

    4. 创建Dao接口 StudentDao , 创建一个查询学生的方法

    1. package com.it.dao;
    2. import com.it.entity.Student;
    3. import org.apache.ibatis.annotations.Mapper;
    4. /**
    5. * 告诉MyBatis这是dao接口,创建此接口的代理对象。
    6. * 位置:在类的上面
    7. */
    8. @Mapper
    9. public interface StudentDao {
    10. //根据主键查询学生对象
    11. Student selectStudentById(Integer id);
    12. void selectStudentById();
    13. }

    5. 创建Dao接口对应的Mapper文件, xml文件, 写sql语句

    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.it.dao.StudentDao">
    6. <select id="selectStudentById" resultType="com.it.entity.Student">
    7. select id,name,age from student where id=#{id}
    8. select>
    9. mapper>

    6. 创建Service层对象, 创建StudentService接口和他的实现类。 去dao对象的方法。完成数据库的操作

    StudentService接口

    StudentService接口实现类

    7. 创建Controller对象,访问Service。

    1. package com.it.controller;
    2. import com.it.entity.Student;
    3. import com.it.service.StudentService;
    4. import org.springframework.stereotype.Controller;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import org.springframework.web.bind.annotation.ResponseBody;
    7. import javax.annotation.Resource;
    8. @Controller
    9. public class StudentController {
    10. @Resource
    11. private StudentService studentService;
    12. @RequestMapping("/student/query")
    13. @ResponseBody
    14. public String queryStudentById(Integer id){
    15. Student student = studentService.queryStudentById(id);
    16. return student.toString();
    17. }
    18. }

    8. 写application.properties文件

       配置数据库的连接信息。

    1. server.port=9001
    2. server.servlet.context-path=/orm
    3. #连接数据库
    4. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    5. spring.datasource.url=jdbc:mysql://localhost:3306/springboot1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
    6. spring.datasource.username=root
    7. spring.datasource.password=123456

    项目测试

    三、在SpringBoot框架集成MyBatis的三个方式,上面例子是用的第一种方法

     第一种方式 : @Mapper

    @Mapper:放在dao接口的上面, 每个接口都需要使用这个注解。

    1. /**
    2. * @Mapper:告诉MyBatis这是dao接口,创建此接口的代理对象。
    3. * 位置:在类的上面
    4. */
    5. @Mapper
    6. public interface StudentDao {
    7. Student selectById(@Param("stuId") Integer id);
    8. }

    第二种方式  @MapperScan

    在主方法函数类上面加上@MapperScan,使用于有多个dao文件时使用,不用再一个一个的在Dao接口中编写

    1. package com.it;
    2. import org.mybatis.spring.annotation.MapperScan;
    3. import org.springframework.boot.SpringApplication;
    4. import org.springframework.boot.autoconfigure.SpringBootApplication;
    5. /**
    6. * @MapperScan:找到Dao接口和Mapper文件
    7. * basePackages:Dao接口所在的包名
    8. */
    9. @SpringBootApplication
    10. @MapperScan(basePackages = "com.it.dao")
    11. public class Application {
    12. public static void main(String[] args) {
    13. SpringApplication.run(Application.class, args);
    14. }
    15. }

    第三种方式多用于项目实战,推荐使用

  • 相关阅读:
    程序运行过程中消耗的是堆内存还是栈内存还是其他?
    商城免费搭建之java商城 开源java电子商务Spring Cloud+Spring Boot+mybatis+MQ+VR全景+b2b2c
    Java基础面试题汇总(不定期更新)
    Golang Array 数组使用注意事项和细节
    Android之启动奔溃提示异常java.lang.SecurityException: Permission Denial: startForeground
    6.紧耦合和松耦合有什么区别?
    关于SpringBoot项目中读取不到自建email.yml配置文件内容的问题
    stm32使用通用定时器生成pwm
    day03-搭建微服务基础环境02
    springboot基于java的康泰小区物业管理系统的设计与实现毕业设计源码101926
  • 原文地址:https://blog.csdn.net/weixin_59334478/article/details/126691814