Mybatis框架-01:快速入门实例(快速搭建一个Mybatis工程)

第一步:创建工程

Mybatis框架-01:快速入门实例(快速搭建一个Mybatis工程)_第1张图片

第二步:引入jar包

Mybatis框架-01:快速入门实例(快速搭建一个Mybatis工程)_第2张图片

第三步:数据库建表

Mybatis框架-01:快速入门实例(快速搭建一个Mybatis工程)_第3张图片

第四步:创建于数据库表对应的实体类

@Setter@Getter
public class Customer {
    private Integer cust_id;
    private String cust_name;
    private String cust_profession;
    private String cust_phone;
    private String email;

    @Override
    public String toString() {
        return "Customer{" +
                "cust_id=" + cust_id +
                ", cust_name='" + cust_name + '\'' +
                ", cust_profession='" + cust_profession + '\'' +
                ", cust_phone='" + cust_phone + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

第五步:创建核心配置文件SqlMappingConfig.xml




    
    
        
            
            
            
            
                
                
                
                
            
        
    


   

第六步:创建类的映射文件Customer.xml




    
    

 

第七步:将创建的映射文件引入核心配置文件中


    
        
    

第八步:编写测试类测试工程

public class TestOne {
    @Test
    public void test1() throws IOException {
        //创建SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        //加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMappingConfig.xml");
        //创建SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = factoryBuilder.build(inputStream);
        //创建session对象
        SqlSession session = sqlSessionFactory.openSession();
        //执行sqlsession对象执行查询
        //第一个参数是customer.xml的statement的id
        //第二额参数是执行sql需要的参数
        Customer customer = session.selectOne("queryCustomerById", 1);

        System.out.println(customer);

        session.close();

    }
}

 

你可能感兴趣的:(Mybatis,mybatis,数据库)