Insert hashmap key and value in mybatis - mybatis

I am simply trying to persist the map's key and value in a database using mybatis but I don't know how to fetch key and value from map in mybatis query
THIS IS MY QUERY:-
<insert id="insertCategory"
parameterType="java.util.HashMap">
INSERT
<foreach collection="list" item="element" index="index">
INTO
Category(Category_ID, Category_CODE, Category_Name)
values(Category_SEQ,#{element.key},#{element.value})
</foreach>
SELECT * FROM dual
</insert>

Related

MyBatis - How can I define an update xml mapper, that returns values?

I have the following xml mapper definition:
<update id="updateProductViews">
UPDATE products_views
SET views = views + 1
WHERE product_id = #{productId}
RETURNING views;
</update>
What I'm trying to get is the returning value of the SQL query within the mapper definition.
Unfortunately, mybatis doesn't allow me to pass the resultType="Integer" in an update mapper definition, and if I don't pass resultType attribute, I just get back -1, after each update.
How should I define an update mapper that returns a value?
Thanks
PS: views stores a bigint value. By DB is postgresql

how to get MyBatis insert primary key value in PostgreSQL 13 using identify as sequece

I am now using PostgreSQL 13, after insert record using MyBatis, I need to get the primary key of insert record. In the old way, I am using serial and the config would like this:
<table tableName="article"
enableCountByExample="true"
enableUpdateByExample="true"
enableDeleteByExample="true"
enableSelectByExample="true"
selectByExampleQueryId="true">
<generatedKey column="ID" sqlStatement="SELECT currval('article_id_seq')" identity="true" />
</table>
recently I switch the PostgreSQL primary key as an identify(I read the PostgreSQL manual and tell me this is a better way to generate serial primary key) like this:
ALTER TABLE rss_sub_source
ALTER id SET NOT NULL, -- optional
ALTER id ADD GENERATED ALWAYS AS IDENTITY
(START WITH 1233);
and the table does not contain sequence, what should I do to get the primary key now?
The best way is to use the native JDBC support for generated keys. The JDBC driver will retrieve the new value. You can set this up in MyBatis Generator like this:
<table tableName="article">
<generatedKey column="ID" sqlStatement="JDBC" />
</table>

liquibase + jdbctemplate update statement - autoincrement issue

In liquibase changset I am inserting some record to database
<changeSet id="test" author="xyz">
<insert tableName="testtable">
<column name="id" value="1"/>
<column name="name" value="testdata"/>
</insert>
</changeSet>
Then using jdbcTemplate I am trying to insert new rows using instance update() method.
jdbcTemplate.update(
"INSERT INTO test.testtable(name) VALUES (?)",
new Object[] {
someObject.getName()
});
When running above method for the first time, I am getting error that record with this ID already exists in the table. However when I repeat operation , I succeed with incremented ID with value 2.
How to integrate liquibase and jdbcTemplate together to eliminate this problem? I would expect that jdbcTemplate will somehow recognize that this ID 1 is already occupied and insert data with incremented, non-conflicting, unique ID.
I am using postgres.
Is there any option to do it without removing liquibase entry with hardcoded ID value?
I would expect that jdbcTemplate will somehow recognize that this ID
1 is already occupied and insert data with incremented,
non-conflicting, unique ID.
This would not happen. Liquibase looks to see if the changeset has been ran in the environment (by checking to see if the changeset id is present in the databasechangelog table). If it is not present, it will run the changeset. Just that simple, Liquibase is not doing any thinking for you, you have to make sure that the insert will work in any reasonable condition.
I did like the suggestion in the comment above to not insert an id but instead use an auto-increment if you are wanting to always insert and have the id increment.

How to read sql server insert/update into value with mybatis?

I'm trying to retrieve the value of a SQL Sever temporal table's SYSSTARTTIME field with myBatis and can't figure it out. I had something like this:
<insert id="insertVersionedTableData" parameterType="com.me.dao.vo.DataVO">
<selectKey keyProperty="systemStartTm" resultType="java.util.Date" order="AFTER">
SELECT sysstarttm FROM #MyTableVar;
</selectKey>
DECLARE #MyTableVar TABLE (sysstarttm datetime);
INSERT INTO
dbo.versioned_table (id, first_name, last_name)
OUTPUT inserted.system_start_tm into #MyTableVar
VALUES (#{id}, #{firstName}, #{lastName});
</insert>
When I run this though I can see the insert in the console logging but it then throws an exception saying that #MyTableVar must be declared so it's like the table variable goes out of scope after the insert and the tag runs just like a regular SELECT
I've tried the generated keys functionality too but myBatis expects the key to be the first field in the table which my autogenerated SYSTEMSTARTTIME is not.

How to get the inserted or updated object after insert and update sql in mybatis

Is there any way to get the newly inserted or updated object after insert/update sql query in mybatis? Or I have to run the select query to retrieve it?
You can retrieve the generated key from an insert statement (keyProperty, keyColumn, useGeneratedKeys).
You already have the values for properties/columns inserted/updated.
You cannot retrieve values from columns not inserted (column default value) or updated (record column current value).
Then you have to select these values.