在上一篇文章springBoot+Hibernate多数据源配置与使用讲述了Springboot整合Hibernate实现多数据源配置,这篇文章则讲述了整合mybatis的情况,对于Hibernate,其自带的各类数据库模板操作方法能够很方便快捷的完成基本的增删改查功能,特点:简单,快捷,而与之相对应的则是它的灵活性就没那么好了,当需要进行复杂的操作(诸如多表查询),Hibernate就有点捉襟见肘了。反观mybatis,由于其直接使用SQL语句完成程序到数据库的相应操作,无论多么简单的操作,你都得写出其完整的SQL语句,因此显得比较繁琐,而在另一方面,由于直接使用SQL语句完成操作,其灵活性不言而喻。至于选择哪一个框架,则视具体的需求而定。
一、创建Springboot项目
创建项目的方式多种多样,可以任意选择,结果都是一样的
<1>直接登陆Spring Initializer页面,在页面选择项目类型(如Maven Project)、Spring boot版本等,填写项目名,添加依赖后,点击“Generate Project”就会下载一个压缩包。下载后,解压缩,在idea或者eclipse中将其打开即可。
<2>在idea中新建一个Maven项目,然后添加jar包依赖,在我之前的一篇文章Spring-datatable-Jpa实现增删改查及分页有一个简单的介绍,需要的初学者可以看下。
<3>在idea中新建一个Spring Initializer项目,一路next,在需要填入项目名的时候,填入即可。
二、POM.xml
pom.xml中导入的依赖项即是项目所需要导入的各种jar包,本文所用到的jar包如下:
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.40</version>
</dependency>
<!--部署热启动依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration> <!--热启动配置-->
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>