SpringBoot之整合Mybatis

一:什麼是Mybatis。html

  mybatis官方網站(http://www.mybatis.org/mybatis-3/zh/index.html)中是這樣描述的:java

MyBatis 是一款優秀的持久層框架,它支持定製化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎全部的 JDBC 代碼和手動設置參數以及獲取結果集。MyBatis 能夠使用簡單的 XML 或註解來配置和映射原生信息,將接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java對象)映射成數據庫中的記錄。mysql

  相信已經有不少朋友用過,或者是正在使用mybatis了,那麼接下來咱們就整合到Spring boot中去吧。git

  若是你不瞭解spring boot,請看一下,個人spring boot簡單示例文章吧:目錄 , SpringBoot之簡單入門github

二:開始搭建web

  1,打開eclipse。依次點擊 File -> New -> Other。選擇 Spring Starter Project ,而後Nextspring

    

  2.填寫項目信息,而後Nextsql

    

  3.選擇Spring boot版本,在編寫這篇文章的時候,spring boot的最新版本是2.0.5,因此就使用了這個版本。數據庫

    

  4.選擇項目依賴,選擇web,mysql驅動包,mybatisapache

    

    

  5.最後點擊Finish。等待一會,項目就建立完成了。建立好的項目目錄以下:

    

  咱們也來看一下pom.xml中的內容:能夠看到,mysql的驅動包和mybatis已經有了。

  

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zcz</groupId>
    <artifactId>learnSpringBootWithMybatis2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>learnSpringBootWithMybatis2</name>
    <description>learnSpringBootWithMybatis</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

  6.接下來就要寫代碼了。新建controller,entity,dao,servcie,service.impl包。在資源文件夾中新建mapper包。在資源文件夾中新建application.yml文件。新建完成後的文件目錄以下圖:

    

  7.編寫application.yml配置文件。內容以下

    

# 默認使用開發配置
spring:
  profiles:
    active: dev
    
#配置mybatis參數
mybatis:
  type-aliases-package: com.zcz.entity
  mapper-locations: classpath:mapper/*.xml
  
--- 
  
#開發配置
spring:
  profiles: dev

  datasource: 
    url: jdbc:mysql://localhost:3306/learnspringbootwithmybatis
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123 

  8.新建數據庫learnspringbootwithmybatis,並在數據庫中新建表user

    

  9.在entity包中新建User類,代碼以下:

    

package com.zcz.entity;

public class User {
    private int id;
    private String name;
    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;
    }
    
}

  10.在dao中,新建UserDao接口類,並添加@Mapper註解,代碼以下:

    類中用到了@Mapper註解,若是你不瞭解他們的用法和意義的話能夠參考 經常使用註解記錄

package com.zcz.dao;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.zcz.entity.User;

@Mapper
public interface UserDao {
    public List<User> findAllUser();
}

  11.在service中新建UserService 接口,代碼以下:

   

package com.zcz.service;

import java.util.List;

import com.zcz.entity.User;

public interface UserService {
    public List<User> getUsers();
}

 

  12.在service.impl中新建UserServiceImpl實現類,實現UserService接口,添加@Service註解,代碼以下:

    類中用到了@Service,@Autowired註解,若是你不瞭解他們的用法和意義的話能夠參考 經常使用註解記錄

package com.zcz.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcz.dao.UserDao;
import com.zcz.entity.User;
import com.zcz.service.UserService;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    
    @Override
    public List<User> getUsers() {
        // TODO Auto-generated method stub
        return userDao.findAllUser();
    }

}

13.在controller中新建UserController類,並配置訪問路徑,代碼以下:

  類中用到了@RestController,@RequestMapping註解,若是你不瞭解他們的用法和意義的話能夠參考 經常使用註解記錄

package com.zcz.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zcz.entity.User;
import com.zcz.service.UserService;

@RestController
@RequestMapping("/user")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @RequestMapping("/getUser")
    public List<User> getUsers(){
        return userService.getUsers();
    }
}

14,在mapper中新建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.zcz.dao.UserDao">
 
    <select id="findAllUser" resultType="com.zcz.entity.User">
        select id,name from user
    </select>
 
</mapper>

 

15,修改數據庫,添加兩天記錄

  

16,進入LearnSpringBootWithMybatis2Application類中,鼠標右鍵:Run as -> Spring Boot App。啓動項目。並訪問http://localhost:8080/user/getUser

  

17,好了,本次Spring Boot 整合Mybatis的過程就只有這些了。雖然這只是一個簡單的配置,可是咱們成功了,不是嗎?

  你們也發現了,這裏只有一個查詢的功能,接下來我將繼續實現增,刪,改的內容。在這裏:SpringBoot之整合Mybatis(增,改,刪)

 18,我把源代碼上傳到了github倉庫:https://github.com/ZCC1/learnSpringBootWithMybatis2.git。能夠clone下來參考。歡迎star。


原創不易,轉發請註明出處:https://www.cnblogs.com/zhangchengzi/p/9662247.html

相關文章
相關標籤/搜索