🎈博客主页:🌈我的主页🌈
🎈欢迎点赞 👍 收藏 🌟留言 📝 欢迎讨论!👏
🎈本文由 【泠青沼~】 原创,首发于 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
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/15747.html