首页 > 其他分享 >mybatis的mapper映射文件

mybatis的mapper映射文件

时间:2022-12-14 14:37:02浏览次数:54  
标签:mapper student 映射 location Student mybatis hobby id

mybatis的mapper映射文件

MyBatis 的真正强大在于它的语句映射,这是它的魔力所在。由于它的异常强大,映射器的 XML 文件就显得相对简单。如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉了将近 95% 的代码。MyBatis 致力于减少使用成本,让用户能更专注于 SQL 代码。

 select标签

<select id="selectStudent1" resultType="su">
select * from student where id = #{id}
</select>

<select id="selectStudent2" parameterType="int" resultType="hashmap">
 SELECT * FROM student WHERE ID = #{id}
</select>

Student selectStudent1(Integer id);
HashMap selectStudent2(Integer id);

Insert标签
如果你的数据库支持自动生成主键的字段(比如 MySQL 和 SQL Server),那么你可以设置 useGeneratedKeys=”true”,然后再把 keyProperty 设置为目标属性就 OK 了。例如,如果上面的 Author 表已经在 id 列上使用了自动生成,那么语句可以修改为:

<insert id="insertStudent" useGeneratedKeys="true" keyProperty="id">
insert into student (name,location,hobby,age)
values (#{name},#{location},#{hobby},#{age})
</insert>

Student s=new Student();
s.setAge(10);
s.setLocation("上海");
s.setName("陈三龙");
s.setHobby("旅行");
sqlSession.insert("com.po.pf.repository.StudentMapper.insertStudent", s);
System.out.println(s.getId());

返回的主键为3

resultMap标签

<resultMap id="studentResultMap" type="su">
<id property="id" column="user_id" />
<result property="name" column="name"/>
<result property="location" column="location"/>
<result property="hobby" column="hobby"/>
<result property="age" column="age"/>
</resultMap>

<select id="selectStudent3" parameterType="int" resultMap="studentResultMap">
SELECT * FROM student WHERE ID = #{id}
</select>

Student student = sqlSession.selectOne("com.po.pf.repository.StudentMapper.selectStudent3", 1);
System.out.println(student.toString());

打印结果
Student(id=null, name=王泽坤, location=北京, hobby=游泳, age=21)

 

标签:mapper,student,映射,location,Student,mybatis,hobby,id
From: https://www.cnblogs.com/popopopopo/p/16982016.html

相关文章

  • myBatis的全局配置文件
    myBatis的全局配置文件mybatis封装需要的三要素数据源执行语句操作者SqlSessionFactoryBuilder第一步解析xml文件configuration(配置)001运行环境environmentdataSource......
  • 简单端口映射、转发、重定向工具之Rinetd
    ◆一、概述Rinetd是为在一个Unix和Linux操作系统中为重定向传输控制协议(TCP)连接的一个工具。将TCP连接从一个IP地址和端口重定向到另一个。它处理文件中/etc/rinetd......
  • mybatis入门案例
    创建模块,导入坐标:<properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></prop......
  • mybatis简化代码
    创建模块,导入坐标:<properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></propert......
  • RequestMappingHandlerMapping请求地址映射的初始化流程!
    之前的文章里,介绍了DispatcherSerlvet处理请求的流程。其中一个核心的步骤是:请求地址映射,即根据request获取对应的HandlerExcecutionChain。为了后续的请求地址映射,在项......
  • @Mapper 与 @MapperScan 的区别
    之前一直用Mapper,后来项目中用MapperScan,再后来一不小心两个一起用了,就出现问题了,本文讲解两个注解的作用和优先级。1、@Mapper作用用在接口类上,在编译之后会生成相......
  • Spring Boot MyBatis 通用Mapper插件集成 good
    看本文之前,请确保你已经在SpringBoot中集成MyBatis,并能正常使用。如果没有,那么请先移步做了解后,再按本文步骤操作。使用MyBatis在我们通过xml集中配置SQL,并通过创建接口Mapp......
  • Spring boot Mybatis
    最近刚接触Springboot,正是因为他的及简配置方便开发,促使我下定决心要用它把之前写的项目重构,那么问题来了,springboot怎么整合mybatis呢,下面几个配置类来搞定。在我的代码......
  • 分块内存映射处理大文件-例子
    内存映射文件可以用于3个不同的目的•系统使用内存映射文件,以便加载和执行.exe和DLL文件。这可以大大节省页文件空间和应用程序启动运行所需的时间。•可以使用内存映射......
  • mybatis
    mybatis代理类创建DefaultSqlSession#getMapperMapperRegistry#getMapperMapperProxyFactory#newInstanceProxy.newProxyInstance(,,newMapperProxy(newMapperM......