when using Mybatis, I encounter the avaticaSqlexception. Does anyone know the reason?
<insert id="insert" parameterType="com.*">
insert into stats_sms (pin,prizeId,templateId,sendTime) values ('123','123','123','123')
</inserte>
The value you pass are all hardcode string,so change parameterType to String or just remove parameterType attribute
<insert id="insert" parameterType="String">
insert into stats_sms (pin,prizeId,templateId,sendTime) values ('123','123','123','123')
</insert>
OR
<insert id="insert">
insert into stats_sms (pin,prizeId,templateId,sendTime) values ('123','123','123','123')
</insert>
Related
I am trying to dynamically include select query based on some input parameters.
The following is what I am trying to achieve.
<sql id="query1">
SELECT * from
table_1
WHERE a = #{param1}
</sql>
<sql id="query2">
SELECT * from
table_2
WHERE b = #{param2}
</sql>
<select id = "selectSomethingFromDB">
<include refid="#{readerIdName}" />
</select>
I am planning to pass the sql id name as parameter to the query and trying to dynamically choose the select query based on this param. (Sort of like a factory design implementation).
However #{readerIdName} is not getting replaced with the value I am passing as param.
Is something like this possible?
The xml attributes at this level are not evaluated in runtime by mybatis.
You can use a single <SQL id="..."> query and an <if test="..."> dynamic sql in it.
I do
INSERT INTO table DEFAULT VALUES RETURNING id
which returns the ID, which is later used in the calling code. But if I do
INSERT INTO table DEFAULT VALUES RETURNING id;
INSERT INTO table DEFAULT VALUES RETURNING id;
INSERT INTO table DEFAULT VALUES RETURNING id;
I only get the latest returned value.
What's a proper way to do it, as an either "do this n times" construct or a union of the above (which should work for any return query)?
You can exploit the (lesser known fact) that you can run a SELECT statement without putting any column into the select list.
insert into the_table --<< no columns here!
select --<< no columns here either!
from generate_series(1,3);
Online example
my string parameter is like this: '(1,2,3,4)'
I want to iterate over this
SELECT * FROM table WHERE value IN #{myparameter}
How can I do this?
You can put the value into the query verbatim like this:
SELECT * FROM table WHERE value IN ${myparameter}
But note that this does not use prepared statements parameters. It has all the downsides of not using prepared statements, namely: it is not type save, vulnerable to SQL injection, requires escaping etc
You should better convert the string to a list and then generate the query in a type safe manner:
SELECT *
FROM table
WHERE value IN
<foreach item="item" index="index" collection="myparameter"
open="(" separator="," close=")">
#{item}
</foreach>
myparameter should be a Collection of whatever type the value column is.
I'm using views in Postgresql and I encounter a problem in Zend: I can't retrieve the last inserted value of an insert request.
Example:
I have an user_view and when I insert in, it does instead:
INSERT INTO basic_user(name, email) VALUES [...]; INSERT INTO extended_user(id, phone) VALUES (lastval(), [...]);
When I execute the request in Zend and call getGeneratedValue() it returns NULL...
How can I get the lastval() ?
I tried to use the RETURNING clause in my INSERT but without success..
Thx for your help.
The solution in PGSQL is currval(Sequence name) PG Doc.
Example
INSERT INTO basic_user(name, email) VALUES [...]; INSERT INTO extended_user(id, phone) VALUES (currval(basic_user_seq)(), [...]);
My project is a Database center, like PLSQL but used in a web browser. Sometimes I need to create or alter a table, but I don't know if Mybatis supports DDL, and I haven't found any documents about this.
For the most part DDL works just like DML using mybatis. The one difference is that you will need to use ${} instead of #{} for parameters. Most databases do not support prepared statements with DDL. The $ notation is a string substitution rather than a parameter for a prepared statement.
<update id="exchangePartition" parameterType="java.util.Map">
alter table ${destinationTableName}
exchange partition ${destinationPartitionName}
with table ${sourceTableName}
including indexes
with validation
</update>
It is also helpful to know the call syntax with the statement type callable to invoke stored procedures.
<update id="gatherStatistics" statementType="CALLABLE" parameterType="Map">
{call
dbms_stats.gather_table_stats(
ownname => #{tableOwner},
tabname => #{tableName}
<if test="partitionName != null">
, partname => #{partitionName}
</if>
)
}
</update>
MyBatis supports any native SQL/PlSql commands.
So yes, it supports DDL statements.