当前位置: 代码网 > it编程>编程语言>Java > MyBatis参数与SqlMapConfig.xml核心配置示例详解

MyBatis参数与SqlMapConfig.xml核心配置示例详解

2026年08月06日 Java 我要评论
一、parametertype参数类型1.1 简单数据类型int、double、string、long等。框架提供了简写方式,例如 java.lang.integer 可以简写为 int、intege

一、parametertype参数类型

1.1 简单数据类型

int、double、string、long等。框架提供了简写方式,例如 java.lang.integer 可以简写为 int、integer、int、integer 等。

<select id="findbyid" parametertype="int" resulttype="user">
    select * from user where id = #{id}
</select>

1.2 pojo对象类型

直接使用实体类的全路径或别名:

<insert id="insert" parametertype="com.qcbyjy.domain.user">
    insert into user (username) values (#{username})
</insert>

1.3 pojo包装对象类型

当需要传递多个实体类参数时,可以创建包装类:

public class queryvo implements serializable {
    private string name;
    private user user;
    private role role;
    // getter/setter省略
}

<select id="findbyvo" parametertype="com.qcbyjy.domain.queryvo" resulttype="user">
    select * from user where username = #{user.username}
</select>

二、resulttype结果类型

2.1 返回简单数据类型

int、double、long、string等:

<select id="findbycount" resulttype="int">
    select count(*) from user
</select>

2.2 返回pojo数据类型

直接返回实体类对象:

<select id="findbyid" resulttype="user">
    select * from user where id = #{id}
</select>

三、resultmap结果映射

当sql查询字段名和pojo的属性名不一致时,可以通过 resultmap 建立映射关系:

<!-- 使用resultmap -->
<select id="findusers" resultmap="usermap">
    select id _id, username _username, birthday _birthday, 
           sex _sex, address _address from user
</select>

<!-- 配置resultmap -->
<resultmap id="usermap" type="com.qcbyjy.domain.user">
    <result property="id" column="_id"/>
    <result property="username" column="_username"/>
    <result property="birthday" column="_birthday"/>
    <result property="sex" column="_sex"/>
    <result property="address" column="_address"/>
</resultmap>

resultmap配置说明:

  • property:javabean中的属性名
  • column:数据库表中的字段名

四、sqlmapconfig.xml核心配置

4.1 properties标签管理数据库信息

方式一:直接在配置文件中定义property标签

<properties>
    <property name="jdbc.driver" value="com.mysql.jdbc.driver"/>
    <property name="jdbc.url" value="jdbc:mysql:///mybatis_db"/>
    <property name="jdbc.username" value="root"/>
    <property name="jdbc.password" value="root"/>
</properties>

方式二(推荐):读取外部jdbc.properties文件

创建 jdbc.properties 文件:

jdbc.driver=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql:///mybatis_db
jdbc.username=root
jdbc.password=root

在 sqlmapconfig.xml 中引入:

<properties resource="jdbc.properties"/>

然后使用 ${} 引用:

<datasource type="pooled">
    <property name="driver" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</datasource>

4.2 typealiases类型别名

mybatis内置了类型别名注册,我们自己也可以注册别名:

<typealiases>
    <!-- 针对com.qcbyjy.domain包下的所有类,使用类名做为别名 -->
    <package name="com.qcbyjy.domain"/>
</typealiases>

配置后,在mapper.xml中可以直接使用类名(不区分大小写):

<select id="findall" resulttype="user">
    select * from user
</select>

总结

到此这篇关于mybatis参数与sqlmapconfig.xml核心配置的文章就介绍到这了,更多相关mybatis参数与sqlmapconfig.xml核心配置内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com