springboot+mybatis

很久没写写东西了,去年底开始在做微服务改造,玩spring boot还有spring cloud。总结一下,免得脑子不好使,忘记事情。

springboot和spring jpa有很好的天然集成,但是如果要用mybatis的话,还是需要自己做一点点配置。刚开始弄springboot和mybatis集成的时候,一顿搜索,发现网上好多人写的都很复杂,属于纯手工集成的方式,自己配置datasource,自己配置扫描路径,自己配置transactionManager。

其实mybatis官方已经帮我们写好了MyBatis-Spring-Boot-Starter,我们只有把它声明到pom.xml即可。而且官方也有相关的demo。但是web工程集成mybatis的时候出现的问题,不知道是不是共性问题,就是必须自己用@Bean注解配置一个DataSource实例,不然springboot就是集成不了mybatis。

具体操作步骤如下:

新建一个mave工程springboot_mybatis

  1. 最后的工程如下:

QQ截图.png

配置pom.xml

  1. pom.xml配置如下:

    <?xml version="1.0" encoding="UTF-8"?>

    <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>

    1<groupId>com.lee</groupId> 2<artifactId>springboot_mybatis</artifactId> 3<version>0.0.1-SNAPSHOT</version> 4<packaging>jar</packaging> 5 6<name>springboot_mybatis</name> 7<description>Spring Boot Setup Demo</description> 8 9<parent> 10 <groupId>org.springframework.boot</groupId> 11 <artifactId>spring-boot-starter-parent</artifactId> 12 <version>1.4.5.RELEASE</version> 13 <relativePath/> <!-- lookup parent from repository --> 14</parent> 15 16<properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 <java.version>1.8</java.version> 19 <mybatis-spring.version>1.2.3</mybatis-spring.version> 20 <mybatis.version>3.3.0</mybatis.version> 21 <h2.version>1.4.190</h2.version> 22</properties> 23 24<dependencies> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-web</artifactId> 28 </dependency> 29 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-test</artifactId> 33 <scope>test</scope> 34 </dependency> 35 36 <dependency> 37 <groupId>org.mybatis.spring.boot</groupId> 38 <artifactId>mybatis-spring-boot-starter</artifactId> 39 <version>1.2.0</version> 40 </dependency> 41 42 <!-- spring jdbc --> 43 <dependency> 44 <groupId>org.springframework</groupId> 45 <artifactId>spring-jdbc</artifactId> 46 </dependency> 47 48 <!-- 数据库连接池 --> 49 <dependency> 50 <groupId>com.alibaba</groupId> 51 <artifactId>druid</artifactId> 52 <version>1.0.27</version> 53 </dependency> 54 55 <!-- h2 --> 56 <dependency> 57 <groupId>com.h2database</groupId> 58 <artifactId>h2</artifactId> 59 <version>${h2.version}</version> 60 </dependency> 61</dependencies> 62 63<build> 64 <plugins> 65 <plugin> 66 <groupId>org.springframework.boot</groupId> 67 <artifactId>spring-boot-maven-plugin</artifactId> 68 </plugin> 69 </plugins> 70</build> 71 72<repositories> 73 <repository> 74 <id>spring-snapshots</id> 75 <name>Spring Snapshots</name> 76 <url>https://repo.spring.io/snapshot</url> 77 <snapshots> 78 <enabled>true</enabled> 79 </snapshots> 80 </repository> 81 <repository> 82 <id>spring-milestones</id> 83 <name>Spring Milestones</name> 84 <url>https://repo.spring.io/milestone</url> 85 <snapshots> 86 <enabled>false</enabled> 87 </snapshots> 88 </repository> 89</repositories> 90<pluginRepositories> 91 <pluginRepository> 92 <id>spring-snapshots</id> 93 <name>Spring Snapshots</name> 94 <url>https://repo.spring.io/snapshot</url> 95 <snapshots> 96 <enabled>true</enabled> 97 </snapshots> 98 </pluginRepository> 99 <pluginRepository> 100 <id>spring-milestones</id> 101 <name>Spring Milestones</name> 102 <url>https://repo.spring.io/milestone</url> 103 <snapshots> 104 <enabled>false</enabled> 105 </snapshots> 106 </pluginRepository> 107</pluginRepositories>
    </project>

Mapper

  1. application.properties声明mybatis相关的属性

    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=org.h2.Driver spring.datasource.jdbc-url=jdbc:h2:tcp://localhost/~/springbootmybatis spring.datasource.username=sa spring.datasource.password= spring.datasource.schema=classpath:import.sql

    声明mapper所在路径

    mybatis.mapper-locations=classpath:mapper/*.xml

这里用的是h2数据库,如果使用其他数据库,只要在pom添加相关的依赖和配置数据库的相关属性即可。

  1. *Mapper.xml的配置和普通的配置没有区别

  2. CityMapper.java接口

    package com.mapper;

    import org.apache.ibatis.annotations.Mapper;

    import com.domain.City;

    /**

    • Created by liyc02 on 2017/5/12. */ @Mapper public interface CityMapper { City selectCityById(int id); }

特别注意的是使用的[@Mapper](https://my.oschina.net/u/1452465) 注解而不是之前的[@Repository](https://my.oschina.net/u/3055569) 注解。

数据源配置

  1. 开始以为在application.properties中声明了spring.datasource.type=com.alibaba.druid.pool.DruidDataSource就可以自动配置了,结果不行,需要自己手动配置DataSourceConfig.java

    package com.config;

    import javax.sql.DataSource;

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment;

    import com.alibaba.druid.pool.DruidDataSource;

    @Configuration public class DataSourceConfig { @Autowired private Environment env;

    1@Bean 2public DataSource getDataSource() { 3 DruidDataSource ds = new DruidDataSource(); 4 ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name")); 5 ds.setUrl(env.getProperty("spring.datasource.jdbc-url")); 6 ds.setUsername(env.getProperty("spring.datasource.username")); 7 ds.setPassword(env.getProperty("spring.datasource.password")); 8 9 return ds; 10}

    }

以上,运行App.java即可,访问浏览器,可以得到结果。

QQ截图.png

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

​一篇文章总结一下Python库中关于时间的常见操作

前言本次来总结一下关于Python时间的相关操作,有一个有趣的问题。如果你的业务用不到时间相关的操作,你的业务基本上会一直用不到。但是如果你的业务一旦用到了时间操作,你就会发现,淦,到处都是时间操作。。。所以思来想去,还是总结一下吧,本次会采用类型注解方式。time包importtime时间戳从1970年1月1日00:00:00标准时区诞生到现在