mybatis直接执⾏传⼊的sql语句(⼀)通过配置⽂件的⽅式实现
1、dao.java
public List<Map<String, Object>> selectPublicItemList(@Param(value="sqlStr") String sqlStr);
2、l
<select id="selectPublicItemList" parameterType="String" resultType="java.util.Map">
${sqlStr}
</select>
3、测试类
package com.pository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
resultmap和resulttype区别和应用import org.st.context.SpringBootTest;
import st.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import java.util.Map;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class UserMapperTest {bluehost
@Autowired
private PublicMapper publicMapper;
@Test
轮播台爱奇艺
public void getPublicData(){
String sql = "select id,age,password,sex,username from user where id = 1";
List<Map<String, Object>> list = publicMapper.selectPublicItemList(sql);
}
}
(⼆)通过注解的⽅式实现
1、dao.java
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.LinkedHashMap;
import java.util.List;
/**
* @description:
* @author: zhengkeyang
* @date: 2019/7/12
*/
@Mapper
public interface PublicMapper {c语言贪吃蛇游戏设计
@Select("${sqlStr}")
List<LinkedHashMap<String,Object>> getPublicItems(@Param(value = "sqlStr") String sqlStr);shiro框架技巧
}mysql语句的执行顺序
2、测试类
@Autowired
private PublicMapper publicMapper;
@GetMapping("/user")
public Object getUser(){
List<LinkedHashMap<String,Object>> mapList = PublicItems("select id,age,password,sex,username from user where id = 1");        return mapList;
}
注意事项:
Dao中getPublicItems返回值的Map集合要指定为LinkedHashMap,不然查询的数据和sql中字段顺序会不⼀致。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。