• spring boot集成mybatis(xml配置模式)


    一.项目所需材料:

    spring boot、mybatis

    pom文件配置如下:

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.4.0</version>
            <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.38</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.0.1</version>
            </dependency>
        </dependencies>

    二.项目目录结构

    三.项目代码

    UserEntity.java

    import java.io.Serializable;
    
    public class UserEntity implements Serializable {
    
        private Long id;
    
        private String user_name;
    
        private String passWord;
    
        private String realName;
    
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getUser_name() {
            return user_name;
        }
    
        public void setUser_name(String user_name) {
            this.user_name = user_name;
        }
    
        public String getPassWord() {
            return passWord;
        }
    
        public void setPassWord(String passWord) {
            this.passWord = passWord;
        }
    
        public String getRealName() {
            return realName;
        }
    
        public void setRealName(String realName) {
            this.realName = realName;
        }
    }

    UserMapper.java

    @Repository
    public interface UserMapper {
        UserEntity queryUser(int id);
    }

    UserService.java

    @Service
    public class UserService {
    
        @Autowired
        UserMapper userMapper;
    
        public UserEntity Sel(int id) {
            return userMapper.queryUser(id);
        }
    }

    UserMapper.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="***.com.mybatis14.mapper.UserMapper">
    
           <!-- <resultMap id="BaseResultMap" type="***.com.mybatis14.entity.UserEntity">
                <result column="id" jdbcType="INTEGER" property="id" />
                <result column="user_name" jdbcType="VARCHAR" property="user_name" />
                <result column="passWord" jdbcType="VARCHAR" property="passWord" />
                <result column="realName" jdbcType="VARCHAR" property="realName" />
            </resultMap>-->
    
        <select id="queryUser" resultType="***.com.mybatis14.entity.UserEntity">
            select * from user where id = #{id}
        </select>
    
    </mapper>

    说明:

    1.resultMap部分可以写,也可以不写,均能正常运行。

    2.select id需要与具体的Mapper中的方法定义相对应(本示例为:queryUser)。

    UserController.java

    @RestController
    @RequestMapping("/user")
    public class UserController {
    
        @Autowired
        private UserService userService;
    
        @RequestMapping("/get")
        public UserEntity getUserByName(int id) {
            return userService.Sel(id);
        }
    }

    application.properties

    spring.datasource.url=jdbc:mysql://localhost:3306/business?useUnicode=true&characterEncoding=utf8&useSSL=false
    spring.datasource.username=root
    spring.datasource.password=******
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    mybatis.mapper-locations=classpath:mapping/*Mapper.xml
    #mybatis.type-aliases-package=***.com.mybatis14.entity

    说明:mybatis.type-aliases-package=***.com.mybatis14.entity可配也可以不配,均能正常运行。

    Mybatis14Application.java

    @MapperScan("***.com.mybatis14.mapper")
    @SpringBootApplication
    public class Mybatis14Application {
        public static void main(String[] args) {
            SpringApplication.run(Mybatis14Application.class, args);
        }
    }

    四.启动并测试

    输入:http://localhost:8080/user/get?id=1

    返回结果:

    参考:https://www.cnblogs.com/lvchengda/p/12619520.html

  • 相关阅读:
    项目依赖库列表文件requirements.txt生成和使用
    pymysql执行sql语句无效问题
    Prometheus
    关于在安装wampserver出现“由于找不到msvcr110.dll”的问题的解决办法
    安装centos8和Window10出现的一些问题和解决方法
    LeetCode833题:字符串中的查找与替换
    Pycharm中github的使用(只有链接供自己学习使用)
    机器学习《西瓜书》的学习笔记——机器学习使用的领域
    机器学习中数理统计与参数估计的相关基础概念
    怎样在Anaconda中的某一个环境中安装Python的相关包(pypyodbc)
  • 原文地址:https://www.cnblogs.com/jizhong/p/13974924.html
Copyright © 2020-2023  润新知