zoukankan      html  css  js  c++  java
  • 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   
  • 相关阅读:
    android http通信——HttpURLConntection
    Android UI设计秘笈
    android文字滚动
    android半透叠加对照表
    gcc编译问题记录
    安装AIX补丁集
    php安装过程记录
    参加计算机大会了
    Linux平台mysql的安装配置
    oracle 故障案例排查
  • 原文地址:https://www.cnblogs.com/MrliBlog/p/11104273.html
Copyright © 2011-2022 走看看