SpringBoot+Mybatis+MySql

(注:第一次写不喜勿喷)

1:前提安装好JDK ,IDEA,MAVEN 此处不多赘述 ,不会的同学请先进行学习

2:File→New →Project

SpringBoot+Mybatis+MySql_第1张图片    

SpringBoot+Mybatis+MySql_第2张图片    

SpringBoot+Mybatis+MySql_第3张图片

SpringBoot+Mybatis+MySql_第4张图片    

SpringBoot+Mybatis+MySql_第5张图片    

3: pom.xml 信息



    4.0.0

    com.boot
    yunxiaodefengdemo
    0.0.1-SNAPSHOT
    jar

    yunXiaoDeFengDemo
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-test
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.0
        
        
        
            mysql
            mysql-connector-java
            runtime
        
    

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



4:在src\main\resources找到 application.properties 改名为application.yml

    注意:需要填写信息时请在冒号后面空格,否则数据无效

 
  
#端口号
server:
  port: 82

#数据库链接设置
spring:
 datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: root

#整合Mybatis
mybatis:
#  扫描数据持久对应的实体类 路径 Package级别
  type-aliases-package: com.boot.yunxiaodefengdemo.entity
#  mybatis 对应的 .xml文件路径
  mapper-locations: classpath:mapper/*.xml
#  多层级目录  mapper-locations: classpath:mapper/**/**.xml


#  打印 sql 扫描dao层
logging:
  level:
    com:
      boot:
       yunxiaodefengdemo:
        Dao:  debug

5:  创建controlelr 、service、dao、entity

SpringBoot+Mybatis+MySql_第6张图片    SpringBoot+Mybatis+MySql_第7张图片

(Service,Dao,entity同上步骤)

6:Controller中创建接口,Service中创建接口和实现类,Dao创建接口,Entity创建User实体

Controller代码如下              

package com.boot.yunxiaodefengdemo.Controller;

import com.boot.yunxiaodefengdemo.Entity.User;
import com.boot.yunxiaodefengdemo.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 〈一句话功能简述〉
* 〈〉 * * @author yunXiaoDeFeng * @create 2018/7/2 * @since 1.0.0 */ @RestController @RequestMapping("user") public class UserController { @Autowired private UserService userService; @RequestMapping(method = RequestMethod.GET) public List hello(){ List hello=null; try { hello = userService.hello( ); }catch (Exception e){ System.out.println(e ); } return hello; } }

Service代码如下

    接口

package com.boot.yunxiaodefengdemo.Service;

import com.boot.yunxiaodefengdemo.Entity.User;

import java.util.List;

/**
 * 〈一句话功能简述〉
* 〈〉 * * @author yunXiaoDeFeng * @create 2018/7/2 * @since 1.0.0 */ public interface UserService { List hello()throws Exception; }

    实现类

package com.boot.yunxiaodefengdemo.Service.impl;

import com.boot.yunxiaodefengdemo.Dao.UserMapper;
import com.boot.yunxiaodefengdemo.Entity.User;
import com.boot.yunxiaodefengdemo.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 〈一句话功能简述〉
* 〈〉 * * @author yunXiaoDeFeng * @create 2018/7/2 * @since 1.0.0 */ @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List hello()throws Exception { return userMapper.hello(); } }

Dao代码如下

package com.boot.yunxiaodefengdemo.Dao;

import com.boot.yunxiaodefengdemo.Entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 〈一句话功能简述〉
* 〈〉 * * @author yunXiaoDeFeng * @create 2018/7/2 * @since 1.0.0 */ @Mapper //注意:别忘了此注解 注入Bean public interface UserMapper { List hello()throws Exception; }

Entity代码如下

package com.boot.yunxiaodefengdemo.Entity;

/**
 * 〈一句话功能简述〉
* 〈〉 * * @author yunXiaoDeFeng * @create 2018/7/2 * @since 1.0.0 */ public class User { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }    

usermapper.xml





  
  id,name
  
          
  
  


7 最终的包结构

SpringBoot+Mybatis+MySql_第8张图片    

8:控制台打印 为以下信息证明启动成功

SpringBoot+Mybatis+MySql_第9张图片    

9:浏览器 输入http://127.0.0.1:82/user


10 : 控制台打印出sql


你可能感兴趣的:(SpringBoot+Mybatis+MySql)