java入門015~springboot2整合mybatis,輕鬆實現mysql數據的增刪改查

前面我們講完了一些java和springboot的基礎知識以後,今天我們就來講下springboot實現數據庫的管理。

目前比較主流的方式有兩種

1,springboot結合mybatis管理數據庫
2,springboot結合jpa管理數據

這兩種方式各有各的好,今天我們就先來講講springboot2結合mybatis實現數據的增刪改查操作,下一節我們再講jpa。

一,在pom.xml裏添加mybatis依賴。


如上圖所示,我們需要添加mybatis和mysql這兩個依賴。代碼如下

        <!-- mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>LATEST</version>
        </dependency>
        <!--  mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

這裏做下簡單說明
mybatis庫是我們實現mybatis的核心。
mysql庫,是用來驅動管理數據庫用的。

二,在application.yml裏配置如下信息


上圖就是做了,本地服務器端口配置,數據庫關聯配置。重要的是下面這段。

mybatis:
  mapper-locations: classpath:/mybatis/mapper/*.xml
  config-location:  classpath:/mybatis/config/mybatis-config.xml
  • mapper-locations 是配置我們mapper.xml文件的位置,我們把它配置在/src/main/resource/mybatis/mapper目錄下。

  • config-locations 是配置mybatis-confg.xml文件的位置。我們把它配置在/src/main/resource/mybatis/config目錄下。

接下來我們在resource目錄下新建mybatis目錄,然後在mybatis目錄下新建/mapper目錄和/config目錄

三,配置mybatis的配置xml文件

1,在/src/main/resource/mybatis/config目錄下新建mybatis-config.xml

具體代碼如下。

把代碼給大家貼出來。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <!-- 數據庫表對應的bean   -->
    <typeAliases>
        <package name="com.shitou.springbootdemos.mybatis.bean"/>
    </typeAliases>
</configuration>

這裏要注意的是,typeAliases下的package配置的是我們實體類所在目錄。

 <typeAliases>
        <package name="com.shitou.springbootdemos.mybatis.bean"/>
 </typeAliases>

如我們的User實體類,在下圖所示的這個目錄

2,在/src/main/resource/mybatis/mapper目錄下新建UserMapper.xml

這個UserMapper.xml裏就是我們要執行的一些sql語句。先把代碼給大家貼出來。

<?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">
<!--聲明對應bean的命名空間-->
<mapper namespace="com.shitou.springbootdemos.mybatis.repository.UserMapper">

    <!--    查詢到的結果返回樣式-->
    <resultMap id="SysUserResultMap" type="User">
        <id property="id" column="id" javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result property="name" column="name" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result property="age" column="AGE" javaType="java.lang.Integer" jdbcType="INTEGER"/>
    </resultMap>

    <!--增-->
    <insert id="save" parameterType="User">
        insert into user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="age != null">
                age
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER},
            </if>
            <if test="name != null">
                #{name,jdbcType=VARCHAR},
            </if>
            <if test="age != null">
                #{age,jdbcType=INTEGER},
            </if>
        </trim>
    </insert>

    <!--    刪-->
    <delete id="deleteById">
        delete from user where id=#{id}
    </delete>

    <!--    改-->
    <update id="update" parameterType="User">
        update user
        <set>
            <if test="name != null">
                name = #{name,jdbcType=VARCHAR},
            </if>
            <if test="age != null">
                age = #{age,jdbcType=INTEGER},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>


    <!--    查-->
    <select id="selectAll" resultMap="SysUserResultMap">
        select * from user
    </select>
    <select id="selectById" resultMap="SysUserResultMap">
        select
        *
        from user
        where id = #{id,jdbcType=INTEGER}
    </select>
</mapper>

四,創建User實體類。

package com.shitou.springbootdemos.mybatis.bean;

/**
 * user數據表對應的bean
 */
public class User {
    private Integer id;
    private String name;
    private Integer age;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

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

我們這裏就簡單的創建一個user類,和我們的user表對應,只有id,name,age三個字段

五,創建UserMapper接口,用來做增刪改查操作


代碼貼出來給大家

/**
 * user表對應的mapper
 */
public interface UserMapper {
    //新增用戶

    int save(User user);

    //更新用戶信息
    int update(User user);

    //根據id刪除
    int deleteById(int id);

    //根據id查詢
    User selectById(int id);

    //查詢所有用戶信息
    List<User> selectAll();
}

六,創建user表。

我創建數據庫表是用idea自帶的可視化管理工具創建的,我前面有寫文章講過,如果藉助idea來可視化管理mysql數據庫。大家可以去翻看我之前的文章。

可視化建表

這時候mybatis的配置還不算成功,我們需要把mapper的路徑暴露給spring 讓它來掃描管理,所以我們需要在Chapter4Application.java文件上加上註解@MapperScan(“com.shitou.springbootdemos.mybatis.repository”) 掃描mapper的所在位置

七,在啓動類裏自動掃描我們定義的mapper接口


至此。我們mybaits的接口算是編寫完畢,接下來我們來測試一下吧。

八,定義controller

如上圖所示,我們定義一個controller來實現一個增加數據的接口。和一個查詢所有用戶的接口。
1, 啓動項目。

2, 在瀏覽器中訪問save接口

返回1代表添加新用戶成功,到user表裏看下,也能看到數據添加成功

3,在瀏覽器裏訪問getAll接口

可以看到我們成功的訪問到我們上一步添加的數據。

到這裏我們就輕鬆的實現了springboot2結合mybatis來管理數據庫的功能了

源碼:

https://github.com/qiushi123/springboot-demos

視頻講解

https://edu.csdn.net/course/detail/23443

往期回顧

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