Mybatis how to return result as a map? - spring

For example, my sql query is select a_id, count(*) from tb group by a_id.
And the result I expect is Map<Long, Integer>.
Can I make it with mybatis annotaion?

Related

How can we write JPA query for below SQL query

how to write jpa query for below sql without using native sql query
select * from emp where emp_id=123 and (emp_name="ABC" OR emp_name is NULL) order by salary
If we say
A and (B or C) <=> (A and B) or (A and C)
then you query must look like this one below
Employee findByEmp_idAndEmp_nameOrEmp_idAndEmp_nameIsNullOrderBySalary(Long empId, String empName);

jdbctemplate equivalent of following query

I have a long list of argument which I need to send to oracle database. I was able to do it by splitting the query but I am unable to find a way to do similar using jdbcTemplate. my query is:
select name,age from person where personId in (A1,F2,D3...G900)
or personId in (A901, C902 , ... R1800)
or personId in (A1801,G1802 .... H2700)
or personId in (P2701, G2702 ... R3600)
or since oracle allow more than 1000 touple but does not allow in so JDBC equivalent for
SELECT field1, field2, field3
FROM table1
WHERE (1, name) IN ((1, value1), (1, value2), (1, value3),.....(1, value5000));
List<Map<String, Object>> findPeeps(List<Long> personIds) {
String sql = "select name,age from person where personId in (:personIds)";
return namedParameterJdbcTemplate.queryForList(sql, new MapSqlParameterSource("personIds", personIds));
}
As #zaki said you can use that but the error that you are getting is from Oracle since there is limit of records you can put in WHERE IN clause. You can try something like this
insert into TEMP values ( ... );
select * from T where (a,b) in (select x,y from temp);
delete from TEMP;

Need to Convert this SQL query to LINQ

can anybody help me to convert this SQL query to LINQ code in MVC? I need to return a list. The DB context entity is: _dbContext.
select distinct table1.AIG_ID, table1.GMT_NAME, table1.AIG_Number
from table1 left join table2 on table1.AIG_ID = table2.AIG_ID**
var data=(from item in db.table1 join
item1 in db.table2 on item.AIG_ID equals item1.AIG_ID
select new {item.AIG_ID ,item.GMT_NAME ,item.AIG_Number }).GroupBy(a=>a.AIG_ID).select(a=>a.FirstOrDefault()).ToList();
I write this part for your distinct in sql
GroupBy(a=>a.AIG_ID).select(a=>a.FirstOrDefault())

Unoin clause in querydsl 4.1.3?

How can I use union clause in querydsl 4.1.3?
I want just to use union clause. Need an example.
#sql
select * from (
select a,b from tableA union
select a,b from tableB
)
QueryDSL - how to join to a union of subqueries
I can't find SQLSubQuery class in querydsl lib(version 4.1.3)
I'm using
* spring boot
* spring-data
* querydsl(4.1.3)
Its perfectly possible for QueryDSL SQL queries:
QSurvey survey1 = new QSurvey("survey1");
QSurvey survey2 = new QSurvey("survey2");
QSurvey survey3 = new QSurvey("survey3");
SQLQuery<Void> query = new SQLQuery<Void>();
query.with(survey1, select(survey1.all()).from(survey1));
query.union(
select(survey2.all()).from(survey2),
select(survey3.all()).from(survey3));
The last statement returns a subquery that yields the union of the two queries.

how to write this oracle query in jpa?

Query:
select *
from easquestionsinfo
where questionname in(select questionname
from easresponseinfo
where isconflict = 'yes')
This query works fine and returns me the records from table 'easquestioninfo' when questionname is equal to the one returned by the inner query which returns set of questionname where isconflict='yes'.
JPA supports JPQL, SQL, and Criteria.
You can execute this SQL directly using createNativeQuery().
For JPQL, it depends on your object model, perhaps something like,
Select q fom QuestionInfo q where q.name in (Select r.name from ResponseInfo q2 where r.isConflict = 'yes')
See,
http://en.wikibooks.org/wiki/Java_Persistence/JPQL

Resources