java入门015~springboot2整合mybatis,轻松实现mysql数据的增删改查


 <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

往期回顾

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值