idea下mybatis的简单搭建和使用<转>

  1. 首先在IDEA中创建一个 Gradle Java项目,然后一直下一步。其实GradleMeaven好用的,Gradle可以用下面网站进行搜索

    [https://package-search.jetbrains.com/]:

    360截图184306298711887.png
360截图170010185750105.png

360截图18490927516072.png
  1. 创建好项目在build.gradle引用mybatislog4j:log4jmysql-connector-java

    plugins {
        id 'java'
    }
    
    group 'com.hwp'
    version '1.0-SNAPSHOT'
    
    sourceCompatibility = 1.8
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.12'
        implementation 'org.mybatis:mybatis:3.5.5'
        implementation 'mysql:mysql-connector-java:5.1.25'
       implementation 'log4j:log4j:1.2.17' 
    }
    
    
  1. main目录下创建javaresources文件夹,其中java文件夹为Sources Root属性,resources 文件夹为 ** Resources Root** 属性

    360截图16550422698483.png

  2. resources文件夹中创建Configure.xmldb.propertieslog4j.properties具体内容如下

    Configure.xml文件为mybatis的配置文件

    
    
    
    
        
        
    
            
        
        
            
                
                
                    
                    
                    
                    
                
            
        
        
            
        
    
    
    
  1. db.properties文件为数据库的配置文件

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/book?useSSL=false
    jdbc.username=root
    jdbc.password=hwp123456
    
  2. log4j.properties为log4j的配置文件

    # Global logging configuration
    log4j.rootLogger=DEBUG, stdout
    # Console output...
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
    
  1. 在com.hwp.mapper包下创建BookMapper.java接口类

    package com.hwp.mapper;
    
    import com.hwp.bean.Book;
    
    public interface BookMapper {
        public Book queryById(int  id);
    }
    
    

    8.在com.hwp.mapper.Impl包下创建实现类

    package com.hwp.mapper.Impl;
    
    import com.hwp.bean.Book;
    import com.hwp.mapper.BookMapper;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    
    public class BookMapperImpl implements BookMapper {
        private  SqlSessionFactory sqlSessionFactory;
    
        public BookMapperImpl(SqlSessionFactory sqlSessionFactory) {
            this.sqlSessionFactory=sqlSessionFactory;
        }
    
        @Override
        public Book queryById(int id) {
            SqlSession sqlSession=null;
            Book  book = null;
            try {
                sqlSession=sqlSessionFactory.openSession();
         book=sqlSession.selectOne("com.hwp.mapper.BookMapper.queryById",id);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                if(sqlSession!=null){
                    sqlSession.close();
                }
            }
            return book;
        }
    }
    
    
    1. 创建实体类Book.java 字段名字和数据类型要和数据库对上,这个大家都知道的

          package com.hwp.bean;
          
          public class Book {
              private int id;
              private String bookName;
              private  double bookPrice;
              private    int  bookCount;
              private String  author;
              private int vend_id;
          
              public int getId() {
                  return id;
              }
          
              public void setId(int id) {
                  this.id = id;
              }
          
              public String getBookName() {
                  return bookName;
              }
          
              public void setBookName(String bookName) {
                  this.bookName = bookName;
              }
          
              public double getBookPrice() {
                  return bookPrice;
              }
          
              public void setBookPrice(double bookPrice) {
                  this.bookPrice = bookPrice;
              }
          
              public int getBookCount() {
                  return bookCount;
              }
          
              public void setBookCount(int bookCount) {
                  this.bookCount = bookCount;
              }
          
              public String getAuthor() {
                  return author;
              }
          
              public void setAuthor(String author) {
                  this.author = author;
              }
          
              public int getVend_id() {
                  return vend_id;
              }
          
              public void setVend_id(int vend_id) {
                  this.vend_id = vend_id;
              }
          
              @Override
              public String toString() {
                  return "Book{" +
                          "id=" + id +
                          ", bookName='" + bookName + '\'' +
                          ", bookPrice=" + bookPrice +
                          ", bookCount=" + bookCount +
                          ", author='" + author + '\'' +
                          ", vend_id=" + vend_id +
                          '}';
              }
          }
      
      

    10.数据库预览


    360截图17290501111129147.png
CREATE TABLE `book_info` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `bookName` varchar(200) NOT NULL,
  `bookPrice` double NOT NULL,
  `bookCount` int(11) NOT NULL,
  `author` varchar(45) NOT NULL,
  `vend_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8;


11.在test文件夹下写一个test类,用于测试

package com.hwp;

import com.hwp.bean.Book;
import com.hwp.mapper.BookMapper;
import com.hwp.mapper.Impl.BookMapperImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.apache.ibatis.io.Resources;
import org.junit.Test;

import java.io.InputStream;

public class Test_mybatis0 {
    SqlSessionFactory sqlSessionFactory;
    @Before
    public   void  read() throws  Exception{
        InputStream inputStream=Resources.getResourceAsStream("Configure.xml");
        sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
    }
    @Test
    public  void test01(){
        BookMapper bookMapper=new BookMapperImpl(sqlSessionFactory);
        Book book=bookMapper.queryById(20);
        System.out.println("BOOK:"+book);
    }
}

  1. 测试成功,会有如下打印

    BOOK:Book{id=20, bookName='9书名d', bookPrice=37.0, bookCount=50, author='作者三', vend_id=1001}

你可能感兴趣的:(idea下mybatis的简单搭建和使用<转>)