InteliJ IDEA用Maven搭建Mybatis框架

pom.xml



    4.0.0

    MavenMybatis
    MavenMybatis
    1.0-SNAPSHOT
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.5
            
        
    
    
    
        cglib
        cglib
        3.2.5
    
    
        commons-logging
        commons-logging
        1.2
    
    
        log4j
        log4j
        1.2.17
    
    
        ognl
        ognl
        3.1.15
    
    
        org.apache.logging.log4j
        log4j-core
        2.3
    
    
        org.javassist
        javassist
        3.22.0-CR2
    
    
        org.slf4j
        slf4j-api
        1.7.25
    
    
        org.slf4j
        slf4j-log4j12
        1.7.25
    
    
        org.mybatis
        mybatis
        3.4.5
    
    
        mysql
        mysql-connector-java
        8.0.7-dmr
    
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.5
        
        
            org.mybatis.generator
            mybatis-generator-maven-plugin
            1.3.5
        

    

Configuration.xml





    
        
        
        
        
        
        
        
        
    
    
        
    
    
    
        
            
            
                
                
                
                
            
        
    
    
        
    


Student.java
package com.limbo.models;

public class Student {
    private int id;
    private String name;
    private String sex;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}


Test.java
package com.limbo;

import com.limbo.models.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;

public class Test {
    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            Reader reader = Resources.getResourceAsReader("Configuration.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getSession() {
        return sqlSessionFactory;
    }

    public static void main(String[] args) {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            Student student = session.selectOne("com.limbo.models.StudentMapper.GetStudentByID", 1);
            if (student != null) {
                String stuInfo = "名字:" + student.getName() + ", 性别:" + student.getSex() + ", 年龄:" + student.getAge();
                System.out.println(stuInfo);
            }
        }
    }
}


Student.xml




    



数据库创建语句

-- auto-generated definition
CREATE TABLE student
(
  id   INT AUTO_INCREMENT
    PRIMARY KEY,
  name VARCHAR(10) NOT NULL,
  sex  CHAR        NOT NULL,
  age  INT         NOT NULL
);



参考文章地址http://www.yiibai.com/mybatis/install_configure.html

你可能感兴趣的:(Mybatis,mybatis,maven,框架,IntelliJ,IDEA,java)