IDEA使用MyBatis【超级详细,绝对能运行】

先上成功运行结果:

1.新建项目 ,这里新建一个普通的JavaSE项目,当然你也可以新建web项目

IDEA使用MyBatis【超级详细,绝对能运行】_第1张图片

IDEA使用MyBatis【超级详细,绝对能运行】_第2张图片

 

IDEA使用MyBatis【超级详细,绝对能运行】_第3张图片

 然后点Next,然后再点Finish就好了。

2.创建resources文件夹,该文件夹和java文件夹同一级

IDEA使用MyBatis【超级详细,绝对能运行】_第4张图片

创建完以后,在文件夹上点右键:

IDEA使用MyBatis【超级详细,绝对能运行】_第5张图片

3.在Java文件夹中新建学生实体类,对应数据库中的学生表:

package com.leo;

import lombok.Data;

@Data
public class Student {
    /**
     *
     */
    private Integer id;

    /**
     *
     */
    private String name;

    /**
     *
     */
    private String sex;

    /**
     *
     */
    private String specialty;

    /**
     *
     */
    private String grade;
}

然后在类名上按Alt+Insert,会出来这个,没有的话应该是没装插件,先去装mybatis的插件:

IDEA使用MyBatis【超级详细,绝对能运行】_第6张图片

IDEA使用MyBatis【超级详细,绝对能运行】_第7张图片

然后在studentmapper.xml 中写sql语句:




    
    
        
        
        
        
        
    

    
    
        id,
        `name`,
        sex,
        specialty,
        grade
    

    
    
        INSERT INTO student (
            id,
            `name`,
            sex,
            specialty,
            grade
        ) VALUES (
            #{student.id,jdbcType=INTEGER},
            #{student.name,jdbcType=VARCHAR},
            #{student.sex,jdbcType=VARCHAR},
            #{student.specialty,jdbcType=VARCHAR},
            #{student.grade,jdbcType=VARCHAR}
        )
    

    
    
        INSERT INTO student
        
            id,
            `name`,
            sex,
            specialty,
            grade,
        
        VALUES
        
            #{student.id,jdbcType=INTEGER},
            
            #{student.name,jdbcType=VARCHAR},
            
            #{student.sex,jdbcType=VARCHAR},
            
            #{student.specialty,jdbcType=VARCHAR},
            
            #{student.grade,jdbcType=VARCHAR},
            
        
    

    
    
        INSERT INTO student (
        id,
        `name`,
        sex,
        specialty,
        grade
        )VALUES
        
            (
            #{student.id,jdbcType=INTEGER},
            #{student.name,jdbcType=VARCHAR},
            #{student.sex,jdbcType=VARCHAR},
            #{student.specialty,jdbcType=VARCHAR},
            #{student.grade,jdbcType=VARCHAR}
            )
        
    

    
    
        UPDATE student
        
            `name`= #{student.name,jdbcType=VARCHAR},
            sex= #{student.sex,jdbcType=VARCHAR},
            specialty= #{student.specialty,jdbcType=VARCHAR},
            grade= #{student.grade,jdbcType=VARCHAR}
        
        WHERE id = #{student.id,jdbcType=INTEGER}
    


    




然后新建xml文件,命名为config.xml,这是mybatis的配置文件:

 





    
        
        
        
        
        
        
        
        
    

    
    
    
        
            
            
                
                
                
                
            
        
    


    
    
        
    

最后写一个含有主函数的类测试一下:

public static void main(String[] args) {
        // 根据 config.xml 配置的信息得到 sqlSessionFactory
        String resource = "config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 然后根据 sqlSessionFactory 得到 session
        SqlSession session = sqlSessionFactory.openSession();
        // 最后通过 session 的 selectList() 方法调用 sql 语句 listStudent
        Student student = session.selectOne("selectOne", 6);
        System.out.println(student.toString());
    }

 

 

你可能感兴趣的:(IDE)