zoukankan      html  css  js  c++  java
  • springboot整合mybatis

    springboot整合mybatis

    1、导包

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.3</version>
    </dependency>
    
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.11</version>
    </dependency>
    
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.2.2</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    

    2、实体类

    package com.yl.bean;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import java.util.Date;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Employee {
        private Integer id;
        private String name;
        private String email;
        private Date hiredate;
    }
    

    3、业务层接口

    package com.yl.service;
    
    import com.yl.bean.Employee;
    import java.util.List;
    
    public interface IEmployeeService {
        /**
         * 查询所有
         */
        List<Employee> queryAll();
    
    }
    

    4、业务层接口实现类

    package com.yl.service.impl;
    
    import com.yl.bean.Employee;
    import com.yl.mapper.IEmployeeMapper;
    import com.yl.service.IEmployeeService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class IEmployeeServiceImpl implements IEmployeeService {
        @Autowired
        private IEmployeeMapper employeeMapper;
    
        /**
         * 查询所有
         */
        @Override
        public List<Employee> queryAll() {
            return employeeMapper.queryAll();
        }
    }
    

    5、持久层接口

    package com.yl.mapper;
    
    import com.yl.bean.Employee;
    import org.springframework.stereotype.Repository;
    import java.util.List;
    
    // 推荐使用@mapper,启动类上就不用加注解指定mapper位置了
    @Repository 
    public interface IEmployeeMapper {
    
        /**
         * 查询所有
         */
        List<Employee> queryAll();
    
    }
    

    6、持久层映射文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.yl.mapper.IEmployeeMapper">
        <!--查询所有-->
        <select id="queryAll" resultType="com.yl.bean.Employee">
            select * from employee
        </select>
    </mapper>
    

    7、配置文件

    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        password: 123456
        username: root
        url: jdbc:mysql://localhost:3306/spring?characterEncoding=utf8&serverTimezone=GMT%2B8
        type: com.alibaba.druid.pool.DruidDataSource # 指定数据源类型
    mybatis:
      mapper-locations: classpath:mapper/*.xml # 指定持久层映射文件的位置
    

    8、编写配置类配置数据源

    package com.yl.configration;
    
    import com.alibaba.druid.pool.DruidDataSource;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import javax.sql.DataSource;
    
    @Configuration
    public class DruidConfig {
    
        /**
         * 将自定义的 Druid数据源添加到容器中,不再让 Spring Boot 自动创建
         * 绑定全局配置文件中的 druid 数据源属性到 com.alibaba.druid.pool.DruidDataSource从而让它们生效
         * @ConfigurationProperties(prefix = "spring.datasource"):作用就是将 全局配置文件中
         * 前缀为 spring.datasource的属性值注入到 com.alibaba.druid.pool.DruidDataSource 的同名参数中
         * @return
         */
        @Bean
        @ConfigurationProperties(prefix = "spring.datasource")
        public DataSource druidDataSource() {
            return new DruidDataSource();
        }
    }
    

    9、启动类

    package com.yl;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.Banner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    // 扫描持久层接口,如果持久层使用@mapper注解则不用扫描
    @MapperScan(basePackages = "com.yl.mapper")
    public class Springboot03Application {
    
        public static void main(String[] args) {
            SpringApplication app = new SpringApplication(Springboot03Application.class);
            app.setBannerMode(Banner.Mode.OFF);
            app.run(args);
        }
    
    }
    
    记得快乐
  • 相关阅读:
    wget下载https文件,服务器可以虚拟机中不行的问题
    FTP被动模式服务器端开有限的端口
    ZOJ
    HDU
    JAVA 大数 A+B问题
    A Simple Math Problem(矩阵快速幂)----------------------蓝桥备战系列
    Covering(矩阵快速幂)
    卡特兰数详讲(转)
    Just a Hook(线段树区间修改值)-------------蓝桥备战系列
    A Simple Problem with Integers(线段树区间更新复习,lazy数组的应用)-------------------蓝桥备战系列
  • 原文地址:https://www.cnblogs.com/Y-wee/p/14046945.html
Copyright © 2011-2022 走看看