mybatis entry and profile template

  1. Project steps to establish mybatis  

    1. Add dependencies four databases Junit mybatis 1.8jdk

    2. Write pojo objects (domain)

    3. Write core configuration file (config.xml)

    4. Write core configuration file written mapping file (sqlMapper.xml)

    5. Testing Framework

  2.   Implementation

    1.  Add dependencies four databases Junit mybatis 1.8jdk

      1.   Chong key project in maven, a jar with maven management pack 

      2.         Configuring maven pom.xml file,

      3.  pom.xml source template
      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.  1 <?xml version="1.0" encoding="UTF-8" ?>
         2 
         3 <!DOCTYPE configuration
         4         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
         5         "http://mybatis.org/dtd/mybatis-3-config.dtd">
         6 
         7 <configuration>
         8 
         9     <environments default="development">
        10         <environment id="development">
        11             <transactionManager type="JDBC" />
        12             <dataSource type="POOLED">
        13                 <property name="driver" value="com.mysql.jdbc.Driver" />
        14                 <property name="url" value="jdbc:mysql://127.0.0.1:3306/kgcnews?characterEncoding=utf8" />
        15                 <property name="username" value="root" />
        16                 <property name="password" value="root" />
        17             </dataSource>
        18         </environment>
        19     </environments>
        20 
        21     <mappers>
        22         <mapper resource="sqlMapper/newsDetailMapper.xml"></mapper>
        23     </mappers>
        24 
        25 </configuration>
        View Code   
    4.   编写核心配置文件的映射文件(sqlMapper.xml)

      1. sqlMapper.xml模板

      2.  1 <?xml version="1.0" encoding="utf-8" ?>
         2 
         3 <!DOCTYPE mapper
         4         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         5         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
         6 
         7 <mapper namespace="cn.kgc.pojo">
         8 
         9     <select id="findAll" resultType="cn.kgc.pojo.NewsDetail">
        10         select * from kgcnews.news_detail;
        11     </select>
        12 
        13 </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    

Guess you like

Origin www.cnblogs.com/MrliBlog/p/11104273.html