springboot整合mybatis

一、开发环境
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 -->
        <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

### 构建任务失败解决方案 当遇到 `Execution failed for task ':app:shrinkReleaseRes'` 错误时,这通常意味着资源压缩过程中出现了问题。此错误可能由多种原因引起,包括但不限于配置不正确、依赖冲突或特定于项目的其他因素。 #### 可能的原因分析 1. **ProGuard 或 R8 配置不当** ProGuard 和 R8 是用于优化和混淆代码以及减少 APK 大小的工具。如果这些工具的配置存在问题,可能会导致资源无法正常处理[^1]。 2. **重复资源** 如果项目中有多个模块定义了相同的资源名称,可能导致冲突并引发该错误。检查是否存在重名的 drawable、string 等资源文件[^2]。 3. **第三方库兼容性** 某些第三方库可能与当前使用的 Gradle 插件版本或其他库存在兼容性问题,从而影响到资源打包过程中的行为[^3]。 4. **Gradle 缓存问题** 有时旧缓存数据会干扰新编译的结果,尝试清理本地仓库和重新同步项目可以帮助排除此类潜在障碍[^4]。 #### 推荐的操作方法 为了有效解决问题,建议按照以下步骤逐一排查: ```bash # 清理项目构建目录 ./gradlew clean # 删除 .gradle 文件夹下的所有内容以清除缓存 rm -rf ~/.gradle/caches/ ``` 调整 `build.gradle` 中的相关设置也是一个重要环节: ```groovy android { ... buildTypes { release { minifyEnabled true // 是否启用代码缩减 shrinkResources true // 是否开启资源压缩 proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' // 尝试禁用 shrinkResources 来测试是否为资源压缩引起的错误 // shrinkResources false } } } ``` 此外,在 `proguard-rules.pro` 文件内添加必要的保留规则,防止关键类被意外移除: ```text -keep class com.example.yourpackage.** { *; } # 替换为你自己的包路径 -dontwarn androidx.**,com.google.** # 忽略警告信息 ``` 最后,确保所使用的 Android Studio 版本是最新的稳定版,并且已经应用了所有的补丁更新。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值