使用idea + maven + +Spring Boot + mysql + MyBatis 快速搭建起SSM框架(SpringBoot 入門案例)

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

}

可以看到,自動注入時會提示有紅色波浪線,這裏不是語法錯誤,就這樣也是可以運行的。但是這樣看起來確實不爽,可參照以下博文解決IntelliJ Idea解決Could not autowire. No beans of 'xxxx' type found的錯誤提示

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厲害的地方了,下面運行項目

運行結果:

(備註:數據庫那邊要自己建

 

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

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章