Spring Boot 整合MyBatis(注解版与XML版比较)

🎈博客主页:🌈我的主页🌈
🎈欢迎点赞 👍 收藏 🌟留言 📝 欢迎讨论!👏
🎈本文由 【泠青沼~】 原创,首发于 CSDN🚩🚩🚩
🎈由于博主是在学小白一枚,难免会有错误,有任何问题欢迎评论区留言指出,感激不尽!🌠个人主页



🌟 一、配置依赖项

        <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>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

🌟 二、MyBatis配置

🌟🌟 2.1、查询

注解:

    @Select("select * from user where id = #{id}")
    User getUserByid(int id);

XML:

 <resultMap id="userMap" type="com.dong.mybatis.model.User">
        <id property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="password" column="password"/>
    </resultMap>
    <select id="getUserByid" resultMap="userMap">
        select  * from user where id = #{id};
    </select>

注解:
 @Results({
            @Result(property = "password",column = "password")
    })
    @Select("select * from user")
    List<User> getAll();

XML:

 <select id="getAll" resultMap="userMap">
        select * from user;
 </select>

🌟🌟 2.2、插入

注解:

@Insert("insert into user (username,password)values(#{username},#{password})")
    @SelectKey(statement = "select last_insert_id()",keyProperty = "id",before = false,resultType = Integer.class)
    int addUser(User user);

XML:

 <insert id="addUser" parameterType="com.dong.mybatis.model.User" useGeneratedKeys="true" keyProperty="id">
        insert into user (username,password)values(#{username},#{password});
</insert>

🌟🌟 2.3、删除

注解:

   @Delete("delete from user where id = #{id}")
    int deletebyid(int id );

XML:

<delete id="deletebyid">
        delete from user where id = #{id};
</delete>

🌟🌟 2.4、更新

注解:

@Update("update user set  username = #{username} where id=#{id}")
    int updateUser(int id,String username);

XML:

    <update id="updateUser">
        update user set  username = #{username} where id=#{id};
    </update>

🌟 三、Usermapper.xml配置类路径

🌟🌟 3.1、配置文件放置在与接口文件同一目录下

在这里插入图片描述

🌟🌟 3.2、配置文件放在接口文件所在同一个包下

在这里插入图片描述

这时我们就需要配置POM.xml文件使路径生效,否则只会默认在Resources目录下

        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>

🌟🌟 3.3、配置文件放在Resources目录下mapper包下

在这里插入图片描述

  • application.properties配置:
mybatis.config-location=classpath:mapper/*.xml
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值