zoukankan      html  css  js  c++  java
  • spring boot 与 Mybatis整合(*)

    1. 在pom.xml文件中加入数据库、spring-mybatis整合
      <!-- spring boot 整合mybatis -->
              <dependency>
                  <groupId>org.mybatis.spring.boot</groupId>
                  <artifactId>mybatis-spring-boot-starter</artifactId>
                  <version>1.3.0</version>
              </dependency>
              <!-- MYSQL -->
              <dependency>
                  <groupId>mysql</groupId>
                  <artifactId>mysql-connector-java</artifactId>
              </dependency>
    2. 在springboot的启动类中添加注解
      package cn.java.controller;
      
      import org.mybatis.spring.annotation.MapperScan;
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      
      /*
       * spring boot 启动程序
       */
      @SpringBootApplication(scanBasePackages= {"cn.java.controller","cn.java.service.impl"})
      @EnableAutoConfiguration
      //配置mapper层扫描
      @MapperScan(basePackages="cn.java.mapper")
      public class StartApplication {
          public static void main(String[] args) {
              SpringApplication.run(StartApplication.class, args);
          }
      
      }

    控制层GoodController 

    package cn.java.controller;
    
    import java.util.List;
    import java.util.Map;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import cn.java.service.GoodService;
    
    @Controller
    @RequestMapping("/goods/")
    public class GoodController {
        @Autowired
        private GoodService goodService;
        //获取goods表中所有数据
        @RequestMapping("/selectAllGoods.do")
        @ResponseBody
        public List<Map<String,Object>> selectAllGoods(){
            return goodService.getAll();
        }
    }

     业务层

    package cn.java.service;
    
    import java.util.List;
    import java.util.Map;
    
    public interface GoodService {
    
        List<Map<String, Object>> getAll();
    
    }
    package cn.java.service.impl;
    
    import java.util.List;
    import java.util.Map;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import cn.java.mapper.GoodMapper;
    import cn.java.service.GoodService;
    @Service
    public class GoodServiceImpl implements GoodService {
        /*
         * 获取springboot库下goods表中所有的数据 
         */
        @Autowired
        private GoodMapper goodMapper;
        
        /*
         * 查询所有商品
         */
        
        /* (non-Javadoc)
         * @see cn.java.service.impl.GoodService#getAll()
         */
        @Override
        public List<Map<String,Object>> getAll(){
            return goodMapper.getAllGoods();
        }
    }

    DAO层

    package cn.java.mapper;
    
    import java.util.List;
    import java.util.Map;
    
    import org.apache.ibatis.annotations.Select;
    
    public interface GoodMapper {
        @Select("SELECT * FROM goods")
        public List<Map<String,Object>> getAllGoods();
    }

    application.properties

    #update tomcat port
    server.port=8888
    
    #config datasource(mysql)
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql:///springboot
    spring.datasource.username=root
    spring.datasource.password=78978r

    实体类

    package cn.java.entity;
    
    public class Good {
        private String goodName;
        private Float goodPrice;
        private String goodPhone;
        private String goodAddress;
        private Integer goodNum;
        
        public String getGoodName() {
            return goodName;
        }
        public void setGoodName(String goodName) {
            this.goodName = goodName;
        }
        public Float getGoodPrice() {
            return goodPrice;
        }
        public void setGoodPrice(Float goodPrice) {
            this.goodPrice = goodPrice;
        }
        public String getGoodPhone() {
            return goodPhone;
        }
        public void setGoodPhone(String goodPhone) {
            this.goodPhone = goodPhone;
        }
        public String getGoodAddress() {
            return goodAddress;
        }
        public void setGoodAddress(String goodAddress) {
            this.goodAddress = goodAddress;
        }
        public Integer getGoodNum() {
            return goodNum;
        }
        public void setGoodNum(Integer goodNum) {
            this.goodNum = goodNum;
        }
        
        
    
    }

    数据库springboot

    CREATE TABLE `springboot`.`goods`(  
      `id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
      `good_name` VARCHAR(40) COMMENT '商品名称',
      `good_price` FLOAT(10,2) COMMENT '商品单价',
      `good_phone` VARCHAR(20) COMMENT '商品联系电话',
      `good_address` VARCHAR(40) COMMENT '商品地址',
      `good_num` INT COMMENT '商品库存',
      PRIMARY KEY (`id`)
    );

     GoodMapper.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="cn.java.mapper.GoodMapper">
        <resultMap id="BaseResultMap" type="cn.java.entity.Good">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <id column="good_name" property="goodName" jdbcType="VARCHAR"/>
        <id column="good_price" property="goodPrice" jdbcType="REAL"/>
        <id column="good_phone" property="goodPhone" jdbcType="VARCHAR"/>
        <id column="good_address" property="goodAddress" jdbcType="VARCHAR"/>
        <id column="good_num" property="goodNum" jdbcType="INTEGER"/>
        </resultMap>
        <sql id="Base_Column_List">
            id,good_name,good_price_good_phone,good_address,good_num
        </sql>
        
        <!-- 查询所有 -->
        <select id="selectAllGoods" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"></include> FROM goods;
        </select>
        
        
        <!-- <select id="selectByPrimarKey" resultMap="BaseResultMap" parmeterType="java.lang.Long">
            select
            <include refid="Base_Column_List">
                from goods
                where id = #{id,jdbcType=BIGINT}
            </include>
        </select>
        <delete id="deleteByPrimaryKey" parmeterType="java.lang.Long">
            delete from goods
            where id = #{id,jdbcType=BIGINT}
        </delete>
        <insert id="insert" parmeterType="cn.java.entity.Good">
            insert into goods(id,good_name,good_price,good_phone,good_address,good_num
            )
            values(#{id,jdbcType=BIGINT},#{goodName,jdbcType=VARCHAR},#{goodPrice,jdbcType=REAL},#{goodPhone,jdbcType=VARCHAR},
            #{goodAddress,jdbcType=VARCHAR},#{goodNum,jdbcType=INTEGER}
            )
        </insert>
        <insert id="insertSelective" parmeterType="cn.java.entity.Good">
        insert into goods
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="id != null">
        id,
        </if>
        <if test="goodName != null">
        good_name,
        </if>
        <if test="goodPrice != null">
        good_price,
        </if>
        <if test="goodPhone != null">
        good_phone,
        </if>
        <if test="goodAddress != null">
        good_address,
        </if>
        <if test="goodNum != null">
        good_num,
        </if>
        </trim>    
        <trim prefix="values(" suffix=")" suffixOverrides=",">
        <if test="id != null">
            #{id,jdbcType=BIHINT},
        </if>
        <if test="goodName != null">
            #{goodName,jdbcType=VARCHAR},
        </if>
        <if test="goodPrice != null">
            #{goodPrice,jdbcType=REAL},
        </if>
        <if test="goodPhone != null">
            #{goodPhone,jdbcType=VARCHAR},
        </if>
        <if test="goodAddress != null">
            #{goodAddress,jdbcType=VARCHAR},
        </if>
        <if test="goodNum != null">
            #{goodNum,jdbcType=INTEGER},
        </if>
        </trim>
        </insert>
        <update id="updateByPrimaryKeySelective" prameterType="cn.java.entity.Good">
        update goods
        set    good_name = #{goodName,jdbcType=VARCHAR},
            good_price = #{goodName,jdbcType=VARCHAR},
            good_phone = #{goodPhone,jdbcType=VARCHAR},
            good_address = #{goodAddress,jdbcType=VARCHAR},
            good_num = #{goodNum,jdbcType=INTEGER}
            where id = #{id,jdbcType=BIHINT}
        </update> -->
        
    </mapper>

     (3)当MyBatis  语句较为复杂时  使用xml更加方便

    #config mybatis xml
    mybatis.mapper-location=classpath:mapping/*.xml
    mybatis.type-aliases-package=cn.java.entity

     控制层与业务层  业务层与DAO层 通过接口连接

  • 相关阅读:
    node.js是什么
    python基础 filter ,列表,字典,集合 中根据 条件 筛选 数据
    nginx 自动补全www,当不输入www时候自动补全www
    python爬虫,接口是post请求,参数是request payload 的形式,如何传参
    python使用with开启线程锁
    linux nohup后台执行脚本并指定文件输出 ,nohup 修改默认日志输出文件
    python线程锁
    nginx yum安装启动
    redis desktop manager 远程连接服务器上的redis
    职位列表中英对照
  • 原文地址:https://www.cnblogs.com/mzdljgz/p/10508148.html
Copyright © 2011-2022 走看看