首先声明,该操作方式并不是规范的开发方式,但是对于一些不能共用的放回对象而言,该方式又可以很好的减少编写不必要的pojo对象,具体该怎么样使用,开发者可以视情况而定吧.所言有误望批评指正!!!
分析:返回的结构集就是List集合,而每个对象就可以对应一个Map,map的每个key对应Field,value则对应表字段对应的值,顺着这个思路,我尝试着使用List<Map<String,String>>来接收返回的结果集(一般方式是List<TablePojo>),测试竟然通过了,正确获取到数据
1.引入po.xml需要的依赖(mybtis版本号必须指定,不然下载不到包)
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>love.yi.hellowordweb</groupId>
<artifactId>spring-boot-hellowordweb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- 由引入的父工程统一指定 版本号 -->
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<!-- 包含spring webmvc和tomcat等web开发特性 -->
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 配置数据源 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<!-- 如果使用maven的spring-boot:run运行就不需要该插件 -->
<artifactId>spring-boot-maven-plugin </artifactId>
<dependencies>
<!--springloaded hot deploy -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
<!-- 指定编译期版本,指定jdk版本需要在maven配置文件中配置 -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<!-- pom.xml文件节点总结,看一个节点是不是能有多个兄弟,看父节点有没有复数,没有就一定只能存在一个 -->
</project>
2.application.properties配置数据源和mybatis相关配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
#spring.datasource.useGlobalDataSourceStat=true
#mybatis.config-location=classpath:mybatis-config.xml
#mybatis mapper文件的位置
mybatis.mapper-locations=classpath*:mapper/**/*.xml
#扫描pojo类的位置,在此处指明扫描实体类的包,在mapper中就可以不用写pojo类的全路径名了
mybatis.type-aliases-package=love.yi.helloword.dao
3.编写Dao接口和mapp.xml文件
<?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">
<mapper namespace="love.yi.helloword.dao.IListMapStructureDao">
<select id="getInfoById" resultType="java.util.Map">
SELECT * FROM demo
</select>
</mapper>
package love.yi.helloword.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface IListMapStructureDao {
List<Map<String,String>> getInfoById();
}
4.编写service
package love.yi.helloword.service;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import love.yi.helloword.bean.Demo;
import love.yi.helloword.dao.DemoDao;
import love.yi.helloword.dao.DemoRepository;
import love.yi.helloword.dao.IListMapStructureDao;
@Service
public class DemoService {
@Resource
private IListMapStructureDao IListMapStructureDao;
@Transactional
public List<Map<String, String>> getInfoById(long id) {
// demoRepository.findOne(id);//在demoRepository可以直接使用findOne进行获取.
return IListMapStructureDao.getInfoById();
}
}
5.编写controller:需要引入ali的fastjson包,pom里没写
package love.yi.helloword.web;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import love.yi.helloword.bean.Demo;
import love.yi.helloword.service.DemoService;
@RestController
@RequestMapping("/demo2")
public class Demo2Controller {
@Resource
private DemoService demoService;
@RequestMapping("/listMap")
public String getInfoById() {
List<Map<String, String>> infoById = demoService.getInfoById(1);
String s = JSONArray.toJSON(infoById).toString();
return s;
}
}
6.测试:localhost:8080/demo2/listMap
返回结果: