SpringBoot整合Mybatis(一)

发布时间:2020-06-26 11:10:23 作者:恋上程序员
来源:网络 阅读:501

第一步、创建Maven工程,配置POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.kyy.springboot.mybatis</groupId>
    <artifactId>chapter_2</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

第二步、配置application.yml和application-dev.yml文件

spring:
  profiles:
    active: dev
server:
  port: 80

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-package: com.kyy.springboot.mybatis.entity

logging:
  level:
   com:
    kyy:
     springboot:
      mybatis:
       mapper: debug

第三步、创建entity、mapper、service、controller文件

package com.kyy.springboot.mybatis.entity;

/**
 * Auth: zhouhongliang
 * Date:2019/8/4
 */
public class AuthUser {
    private Long userSid;
    private String userCode;
    private String userName;

    public Long getUserSid() {
        return userSid;
    }

    public void setUserSid(Long userSid) {
        this.userSid = userSid;
    }

    public String getUserCode() {
        return userCode;
    }

    public void setUserCode(String userCode) {
        this.userCode = userCode;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}
package com.kyy.springboot.mybatis.mapper;

import com.kyy.springboot.mybatis.entity.AuthUser;
import org.springframework.stereotype.Repository;

/**
 * Auth: zhouhongliang
 * Date:2019/8/4
 */
@Repository
public interface AuthUserMapper {
    public AuthUser selectAuthUser(Long userSid);
}
package com.kyy.springboot.mybatis.service;

import com.kyy.springboot.mybatis.entity.AuthUser;
import com.kyy.springboot.mybatis.mapper.AuthUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Auth: zhouhongliang
 * Date:2019/8/4
 */
@Service
public class AuthUserService {
    @Autowired
    private AuthUserMapper authUserMapper;
    public AuthUser getAuthUser(Long userSid){
        return authUserMapper.selectAuthUser(userSid);
    }
}
package com.kyy.springboot.mybatis.controller;

import com.kyy.springboot.mybatis.entity.AuthUser;
import com.kyy.springboot.mybatis.service.AuthUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Auth: zhouhongliang
 * Date:2019/8/4
 */
@RestController
public class AuthUserController {
    @Autowired
    private AuthUserService authUserService;
    @RequestMapping("/findAuthUser/{userSid}")
    public AuthUser findAuthUser(@PathVariable Long userSid){
        return authUserService.getAuthUser(userSid);
    }
}
<?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.kyy.springboot.mybatis.mapper.AuthUserMapper">
    <resultMap id="BaseResultMap" type="com.kyy.springboot.mybatis.entity.AuthUser">
        <id column="USER_SID" property="userSid" jdbcType="INTEGER"/>
        <result column="USER_CODE" property="userCode" jdbcType="VARCHAR"/>
        <result column="USER_NAME" property="userName" jdbcType="VARCHAR"/>
    </resultMap>
    <sql id="Base_Column_List">
        USER_SID,USER_CODE,USER_NAME
    </sql>
    <select id="selectAuthUser" resultMap="BaseResultMap" parameterType="java.lang.Long">
        SELECT
        <include refid="Base_Column_List"/>
        FROM auth_user
        WHERE USER_SID=#{userSid,jdbcType=INTEGER}
    </select>
</mapper>

第四步、创建启动类

package com.kyy.springboot.mybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Auth: zhouhongliang
 * Date:2019/8/4
 */
@SpringBootApplication
@MapperScan(basePackages = {"com.kyy.springboot.mybatis.mapper"})
public class ApplicationStart {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationStart.class,args);
    }
}

第五步、启动项目,访问http://localhost/findAuthUser/1003

输出:{"userSid":1003,"userCode":"10000128","userName":"管理员"}

推荐阅读:
  1. springboot整合mybatis
  2. SpringBoot中怎么整合Mybatis

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot mbatis @mapperscan

上一篇:java中要生成不重复随机数的方法

下一篇:基于 MVP 架构使用Android通用开发框架快速开发微博项目实战

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》