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();
        }
    }

    九、运行结果如下

      

  • 相关阅读:
    创建数据库指定编码格式
    java开发环境配置
    Eclipse 配置工程
    声明式事务管理 的5 种方式
    web容器启动顺序
    2.1 实践篇:使用ping来检测网速
    1.1 mysql安装
    1.2 测试人员与开发人员比例
    1.0 软件测试能力
    1.4 测试各阶段(单元、集成、系统 、Alpha、Beta、验收)
  • 原文地址:https://www.cnblogs.com/tinghao/p/11989899.html
Copyright © 2011-2022 走看看