1.首先開啟idea開發工具,然後File->New->Project  ,如下圖,下一步

預設配置,下一步

勾選以下三項,下一步

輸入專案名稱,Finish。。然後靜靜等待專案載入完成。。。

載入完成後生成如下目錄,其中DemoApplication是程式入口(我不知道這麼形容準不準確)

=======================================

新建包和類,注意,新建的包要跟DemoApplication為同級目錄!!!原因的話自己百度去

User.java

package com.example.demo.domain;

/**
 *  使用者實體類
 */

public class User {

    private int id;
    private String name;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

UserMapper.xml

<?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.example.demo.dao.UserDao" >

    <select id="findUserById"  resultType="com.example.demo.domain.User">
        SELECT * FROM user WHERE id = #{id}
    </select>

</mapper>

UserDao.java

package com.example.demo.dao;

import com.example.demo.domain.User;
import org.apache.ibatis.annotations.Mapper;

/**
 * 使用者Dao介面
 * */
//@Repository
@Mapper
public interface UserDao {

    User findUserById(int id);

}

UserService.java

package com.example.demo.service;

import com.example.demo.dao.UserDao;
import com.example.demo.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserDao userdao;

    public User findUserById(int id) {
        User user = userdao.findUserById(id);
        return user;
    }

}

UserCpntroller.java

package com.example.demo.controller;

import com.example.demo.domain.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/find")
    public User find () {
        User user = userService.findUserById(2);
        System.out.print(user);
        return user;//返回的是json格式的資料
    }

}

修改配置檔案application.properties

#資料連線
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=546784
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#Mybatis掃描
mybatis.mapper-locations=classpath*:mapper/*.xml

========================

以上就是全部程式碼,是不是幾乎完全沒有看到配置檔案?這就是SpringBoot厲害的地方了,下面執行專案

執行結果:

(備註:資料庫那邊要自己建

==================================

以上就是全部程式碼了。。。歡迎高手指教