zoukankan      html  css  js  c++  java
  • MyBatis与SpringBoot整合案例

    简单的查询案例

    一、pom.xml文件导入依赖

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--引入springboot-mybatis的依赖 -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.1.1</version>
            </dependency>
            <!--MySQL的依赖-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.32</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </dependency>
            <!-- 引入springboot-mybatis的依赖 -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.1.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.32</version>
            </dependency>
            <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>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>

    二、配置application.properties文件

    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/invoicingsystem?useUnicode=true&amp;characterEncoding=utf-8
    spring.datasource.password=123
    spring.datasource.username=root

    三、在MyBatisSpringSsmApplication配置你的路径

      

     四、创建实体类

    package com.cmy.entity;
    
    public class product {
        private Integer pid;    //商品id
        private String productName; //商品名称
        private  String quantity;   //商品价格
    
        public Integer getPid() {
            return pid;
        }
    
        public void setPid(Integer pid) {
            this.pid = pid;
        }
    
        public String getProductName() {
            return productName;
        }
    
        public void setProductName(String productName) {
            this.productName = productName;
        }
    
        public String getQuantity() {
            return quantity;
        }
    
        public void setQuantity(String quantity) {
            this.quantity = quantity;
        }
    }

    数据库中对应的表

     五、创建Dao层(创建查询所有的方法)

    package com.cmy.dao;
    
    import com.cmy.entity.product;
    import org.apache.ibatis.annotations.Select;
    import org.springframework.stereotype.Repository;
    
    import java.util.List;
    @Repository
    public interface productDao {
        //查询所有商品的方法
        @Select("select * from product")
        public List<product> getAll();
    }

    六、创建Service层

    package com.cmy.service;
    
    import com.cmy.entity.product;
    
    import java.util.List;
    
    public interface productService {
        //查询所有商品
        public List<product> getAll();
    }

    七、创建ServiceImpl层

    package com.cmy.service.impl;
    
    import com.cmy.dao.productDao;
    import com.cmy.entity.product;
    import com.cmy.service.productService;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    import java.util.List;
    @Service
    public class productServiceImpl implements productService {
        @Resource
        private productDao productdao;
        @Override
        public List<product> getAll() {
            return productdao.getAll();
        }
    }

    八、配置Controller层

    package com.cmy.controller;
    
    import com.cmy.service.productService;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import javax.annotation.Resource;
    
    @Controller
    public class productController {
        @Resource
        productService productServices;
        @RequestMapping("/selectAll")
        @ResponseBody
        public Object getAll(){
            return productServices.getAll();
        }
    }

    九、运行结果如下

      

  • 相关阅读:
    数据库基础理解学习-Mysql
    玫瑰花小制作分享-JavaScript(七夕专属浪漫)
    爬虫探索Chromedriver+Selenium初试
    Python 数据结构理解分享
    Python 实用第三方库安装方法
    Python安装-Pycharm+Anaconda
    oracle父子级查询数据树结构
    如何查看class文件的编译jdk版本号
    PL/SQL developer 11.0注册码
    linux 下tomcat出现 Native memory allocation (malloc) failed to allocate 1915224064 bytes for committing reserved memory问题
  • 原文地址:https://www.cnblogs.com/tinghao/p/11989899.html
Copyright © 2011-2022 走看看