SpringBoot整合Mybatis(一)

第一步、創(chuàng)建Maven工程,配置POM

鐵嶺縣網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)2013年開(kāi)創(chuàng)至今到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專(zhuān)注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。

<?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

第三步、創(chuàng)建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>

第四步、創(chuàng)建啟動(dòng)類(lèi)

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);
    }
}

第五步、啟動(dòng)項(xiàng)目,訪問(wèn)http://localhost/findAuthUser/1003

輸出:{"userSid":1003,"userCode":"10000128","userName":"管理員"}

當(dāng)前文章:SpringBoot整合Mybatis(一)
本文路徑:http://muchs.cn/article0/iidcoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、品牌網(wǎng)站制作、商城網(wǎng)站搜索引擎優(yōu)化、ChatGPT做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司