详解Mybatis(五)Mapper接口
更新时间:2019年04月09日 11:55:06 作者:bettermanZYQ
这篇文章主要介绍了Mybatis Mapper接口,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
(1)Mapper接口和原理
Mapper组建
- 1、Mapper文件和Mapper接口应该放在同一个接口中
- 2、Mapper文件中的namespace应该设置为Mapper接口的全限定名称
- 3、Mapper文件中的操作元素ID对应Mapper接口的方法名称
Mapper原理:
动态代理
(2)配置文件
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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)
-->
<mapper namespace="Mybatis.domain.Mapper.UserMapper">
<resultMap type="User" id="BaseResultMap">
<result column="t_id" property="id"/>
<result column="t_name" property="name"/>
<result column="t_salary" property="salary"/>
</resultMap>
<!-- 保存操作 -->
<insert id="save" useGeneratedKeys="true" keyProperty="id">
INSERT INTO t_user (name , salary) VALUES (#{name},#{salary})
</insert>
<!-- 更改操作 -->
<update id="update">
update t_user where name=#{name},salary=#{salary} where id=#{id}
</update>
<!-- 删除操作 -->
<delete id="delete" >
delete from t_user where id=#{id}
</delete>
<!-- 查询单个操作 -->
<select id="select" parameterMap="java.lang.Long" resultType="Mybatis.domain.User">
select * from t_user where id = #{id}
</select>
<!-- 查询多个操作 -->
<select id="selectAll" resultType="User">
select id,name,salary from t_user
</select>
</mapper>
UserMapper.java
import java.util.List;
import Mybatis.domain.User;
public interface UserMapper {
void save(User u);
void update(User u);
void delete(Long id);
User select(User u);
List<User> selectAll();
}
以上所述是小编给大家介绍的Mybatis Mapper接口详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章:
- Mybatis Mapper接口工作原理实例解析
- Mybatis MapperScannerConfigurer自动扫描Mapper接口生成代理注入到Spring的方法
- 详解mybatis通过mapper接口加载映射文件
- mybatis如何通过接口查找对应的mapper.xml及方法执行详解
- 详解MyBatis的getMapper()接口、resultMap标签、Alias别名、 尽量提取sql列、动态操作
- 在IDEA中安装MyBatis Log Plugin插件,执行mybatis的sql语句(推荐)
- mybatis 批量将list数据插入到数据库的实现
- Mybatis mapper接口动态代理开发步骤解析
相关文章
springboot配置请求超时时间(Http会话和接口访问)
本文主要介绍了springboot配置请求超时时间,包含Http会话和接口访问两种,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2024-07-07
通过FeignClient调用微服务提供的分页对象IPage报错的解决
这篇文章主要介绍了通过FeignClient调用微服务提供的分页对象IPage报错的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-03-03
消息队列 RabbitMQ 与 Spring 整合使用的实例代码
本篇文章主要介绍了消息队列 RabbitMQ 与 Spring 整合使用的实例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-08-08


最新评论