SpringBoot整合Mybatis(一)

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

铁岭县网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设等网站项目制作,到程序开发,运营维护。创新互联2013年开创至今到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。

<?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":"管理员"}

当前文章:SpringBoot整合Mybatis(一)
本文路径:https://www.cdcxhl.com/article0/iidcoo.html

成都网站建设公司_创新互联,为您提供网站排名品牌网站制作商城网站搜索引擎优化ChatGPT做网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设公司