从0开始搭建springboot开发模板-整合mybatis
2018-08-30 23:19
183 查看
springboot整合mybatis现在也是非常简单了,没啥好说的。配置一下配置文件即可。
[code] pom文件加入依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.0</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency>
随后yml配置文件添加相关配置,整合就算成功。
[code]spring: datasource: druid: url: jdbc:oracle:thin:@127.0.0.1:1521:mldn username: scott password: tiger driver-class-name: oracle.jdbc.driver.OracleDriver initial-size: 1 max-active: 5 min-idle: 1 max-wait: 60000 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 30000 validation-query: SELECT 1 FROM DUAL test-while-idle: true test-on-borrow: false test-on-return: false pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 filters: stat,slf4j mybatis: mapper-locations: classpath:mapper/*.xml config-location: classpath:mybatis-config.xml
整和成功后,分享两个工作中遇到的小问题吧。
(1)项目初次配置的时候,因为疏忽,导致数据库密码写错,项目启动时一直重复尝试,多次尝试失败后,数据库被锁。
这个问题说大不大,但是密码写错我还是希望项目直接退出,而不是重复尝试。并且控制台给出相应反应。告诉启动的人员,数据库密码写错了。所以基于这个思路,对项目进行了一些改动。
首先配置方法,确认数据库连接是否正常。
[code] @Select("select 1 from dual") String test();
其次,该方法我希望在启动时直接调用。所以新添加连接测试类。
[code]@Component public class MybatisConnect { @Autowired private Mapper mapper; @Autowired public void connectTest(){ try { String s =mapper.test(); }catch (Exception e){ e.printStackTrace(); System.exit(1); } } }
该类以及方法都会在项目启动时随着项目启动而执行。如果连接正常,项目便可直接启动,如果连接异常。被catch捕捉之后,项目便会退出。这样,我的目的就达到了。
(2)在使用mybatis过程中,有时候查出来的结果为两列数据,我期望以其中一列为key,另一列为value,将结果封装至map中。所以,查询结果的时候,需要定义一个结果处理器。
[code]public class MapResultHandler implements ResultHandler { @SuppressWarnings("rawtypes") private final Map result = new HashMap(); @SuppressWarnings("unckecked") @Override public void handleResult(ResultContext resultContext) { @SuppressWarnings("rawtypes") Map map = (Map) resultContext.getResultObject(); result.put(map.get("key"),map.get("value")); } @SuppressWarnings("rawtypes") public Map getResult(){ return result; } }
随后将查出的结果列名分别改为key和value
[code]<resultMap id="querymap" type="hashmap"> <result property="key" column="ename" javaType="string"/> <result property="value" column="sal" javaType="int"/> </resultMap>
随后在定义一个session类,用结果处理器,处理查询出的结果。
[code] @Repository public class Session extends SqlSessionDaoSupport { @Resource public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){ super.setSqlSessionFactory(sqlSessionFactory); } @SuppressWarnings("unchecked") public Map query(){ MapResultHandler handler = new MapResultHandler(); this.getSqlSession().select(Mapper.class.getName()+".query",handler); Map map = handler.getResult(); return map; } }
这样就大功告成,我们可以将结果封装至map中,打印一下试试。
[code] public String index(){ Map<String,Integer> map = session.query(); map.forEach((s, integer) -> System.out.println(s+" "+integer)); return "hello"; }
如此便是控制台打印出的结果。
[code]JONES 2975 TURNER 1500 CLARK 2450 MARTIN 1250 MILLER 1300 SCOTT 3000 WARD 1250 BLAKE 2850 KING 5000 JAMES 950 SMITH 800 ADAMS 1100 FORD 3000 ALLEN 1600
项目源码:https://github.com/liuyueve/templete.git
阅读更多
相关文章推荐
- AngularJS整合Springmvc、Spring、Mybatis搭建开发环境
- JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎
- 基于maven搭建ssm开发框架(1) mybatis和spring的整合
- SpringBoot开发详解(七)-- Mybatis整合Spring Boot
- JavaEE_Mybatis_SpringMVC_框架整合_lesson1_简单的SSM框架搭建_使用原始Dao开发方式进行测试
- AngularJS整合Springmvc、Spring、Mybatis搭建开发环境
- 开始搭建 myBatis.net + Spring.net + asp.net mvc 3 + easyUI 开发平台
- Spring+SpringMvc+Mybatis框架集成搭建教程三(框架整合测试程序开发)
- springMVC学习笔记---day02 springMVC+spring+mybatis整合开发框架搭建
- ssm整合说明与模板-Spring Spring MVC Mybatis整合开发
- 开发框架整合与搭建:spring boot+mybatis+jedis
- MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合 一、搭建开发环境 1.1、使用Maven创建Web项目 执行如下命令: mvn archetype:create
- AngularJS整合Springmvc、Spring、Mybatis搭建开发环境
- SpringBoot 整合 MyBatis 实现简单注解开发
- JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎
- 使用myeclipse搭建SpringBoot框架,整合thymeleaf模板和mybatis
- 使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境
- Eclipse中 搭建 Spring+SpringMVC+Mybatis整合(上)
- MyBatis开发环境搭建及基本组件
- spring集成Mybatis3【由“单独搭建Mybatis”到“Mybatis与Spring的整合/集成”】