码迷,mamicode.com
首页 > 编程语言 > 详细

springboot整合Mybatis(一)——入门

时间:2018-02-12 13:42:28      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:max   1.3   mave   bsp   支持   script   wal   jdbc   web   

一、概述

  1.先导

    mybatis入门随笔:http://www.cnblogs.com/jiangbei/p/6884641.html

 

  2.引入依赖

       <dependency>
              <groupId>org.mybatis.spring.boot</groupId>
              <artifactId>mybatis-spring-boot-starter</artifactId>
              <version>1.3.0</version>
            </dependency>

    //其他必须的依赖还包括mysql连接,web依赖(web项目),此处贴出完整依赖(有部分依赖例如jpa此处可不选)

技术分享图片
<?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>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot_demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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-thymeleaf</artifactId>
        </dependency>
        <!--spring-boot-starter-data-jpa包含spring-data-jpa、spring-orm 和 Hibernate 来支持 JPA-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- mysql连接-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!-- 阿里druid连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>
        <!-- AOP支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <!-- spring-boot-devtools热部署支持-->
        <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>
                <dependencies>
                    <!-- spring热部署 -->
                   <!-- <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>springloaded</artifactId>
                        <version>1.2.6.RELEASE</version>
                    </dependency>-->
                </dependencies>
            </plugin>
        </plugins>
    </build>


</project>
pom.xml 

  额外功能(后文陆续补充)

    •   PageHelper 分页插件
    •   mybatis generator 自动生成代码插件

   3.数据源配置

    依旧是前文的druid数据源

技术分享图片
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/sakila
    username: root
    password: root
    # druid配置项,默认spring-boot不支持,故需要config类来解析
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    filters: stat,wall,log4j
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
application.yml

  4.起步

    以下介绍两种整合方式:

      传统的mapper.xml映射文件

      springboot提倡的无配置注解形式

二、无配置注解形式

  1.配置mybatis:

mybatis:
  type-aliases-package: com.example.demo.bean

  2.Bean

    沿用之前的bean:girl

技术分享图片
package com.example.demo.bean;

import javax.validation.constraints.Min;
import javax.validation.constraints.Size;

/**
 * girl的bean
 *
 * @author zcc ON 2018/2/9
 **/

public class Girl {
    private Integer id;
    @Size(min = 3, max = 8, message = "名称长度不合法!")
    private String name;
    @Min(value = 18, message = "未成年禁止入内!")
    private Integer age;

    public Girl() {
    }

    public Girl(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Girl{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ‘}‘;
    }
}
Girl.java

  3.Mapper 

package com.example.demo.mapper;

import com.example.demo.bean.Girl;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * GirlMapper
 *
 * @author zcc
 **/
// @Mapper 这里可以使用@Mapper注解,但是每个mapper都加注解比较麻烦,所以也可以统一配置@MapperScan在扫描路径在application类中
@Mapper
public interface GirlMapper {

    @Select("SELECT * FROM girl WHERE id = #{id}")
    Girl findGirlById(Integer id);

    @Select("SELECT * FROM girl")
    List<Girl> findAll();

    @Insert("INSERT INTO girl VALUES(NULL,#{age},#{name})")
    int addGirl(Girl girl);

    @Update("UPDATE girl SET age = #{girl.age},name = #{girl.name} WHERE id = #{id}")
    int updateGirl(@Param("id") Integer id, @Param("girl") Girl girl);

    @Delete("DELETE FROM girl WHERE id = #{id}")
    int deleteGirl(Integer id);
}

    以上注解的使用基本与之前xml中的用法一致,完整注解,参考官网:http://www.mybatis.org/mybatis-3/zh/java-api.html

 

springboot整合Mybatis(一)——入门

标签:max   1.3   mave   bsp   支持   script   wal   jdbc   web   

原文地址:https://www.cnblogs.com/jiangbei/p/8444110.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!