您的位置:首页 > 编程语言 > Java开发

springboot整合mybatis

2017-07-20 16:30 381 查看
一、开发环境

jdk:1.8

编译工具:Intellij IDEA 15

版本控制:Maven

二、pom.xml

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springboot.mybatis.version>1.2.0</springboot.mybatis.version>
<junit.version>4.12</junit.version>
<mysql.version>5.1.32</mysql.version>
<fastjson.version>1.1.34</fastjson.version>
<start-class>com.hueason.Application</start-class>
<redis.version>1.3.3.RELEASE</redis.version>
<druid.version>1.0.20</druid.version>
</properties>
<dependencies>
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MySql -->
<
4000
span class="hljs-tag"><dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${springboot.mybatis.version}</version>
</dependency>
<!-- Redis客户端 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>${redis.version}</version>
</dependency>
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>

<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${start-class}</mainClass>
</configuration>
</plugin>

</plugins>
</build>


简单说一下

1、使用springboot开发web首先设置父工程位spring-boot-starter-parent

2、引入spring-boot-starter-web

3、整合mybatis引入mybatis-spring-boot-starter

4、引入连接池和驱动,这里使用mysql驱动和druid连接池

5、sringboot官方建议前台模板使用thymeleaf,而不建议使用jsp,引入spring-boot-starter-thymeleaf

6、我这里使用了热部署,就是每次修改代码后卡都自动重启,引入spring-boot-devtools

三、包结构



1、首先看java部分,简单的三层架构,首先说一下启动类Application。

启动类的位置默认情况下,决定了它会加载的包。比如我的启动类

package com.hueason;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
* Created by Administrator on 2017/7/6.
*/
@EnableScheduling
@SpringBootApplication
@EnableTransactionManagement
@MapperScan("com.hueason.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}


它所在的包为com.hueason,那它默认就会扫描加载com.hueason下的所有注解Bean

还有就是Application上的注解

* @SpringBootApplication :它相当于@Configuration + @EnableAutoConfiguration + @ComponentScan。通常情况下这一个注解就够了

* @EnableScheduling:如果你的项目中有定时任务@Scheduled ,就需要使用这个注解来启动

* @EnableTransactionManagement:如果你的项目需要事务支持@Transactional,就需要使用这个注解来启动

* @MapperScan(“com.hueason.dao”):这个注解用来扫描Mapper,它相当于为dao包下的所有类添加了@Mapper

2、其次来看resources部分

这里首先要说的是application.yml,这是一个配置文件,使用thymeleaf后,他会默认扫描classpath下的application.yml。.yml配置文件的格式和我们以前使用的.propertise有一些区别

#thymeleaf start
spring:
thymeleaf:
mode: HTML5
encoding: UTF-8
content-type: text/html
cache: false #开发时关闭缓存,不然没法看到实时页面
#thymeleaf end
redis:
host: localhost
port: 6379
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/sms?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
username: root
password: 123456

server:
port: 80

mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.hueason.pojo


它对于格式要求的比较严格,但是idea工具的联想功能可以很方便的帮助我们,一定要注意,冒号后面的空格不能丢

springboot对于绝大多数配置都是默认的,不需要我们来操心,如果我们需要修改默认配置,只需要在配置文件配置完后就可以覆盖掉默认。

比如springboot内置了tomcat容器,默认端口号8080,我们可以通过server.port: 80 来修改

thymeleaf框架会默认扫描classpath下的static和templates文件夹,static下用于放置静态文件,如js,css等,templates下用于放置html

至此,我们的框架就搭建完毕了,下一篇给出一个前台使用Ace Admin框架的demo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: