SpringBoot 2.SpringBoot整合Mybatis,springbootmybatis

來源:互聯網
上載者:User

SpringBoot 2.SpringBoot整合Mybatis,springbootmybatis

一、建立Springboot的設定檔:application.properties,並添加MyBatis依賴

SpringApplication 會從 application.properties 檔案中載入配置資訊,下面是添加Spring配置資訊的檔案目錄順序:

  1. 目前的目錄下的/config子目錄中
  2. 目前的目錄中
  3. 一個 classpath 包下的 /config 目錄中
  4. classpath 根目錄中

 大家根據自己習慣來即可。

(1)application.properties 檔案配置如下:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1/test
spring.datasource.username=root
spring.datasource.password=root

(2)添加 MyBatis 以及 MYSQL

        <!-- SpringBoot - MyBatis -->        <dependency>            <groupId>org.mybatis.spring.boot</groupId>            <artifactId>mybatis-spring-boot-starter</artifactId>            <version>1.2.0</version>        </dependency>        <!-- Mysql -->        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>        </dependency>

 

二、建立POJO、Mapper、Service 以及 Controler

(1)User.java

package org.rcddup.app.domain;public class User {    private Long id;    private String name;    private Short age;    private Short status;    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Short getAge() {        return age;    }    public void setAge(Short age) {        this.age = age;    }    public Short getStatus() {        return status;    }    public void setStatus(Short status) {        this.status = status;    }}

(2)UserMapper.java

package org.rcddup.app.dao;import java.util.List;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;import org.rcddup.app.domain.User;@Mapperpublic interface UserMapper {        @Select("select id, name, age, status from user")    List<User> listUser();        @Select("select id, name, age, status from user where id=#{id}")    User getUserById(Long id);    }

(3)IUserService.java

package org.rcddup.app.service;import java.util.List;import org.rcddup.app.domain.User;public interface IUserService {        List<User> listUser();        User getUserById(Long id);    }

(4)UserService.java

package org.rcddup.app.service.impl;import java.util.List;import org.rcddup.app.dao.UserMapper;import org.rcddup.app.domain.User;import org.rcddup.app.service.IUserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class UserService implements IUserService{        @Autowired    private UserMapper userMapper;        @Override    public List<User> listUser() {        return userMapper.listUser();    }    @Override    public User getUserById(Long id) {        return userMapper.getUserById(id);    }    }

(5)UserController.java

package org.rcddup.app.web.controller;import java.util.List;import org.rcddup.app.domain.User;import org.rcddup.app.service.IUserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/user")public class UserController {        @Autowired    private IUserService userService;        @RequestMapping("/list")    public List<User> list() {        return userService.listUser();    }        @RequestMapping("/get")    public User list(Long id) {        return userService.getUserById(id);    }    }

  

  重新啟動 App.java

  輸入:http://localhost:8080/user/get?id=1

  輸入:http://localhost:8080/user/list

  到此,SpringBoot整合Mybatis就成功了。

    因為本人比較懶,就不配置Mapper.xml了,直接使用Mybatis的註解@Select 配置查詢語句。後續會講到Mybatis逆向工程,懶人(不是本人O(∩_∩)O)所造之物,非常贊,哈哈哈!!!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.