mybatis入門及配置文件模板


  1. 建立mybatis項目步驟  

    1. 添加依賴包  4個  數據庫   Junit  mybatis   1.8jdk

    2. 編寫pojo對象(domain)

    3. 編寫核心配置文件(config.xml)

    4. 編寫核心配置文件編寫映射文件(sqlMapper.xml)

    5. 測試框架

  2.   具體實現

    1.  添加依賴包  4個  數據庫   Junit  mybatis   1.8jdk

      1.   在maven中創鍵項目,用maven管理jar包 

      2.         配置maven pom.xml文件,

      3.  pom.xml源碼模板
      4.  1 <?xml version="1.0" encoding="UTF-8"?>
         2 <project xmlns="http://maven.apache.org/POM/4.0.0"
         3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
         5     <modelVersion>4.0.0</modelVersion>
         6 
         7     <groupId>cn.kgc</groupId>
         8     <artifactId>mybatis03</artifactId>
         9     <version>1.0-SNAPSHOT</version>
        10 
        11     <properties>
        12         <maven.coppiler.source>1.8</maven.coppiler.source>
        13         <maven.coppiler.target>1.8</maven.coppiler.target>
        14     </properties>
        15 
        16     <dependencies>
        17         <dependency>
        18             <groupId>mysql</groupId>
        19             <artifactId>mysql-connector-java</artifactId>
        20             <version>5.1.6</version>
        21         </dependency>
        22         <dependency>
        23             <groupId>junit</groupId>
        24             <artifactId>junit</artifactId>
        25             <version>4.12</version>
        26         </dependency>
        27         <dependency>
        28             <groupId>org.mybatis</groupId>
        29             <artifactId>mybatis</artifactId>
        30             <version>3.4.5</version>
        31         </dependency>
        32     </dependencies>
        33 
        34 
        35 </project>
        View Code

         

    2. 編寫pojo對象

      1.   編寫與數據庫表對應的實體類(在idea中可以直接生成)

      2.   

         

         

    3.   編寫核心配置文件(config.xml)

      1. <?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>
          <environments default="development">
            <environment id="development">
              <transactionManager type="JDBC"/>
              <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
              </dataSource>
            </environment>
          </environments>
          <mappers>
            <mapper resource="org/mybatis/example/BlogMapper.xml"/>
          </mappers>
        </configuration>
        View Code

         

         
    4.   編寫核心配置文件的映射文件(sqlMapper.xml)

      1. sqlMapper.xml模板

      2.  
                       
        <?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="org.mybatis.example.BlogMapper"> <select id="selectBlog" resultType="Blog"> select * from Blog where id = #{id} </select> </mapper>
         
    5.   測試框架

      1.   測試文件

      2.  1 package cn.kgc;
         2 
         3 import cn.kgc.pojo.NewsDetail;
         4 import org.apache.ibatis.session.SqlSession;
         5 import org.apache.ibatis.session.SqlSessionFactory;
         6 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
         7 import org.junit.Test;
         8 
         9 import java.io.InputStream;
        10 import java.util.List;
        11 
        12 /**
        13  * @author liyang
        14  * @create 2019/6/28 16:18
        15  */
        16 public class TestNewDetail {
        17     @Test
        18     public void test(){
        19         InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("SqlMapConfig.xml");
        20         SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        21         SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
        22         SqlSession sqlSession = build.openSession();
        23         List<Object> objects = sqlSession.selectList("cn.kgc.pojo.findAll");
        24         for (Object o : objects) {
        25             NewsDetail newsDetail = (NewsDetail)o;
        26             System.out.println(newsDetail);
        27         }
        28     }
        29 }
        View Code    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM