zoukankan      html  css  js  c++  java
  • springboot集成mybatis

    springboot集成mybatis

    pom文件

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>
       <parent>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-parent</artifactId>
           <version>2.3.5.RELEASE</version>
           <relativePath/> <!-- lookup parent from repository -->
       </parent>
       <groupId>com.dtg</groupId>
       <artifactId>springmybatis</artifactId>
       <version>0.0.1-SNAPSHOT</version>
       <name>springmybatis</name>
       <description>Demo project for Spring Boot</description>

       <properties>
           <java.version>1.8</java.version>
       </properties>

       <dependencies>
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-jdbc</artifactId>
           </dependency>
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-web</artifactId>
           </dependency>
           <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>
               <scope>runtime</scope>
           </dependency>
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-test</artifactId>
               <scope>test</scope>
               <exclusions>
                   <exclusion>
                       <groupId>org.junit.vintage</groupId>
                       <artifactId>junit-vintage-engine</artifactId>
                   </exclusion>
               </exclusions>
           </dependency>
       </dependencies>

       <build>
           <plugins>
               <plugin>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-maven-plugin</artifactId>
               </plugin>
           </plugins>
       </build>

    </project>

    application.properties

    server.port=8011

    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

    mybatis.mapper-locations=classpath:mapping/*Mapper.xml
    mybatis.type-aliases-package=com.dtg.springmybatis.domain

     

    创建工程包目录

    domain

    mapper

    service

    controller

    数据库建表语句


    CREATE TABLE `customer` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `customerName` varchar(128) NOT NULL DEFAULT '' COMMENT '客户名',
    `mobile` varchar(32) NOT NULL DEFAULT '' COMMENT '客户手机',
    `phone` varchar(32) NOT NULL DEFAULT '' COMMENT '客户电话',
    `addr` varchar(256) NOT NULL COMMENT '地址',
    `postCode` varchar(13) NOT NULL COMMENT '邮编',
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='客户表';

    实体类Customer

    package com.dtg.springmybatis.domain;

    public class Customer {
       Integer id;
       String customerName;
       String phone;
       String mobile;
       String addr;
       String postCode;

       public Integer getId() {
           return id;
      }

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

       public String getCustomerName() {
           return customerName;
      }

       public void setCustomerName(String customerName) {
           this.customerName = customerName;
      }

       public String getPhone() {
           return phone;
      }

       public void setPhone(String phone) {
           this.phone = phone;
      }

       public String getMobile() {
           return mobile;
      }

       public void setMobile(String mobile) {
           this.mobile = mobile;
      }

       public String getAddr() {
           return addr;
      }

       public void setAddr(String addr) {
           this.addr = addr;
      }

       public String getPostCode() {
           return postCode;
      }

       public void setPostCode(String postCode) {
           this.postCode = postCode;
      }
    }

     

    Mapper接口类CustomerMapper

    package com.dtg.springmybatis.mapper;

    import com.dtg.springmybatis.domain.Customer;

    import java.util.List;

    public interface CustomerMapper {
       List <Customer> queryList();
       Customer queryCustomer(Integer id);
    }

     

    Mapper配置文件customerMapper.xml

    1. resources下创建目录mapping

    2. mapping目示下添加文件customerMapper.xml

      <?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.dtg.springmybatis.mapper.CustomerMapper">

         <!-- 可根据自己的需求,是否要使用 -->
         <resultMap type="Customer" id="CustomerResult">
             <id column="id" property="id" jdbcType="INTEGER" />
             <result column="customerName" property="customerName" jdbcType="VARCHAR" />
             <result column="phone" property="phone" jdbcType="VARCHAR" />
             <result column="mobile" property="mobile" jdbcType="VARCHAR" />
             <result column="addr" property="addr" jdbcType="VARCHAR" />
             <result column="postCode" property="postCode" jdbcType="VARCHAR" />
         </resultMap>

         <select id="queryCustomer" parameterType="INTEGER" resultMap="CustomerResult">
            select *
            from customer
            where 1=1
                and id = #{id,jdbcType=INTEGER}
         </select>

         <select id="queryList" parameterType="INTEGER" resultMap="CustomerResult">
            select *
            from customer
         </select>

      </mapper>

       

    服务接口类CustomerService

    package com.dtgtt.springbootmybatis.service;

    import com.dtgtt.springbootmybatis.entity.Customer;

    import java.util.List;

    public interface CustomerService {
       List<Customer> list();

       Customer queryCustomer(Integer id);
    }

     

    服务实现类CustomerServiceImpl

    package com.dtg.springmybatis.service.impl;


    import com.dtg.springmybatis.domain.Customer;
    import com.dtg.springmybatis.mapper.CustomerMapper;
    import com.dtg.springmybatis.service.CustomerService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import java.util.List;

    @Service
    public class CustomerServiceImpl implements CustomerService {

       @Autowired
       CustomerMapper customerMapper;

       public List<Customer> list(){
           List<Customer> list = customerMapper.queryList();
           return list;
      }

       @Override
       public Customer queryCustomer(Integer id) {
           Customer c = customerMapper.queryCustomer(id);
           return c;
      }
    }

     

    业务控制层MainController

    package com.dtg.springmybatis.controller;

    import com.dtg.springmybatis.domain.Customer;
    import com.dtg.springmybatis.service.CustomerService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;

    import java.util.List;

    @RestController
    @RequestMapping("/main")
    public class MainController {


       @Autowired
       CustomerService customerService;

       @RequestMapping("/list")
       public List<Customer> queryCustomerList(){

           List<Customer> list = customerService.list();
           return list;
      }

       @RequestMapping("/list")
       public Customer queryCustomer(@RequestParam("id") Integer id){

           Customer c  = customerService.queryCustomer(id);
           return c;
      }


    }

    springboot启动类增加@MapperScan注解

    package com.dtg.springmybatis;

    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication
    @MapperScan("com.dtg.springmybatis.mapper")
    public class SpringmybatisApplication {

       public static void main(String[] args) {
           SpringApplication.run(SpringmybatisApplication.class, args);
      }

    }

     

    查询操作

    http://localhost:8011/main/list3

    http://localhost:8011/main/query?id=1

     

     

    个人理解

    mybatis 的使用主要涉及到mapper接口的定义(@MapperScan可扫描相关的接口)和mapper映射文件(mybatis.mapper-locations 指定了映射文件的位置)。

    mapper接口提供了业务系统与mybatis交互的入口

    mapper映射文件则提供了sql的编写配置方式。

    mybatis内部会根据mapper接口的定义,找到相关的mapper映射xml文件,获取要查询的sql,然后对数据库进行操作,并将操作结果封装为mapper接口对应的返回值。

     

     

  • 相关阅读:
    电商零售数仓建模之平台01:平台业务模型
    js自动补零
    Git
    C# HttpClient 请求认证、数据传输笔记
    悲/乐观并发控制的区别
    Win10安装node.js始终出现2503错误解决方法
    行首和行尾快捷键
    IP地址分类和私有地址
    source面板看不到js的调试方法
    js常用类型判断
  • 原文地址:https://www.cnblogs.com/datangguott/p/13933029.html
Copyright © 2011-2022 走看看