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