1. 程式人生 > 資料庫 >sqlmap --os-shell執行原理(mysql篇)

sqlmap --os-shell執行原理(mysql篇)

SSM框架整合

環境

  • IDEA
  • MySQL 5.5
  • Tomcat 8.5.34
  • Maven 3.6.0

搭建資料庫

CREATE DATABASE `ssmbuild`;

USE `ssmbuild`;

DROP TABLE IF EXISTS `books`;

CREATE TABLE `books` (
`bookID` INT(10) NOT NULL AUTO_INCREMENT COMMENT '書id',
`bookName` VARCHAR(100) NOT NULL COMMENT '書名',
`bookCounts` INT(11) NOT NULL COMMENT '數量',
`detail` VARCHAR(200) NOT NULL COMMENT '描述',
KEY `bookID` (`bookID`)
) ENGINE=INNODB DEFAULT CHARSET=utf8

INSERT  INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
(1,'Java',1,'從入門到放棄'),
(2,'MySQL',10,'從刪庫到跑路'),
(3,'Linux',5,'從入門到進牢');

基本專案的搭建

  1. 新建maven專案,新增web支援

  2. 匯入相關的maven依賴

    <dependencies>
       <!--Junit-->
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.12</version>
       </dependency>
       <!--資料庫驅動-->
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>5.1.47</version>
       </dependency>
       <!-- 資料庫連線池 -->
       <dependency>
           <groupId>com.mchange</groupId>
           <artifactId>c3p0</artifactId>
           <version>0.9.5.2</version>
       </dependency>
    
       <!--Servlet - JSP -->
       <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>servlet-api</artifactId>
           <version>2.5</version>
       </dependency>
       <dependency>
           <groupId>javax.servlet.jsp</groupId>
           <artifactId>jsp-api</artifactId>
           <version>2.2</version>
       </dependency>
       <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>jstl</artifactId>
           <version>1.2</version>
       </dependency>
    
       <!--Mybatis-->
       <dependency>
           <groupId>org.mybatis</groupId>
           <artifactId>mybatis</artifactId>
           <version>3.5.2</version>
       </dependency>
       <dependency>
           <groupId>org.mybatis</groupId>
           <artifactId>mybatis-spring</artifactId>
           <version>2.0.2</version>
       </dependency>
    
       <!--Spring-->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-webmvc</artifactId>
           <version>5.1.9.RELEASE</version>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-jdbc</artifactId>
           <version>5.1.9.RELEASE</version>
       </dependency>
    </dependencies>
    

    3.Maven資源過濾問題

    <build>
       <resources>
           <resource>
               <directory>src/main/java</directory>
               <includes>
                   <include>**/*.properties</include>
                   <include>**/*.xml</include>
               </includes>
               <filtering>false</filtering>
           </resource>
           <resource>
               <directory>src/main/resources</directory>
               <includes>
                   <include>**/*.properties</include>
                   <include>**/*.xml</include>
               </includes>
               <filtering>false</filtering>
           </resource>
       </resources>
    </build>
    
    1. 建立基本結構和配置框架

      • com.inspur.pojo (持久層)

      • com.inspur.dao (Dao層)

      • com.inspur.service (業務處理層)

      • com.inspur.controller (業務控制層)

      • mybatis-config.xml (可在IDEA中設定xml模版) --> File | Settings | 編輯器 | 檔案和程式碼模板

        <?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE configuration
                PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
                "http://mybatis.org/dtd/mybatis-3-config.dtd">
         
        <configuration>
            
            <!-- 別名設定 pojo:實體類上可加註解 @Alias("books")-->
            <!--可在Mapper.xml檔案中使用時無需類的全限定名,預設為類名 -->
            <typeAliases>
                <package name=""/>
            </typeAliases>
            <!-- mapper檔案路徑 -->
            <mappers>
                <mapper resource=""></mapper>
            </mappers>
        </configuration>
        
      • applicationContext.xml (IDEA含有spring的初始化的配置檔案)

        <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://www.springframework.org/schema/beans 		    http://www.springframework.org/schema/beans/spring-beans.xsd">
        
        </beans>
        

Mybatis層

  1. 資料庫配置檔案db.properties (設為模版,同上), --> 在Spring配置檔案中讀取

    jdbc.driver=com.mysql.jdbc.Driver
    #如果使用MySQL8.0+,需要增加一個時區的配置; &serverTimezone=Asia/Shanghai
    jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
    jdbc.username=root
    jdbc.password=123456
    
  2. IDEA關聯資料庫(idea自帶的資料庫連結)

  3. 編寫(完善配置資訊 注意標籤的順序)MyBatis的核心配置檔案(mybatis-config.xml)

        <!-- 標準的 日誌工廠實現  -->
        <settings>
            <setting name="logImpl" value="STDOUT_LOGGING"/>
            <!--  是否開啟自動駝峰命名規則 (camel case)對映 -->
            <!--  <setting name="mapUnderscoreToCamelCase" value="true"/>-->
    		<!-- 開啟全域性快取  -->
            <setting name="cacheEnabled" value="true"/>
        </settings>
    
    
    <typeAliases>
    	<package name="com.inspur.pojo"/>
    </typeAliases>
    <mappers>
    	<mapper resource="com.inspur.dao.BookNapper.xml"/>
    </mappers>
    
  4. 編寫資料庫對應的實體類com.inspur.pojo.Books

    使用Lombok外掛
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.apache.ibatis.type.Alias;
    import java.io.Serializable;
    /**
     * @author 12093
     * @Created by 12093
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Alias("books")   //可在Mapper.xml檔案中使用,無需類的全限定名
    public class Books implements Serializable {
         //我們需要將實體類序列化(implements Serializable)!否則就會報錯!
         private int bookID;
         private String bookName;
         private int bookCounts;
         private String detail;
    }
    
  5. 編寫Dao層的Mapper介面

    import com.inspur.pojo.Books;
    import org.apache.ibatis.annotations.Param;
    import java.util.List;
    /**
     * @author 12093
     */
    public interface BookMapper {
        //實現 增/刪/改/查
        // 增加一本書
        int addBook(Books books);
        //  刪除一本書
        int deleteBookById(@Param("bookID") int id);
        // 更新一本書
        int updateBookById(Books books);
    	// 查詢一本書
        Books queryBookById(@Param("bookID") int id);
        // 查詢全部的書
        List<Books> queryAllBook();
        //根據ID查詢一本書
        List queryBookByName(@Param("bookName") String bookName);
        
    }
    
  6. 編寫介面對應的Mapper.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">
    <!--   MyBatis對映檔案  -->
    <!--namespace=繫結一個對應的Dao/Mapper介面-->
    <mapper namespace="com.inspur.dao.BookMapper">
        <cache/>   <!-- 設定此xml檔案,使用二級快取  -->
        
        <!--增加一個Book-->
        <insert id="addBook" parameterType="Books">
            insert into books (bookName,bookCounts,detail)
            values (#{bookName},#{bookCounts},#{detail});
        </insert>
    
         <!--根據id刪除一個Book-->
        <delete id="deleteBookById" parameterType="int">
            delete from books where bookID = #{bookID}
        </delete>
    
        <!--更新Book-->
        <update id="updateBookById" parameterType="Books">
            update books
            set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail}
            where bookID=#{bookID};
        </update>
    
        <!--根據id查詢,返回一個Book-->
        <select id="queryBookById" resultType="Books">
            select * from books 
            where bookID = #{bookID}
        </select>
        
        <!--查詢全部Book-->
        <select id="queryAllBook" resultType="Books">
            select * from books;
        </select>
        
        <!--根據bookName查詢,返回一個Book-->
        <select id="queryBookByName" resultType="Books">
            select * from books where bookName like "%"#{bookName}"%"
        </select>
        
    </mapper>
    
  7. 編寫Service層的介面和實現類

    介面:

    package com.inspur.service;
    
    import com.inspur.pojo.Books;
    import java.util.List;
    /**
     * @author 12093
     * @Classname BookService
     * @Date 2020/11/8 22:58
     *   BookService:底下需要去實現,呼叫dao層
     */
    public interface BookService {
        // 增加一本Book
        int addBook(Books books);
        
        //根據id刪除一本書Book
        int deleteBookById(int id);
    
        // 更新Book
        int updateBookById(Books books);
    
        //根據ID查詢,返回一個Book
        Books queryBookById(int id);
    
        //查詢全部Book,返回list集合
        List<Books> queryAllBook();
        
        //根據書名模糊查詢一本書
     List<Books> queryBookByName(@Param("bookName") String bookName);
    }
    
   
   實現類:
   
   ```java
   mport com.inspur.dao.BookMapper;
   import com.inspur.pojo.Books;
   import java.util.List;
   public class BookServiceImpl implements BookService{
      
       //service調dao層:  組合Dao    可新增業務
       private BookMapper bookMapper;
   
       public void setBookMapper(BookMapper bookMapper) {
           this.bookMapper = bookMapper;
       }
   
       @Override
       public int addBook(Books books) {
           return bookMapper.addBook(books);
       }
   
       @Override
       public int deleteBookById(int id) {
           return bookMapper.deleteBookById(id);
       }
   
       @Override
       public int updateBookById(Books books) {
           return bookMapper.updateBookById(books);
       }
   
       @Override
       public Books queryBookById(int id) {
           return bookMapper.queryBookById(id);
       }
   
       @Override
       public List<Books> queryAllBook() {
           return bookMapper.queryAllBook();
    }
       
     @Override
       public List<Books> queryBookByName(String bookName) {
           return bookMapper.queryBookByName(bookName);
       }
   }

底層需求操作完畢

Spring層