微服务,微架构[六]之springboot集成mybatis
来源:互联网 发布:梦龙网络计划软件使用 编辑:程序博客网 时间:2024/05/29 03:48
介绍
mybatis在现在市场占有率相对比较高,使用较多的一个框架,所以本文主要介绍如何将mybatis集成到springboot中进行使用,在之前使用springmvc中集成mybatis非常麻烦,而且有很多xml配置文件,配置起来非常臃肿,而且维护起来复杂,往往整理这些配置文件就需要花很长时间才能基础搭建起来,springboot集成mybatis完全抛弃之前的xml文件,经过几个简单的配置就可以完成对mybaties的集成。
一、配置数据源与mybatis扫描路径 application.properties
#1.jdbc configspring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.url=jdbc:mysql://localhost:3306/boot?characterEncoding=utf-8&generateSimpleParameterMetadata=truespring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driver#2.extend configspring.datasource.initialSize=5spring.datasource.minIdle=5spring.datasource.maxActive=20spring.datasource.maxWait=60000spring.datasource.timeBetweenEvictionRunsMillis=60000spring.datasource.minEvictableIdleTimeMillis=300000spring.datasource.validationQuery=SELECT 1 FROM DUALspring.datasource.testWhileIdle=truespring.datasource.testOnBorrow=falsespring.datasource.testOnReturn=falsespring.datasource.poolPreparedStatements=truespring.datasource.maxPoolPreparedStatementPerConnectionSize=20spring.datasource.filters=stat,wall,log4jspring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000#mybaties configmybatis.typeAliasesPackage=com.eshengtai.dao.modelmybatis.mapperLocations=classpath:**/mapper/*.xml
1、mybatis.typeAliasesPackage 扫描java基础实体类
2、mybatis.mapperLocations 扫描dao层mybatis接口实现*mapper.xml对应SQL语句文件
二、定义实体类与接口dao类,mapper.xml文件
package com.eshengtai.dao.model;public class EShengTai { private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
@Mapperpublic interface EShengTaiMapper { List<EShengTai> selectAll(); void insert(EShengTai est);}
<?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="com.eshengtai.dao.mapper.EShengTaiMapper" > <resultMap id="BaseResultMap" type="com.eshengtai.dao.model.EShengTai" > <id column="id" property="id" jdbcType="BIGINT" /> <result column="Name" property="name" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > id, name </sql> <select id="selectAll" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from eshengtai </select> <insert id="insert" parameterType="com.eshengtai.dao.model.EShengTai" > insert into eshengtai (id,name) values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}) </insert></mapper>
三、启动springboot扫描mybatis接口文件
package com.eshengtai;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@MapperScan("com.eshengtai.dao.mapper")public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
@MapperScan("com.eshengtai.dao.mapper") 在springboot启动时候会将接口注入到spring扫描的容器中,在dao层调用时候会自行依赖关联实现
以上所有的代码就是springboot集成mybatis最精简的配置,其他的调用方式和以来注入方式都和以前一样,以图片的方式展示其他代码,springboot事务详情文章我会在后面的文章中独立详解
阅读全文
0 0
- 微服务,微架构[六]之springboot集成mybatis
- 微服务,微架构[二]之springboot集成jdbc
- 微服务,微架构[四]之springboot集成Redis缓存
- SpringBoot集成MyBatis(微服务)
- 微服务,微架构[十三]springboot集成RabbitMQ
- 微服务,微架构[三]之springboot集成nosql数据库mongodb
- 微服务,微架构[一]之springboot[helloWorld]
- 微服务,微架构[五]之springboot读取properties文件
- 微服务,微架构[七]之springboot异常处理
- SpringBoot开始微服务架构
- 微服务,微架构[零]springboot入门介绍
- 微服务,微架构[八]springboot多环境切换profiles
- 微服务,微架构[十]springboot模板页面freemarker
- 微服务,微架构[十一]springboot模板页面velocity
- 微服务,微架构[十一]springboot模板页面thymeleaf
- 微服务,微架构[十二]springboot监听器,过滤器
- 微服务架构模式系列文章之六:服务注册表
- 【微服务架构】SpringCloud之Hystrix断路器(六)
- javascript设计模式之装饰器模式
- tf.add_to_collection,tf.get_collection和tf.add_n的用法
- window.location.href和window.open的几种用法和区别
- SQL SEVER碎片化及压缩整理
- java中静态变量hashmap赋值给非静态变量,非静值改变,静态值也改变
- 微服务,微架构[六]之springboot集成mybatis
- python文件操作
- B1020. 月饼 (25’)
- 高吞吐低延迟Java应用的垃圾回收优化
- Linux学习篇第三章之~apache
- STA静态时序分析
- 高光谱遥感图像相关知识梳理大全
- 南阳oj-找球号(一) set的常用用法
- Android5.0Button字体大小写