zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    ubuntu下安装chromium浏览器adobe flash插件
    Eclipse运行时提示failed to create the java virtual machine 解决方法
    64位Ubuntu下不能生成R.java文件的问题(Android)
    Ubuntu下Eclipse的中文支持(GBK)
    Android工程编译错误:The project cannot be built until build path errors are resolved
    Apache POI
    ubuntu下安装Google拼音输入法
    [导入]ThinkPHP新手推荐学习路线
    [导入]示例Blog发表日志的程序流程(总结)
    [导入][视频] ThinkPHP入门第一步
  • 原文地址:https://www.cnblogs.com/jizhong/p/13974924.html
Copyright © 2011-2022 走看看