189 8069 5689

SpringBoot整合Mybatis(一)

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

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



    4.0.0

    com.kyy.springboot.mybatis
    chapter_2
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.3.RELEASE
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        
        
            org.springframework.boot
            spring-boot-starter-test
            runtime
        
        
            MySQL
            mysql-connector-java
            runtime
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

第二步、配置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);
    }
}



    
        
        
        
    
    
        USER_SID,USER_CODE,USER_NAME
    
    

第四步、创建启动类

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(一)
文章分享:http://cdxtjz.cn/article/iidcoo.html

其他资讯