首页 > 数据库 >Mybatis SQL映射文件

Mybatis SQL映射文件

时间:2023-04-29 23:33:08浏览次数:36  
标签:empName 映射 employee SQL Mybatis empname where id select

https://blog.csdn.net/sun_soul/article/details/111773947 SQL映射文件 增删改标签 <insert id="insertEmployee"> INSERT INTO t_employee(empname,gender,email) VALUES(#{empName},#{gender},#{email})

image.png

获取自增主键的值

让MyBatis自动的将自增的id赋值给传入的employee对象的id属性:

  • 原生jdbc中的getGeneratedKeys 获取由于执行此statement对象而创建的所有自动生成的键。
<!--增加-->
<!--让MyBatis自动的将自增的id赋值给传入的employee对象的id属性
    useGeneratedKeys="true":开启使用自增
    keyProperty="id":自增的值赋值给id属性
-->
<insert id="insertEmployee" useGeneratedKeys="true" keyProperty="id">
    insert into t_employee(empname,gender,email) values(#{empName},#{gender},#{email})
</insert>

获取非自增主键的值

selectKey查询主键:

<insert id="insertEmployee2">
    <!--查询主键
        order="BEFORE":在核心sql语句执行之前先运行一个sql查到id
        keyProperty="id":将查到数据库id的最大值加1赋值给javaBean的id属性
        resultType="integer":指定返回类型未integer类型
    -->
    <selectKey order="BEFORE" resultType="integer" keyProperty="id">
        select max(id)+1 from t_employee
    </selectKey>
    insert into t_employee(id,empname,gender,email) values(#{id},#{empName},#{gender},#{email})
</insert>

参数的各种取值

@Param指定封装时的参数名
public Employee getEmpByIdAndEmpName(@Param("id") Integer id, @Param("empName") String empName);

<select id="getEmpByIdAndEmpName" resultType="smq.javabean.Employee">
        select * from t_employee where id=#{id} and empname=#{empName}
</select>
//传入pojo
public Employee getEmployeeByIdAndEmpName(Map<String, Object> map);

<select id="getEmployeeByIdAndEmpName" resultType="smq.javabean.Employee">
         select * from t_employee where id=#{id} and empname=#{empName}
</select>

map.put("id", 1);
map.put("empName", "tomcat1");
  • 多个参数自动封装map。如: method(@Param("id) Integer id, String empName, Employee employee);

Integer id---->#{id} String empName---->#{param2} Employee employee中的email---->#{param3.email}

javaType 通常可以从参数对象中来去确定 如果 null 被当作值来传递,对于所有可能为空的列,jdbcType 需要被设置 对于数值类型,还可以设置小数点后保留的位数: mode 属性允许指定 IN,OUT 或 INOUT 参数。如果参数为 OUT 或 INOUT,参数对象属性的真实值将会被改变,就像在获取输出参数时所期望的那样。

#{}与${}

select * from t_employee where id=#{id} and empname=#{empName} select * from t_employee where id=? and empname=?

select * from t_employee where id=${id} and empname=#{empName} select * from t_employee where id=1 and empname=? 一般都是用#{},因为#{}预编译安全;在不支持预编译的位置(如表名)要进行取值需要使用${}。

查询返回list

public List<Employee> getAllEmps();
// resultType:如果返回的是集合,返回类型应写集合里面元素的类型
<select id="getAllEmps" resultType="smq.javabean.Employee">
select * from t_employee
</select>

查询返回map

//查询多条记录返回多个map,记录的主键作为key,该记录封装好的对象作为value

@MapKey("id")//指定id的值作为key封装
public Map<Integer, Employee> getAllEmpsReturnMap();
<!--查询多条记录返回一个map,返回类型使用元素类型-->
<select id="getAllEmpsReturnMap" resultType="smq.javabean.Employee">
select * from t_employee
</select>

resultMap自定义封装规则.

<?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">
<!--namespace:名称空间。写接口给的全类名,相当于告诉MyBatis这个配置文件是实现哪个接口的。-->
 
<mapper namespace="smq.dao.CatDao">
    <!--resultMap="mycat"指定查询数据封装结果的时候使用mycat自定义封装规则-->
    <select id="getCatById" resultMap="mycat">
        select * from t_cat where id = #{id}
    </select>
    <!--type指定为哪个javaBean自定义封装规则,id是唯一标识-->
    <resultMap id="mycat" type="smq.javabean.Cat">
        <!--id指定主键列的对应规则,column指定哪一列是主键列,property指定cat的哪个属性封装id这个列数据-->
        <id column="id" property="id"/>
        <!--普通列-->
        <result column="cName" property="name"/>
        <result column="cgender" property="gender"/>
        <result column="cAge" property="age"/>
    </resultMap>
 
</mapper>

使用select属性指定分步查询

public interface KeyDao {
    public Key getKeyByIdSimple(Integer id);
}
 
public interface LockDao {
    public Lock getLockByIdSimple(Integer id);
}

<?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">
<!--namespace:名称空间。写接口给的全类名,相当于告诉MyBatis这个配置文件是实现哪个接口的。-->
 
<mapper namespace="smq.dao.KeyDao">
 
    <select id="getKeyByIdSimple" resultMap="mykey">
        select * from t_key where id=#{id}
    </select>
    <!--type指定为哪个javaBean自定义封装规则,id是唯一标识-->
    <resultMap id="mykey" type="smq.javabean.Key">
        <!--id指定主键列的对应规则,column指定哪一列是主键列,property指定cat的哪个属性封装id这个列数据-->
        <id property="id" column="id"/>
        <!--普通列-->
        <result property="keyName" column="keyname"/>
        <!--接下来的属性是一个lock对象,自定义这个对象的封装规则
            select指定一个查询SQL的唯一标识,mybatis自动调用指定的sql将查出的lock封装
            column指定将哪一列的数据传递进去
        -->
        <association property="lock" select="smq.dao.LockDao.getLockByIdSimple" column="lockid">
        </association>
    </resultMap>
</mapper>

<?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">
<!--namespace:名称空间。写接口给的全类名,相当于告诉MyBatis这个配置文件是实现哪个接口的。-->
 
<mapper namespace="smq.dao.LockDao">
    <select id="getLockByIdSimple" resultType="smq.javabean.Lock">
        select * from t_lock where id=#{id}
    </select>
</mapper>

按需加载和延时加载

  • 对于分布查询只发送一个sql
  • 按需加载:需要的时候再去查询。开启全局按需加载策略。
  • 这样只有使用某属性时才会去查询该属性。
<settings>
        <!--开启延迟加载对象-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!--开启属性按需加载-->
	<setting name="aggressiveLazyLoading" value="false"/>
</settings>

标签:empName,映射,employee,SQL,Mybatis,empname,where,id,select
From: https://blog.51cto.com/u_15993308/6237180

相关文章

  • Python: PostgreSQL
     connectionimportpsycopg2frompsycopg2importError,connection,cursorconn:connection|None=Nonec1:cursor|None=Nonetry:conn=psycopg2.connect(host='localhost',port=5432,user='postgres',......
  • PHP连接MYSQL的一些操作
    PHP5以及版本使用允许以下方式连接MYSQL:MYSQLiextensionPDO(PHPDataObjects)1.连接MYSQL//MYSQLi-面向对象<?php$servername="localhost";$username="username";$password="password";//创建连接$conn=newmysqli($sername,$username,$p......
  • MySQL常用数据库语句
    ==数据库==1、创建数据库createdatabase[IFNOTEXISTS]数据库名;2、删除数据库dropdatabase[IFEXISTS]数据库名;3、切换数据库selectdatabase();4、查询数据库showdatabases;————————————————————————————==数据表==1、创建数据表crea......
  • python 读写sqlite3 读写内存中的数据库
    Python中,可以使用标准库sqlite3来读写SQLite数据库。下面是一个示例代码,展示如何连接到SQLite数据库,创建表格,插入数据,查询数据和关闭数据库:importsqlite3#连接到数据库conn=sqlite3.connect('example.db')#创建一个表格conn.execute('''CREATETABLEIFNOTE......
  • docker 创建mysql及卷挂载
    dockerpullmysql:5.7dockerrun-d-p3307:3306-v/home/mysql/conf:/etc/mysql/conf.d-v/home/mysql/data:/var/lib/mysql-eMYSQL_ROOT_PASSWORD=root--namemysql57mysql:5.7----------------------------------------------d后台运行-p3307:3306端口映射-v......
  • 内存映射
     /*内存映射:是将磁盘文件数据映射到内存,用户通过修改内存就能修改磁盘文件#include<sys/mman.h>void*mmap(void*addr,size_tlength,intprot,intflags,intfd,off_toffset);功能:将一个文件或......
  • 驱动开发:通过MDL映射实现多次通信
    在前几篇文章中LyShark通过多种方式实现了驱动程序与应用层之间的通信,这其中就包括了通过运用SystemBuf缓冲区通信,运用ReadFile读写通信,运用PIPE管道通信,以及运用ASYNC反向通信,这些通信方式在应对一收一发模式的时候效率极高,但往往我们需要实现一次性吐出多种数据,例如ARK工具中当我......
  • 驱动开发:通过MDL映射实现多次通信
    在前几篇文章中LyShark通过多种方式实现了驱动程序与应用层之间的通信,这其中就包括了通过运用SystemBuf缓冲区通信,运用ReadFile读写通信,运用PIPE管道通信,以及运用ASYNC反向通信,这些通信方式在应对一收一发模式的时候效率极高,但往往我们需要实现一次性吐出多种数据,例如ARK工具中当......
  • mysql: character set
    --https://dev.mysql.com/doc/refman/8.0/en/charset-database.htmlshowvariableslike"character_set_%";CREATEDATABASE`geovindu`CHARACTERSETutf8COLLATEutf8_general_ci;--mysql官方说明文档才知道原来MySQL8.0已经已经把默认字符集升级成ut8mb4了ALTERDA......
  • MySQL中的锁有哪些,作用是什么?
    概述:锁最要是用来实现MySQL的隔离性。我们都知道事务有四大特性分别是:原子性、一致性、隔离性、持久性,即所说的ACID。一、什么是ACID1、原子性:事务中包含有很多操作,这些操作要么全部执行,要么全部不执行,所以支持回滚操作。2、一致性:系统从一种一致性到另一种一致性状态。事物的一致......