mybatis学习经验分享

第一部分:主配置文件:
 




    
    
        
        
    
    
    
        
            
            
                
                
                
                
            
        
    
    
    
        
    

第二部分:映射的配置文件。




    
   
        insert into employee(name,gender,age,phone) values (#{name},#{gender},#{age},#{phone})
   

    
    
         insert into employee(name,gender,age,phone) values (#{name},#{gender},#{age},#{phone})
         
             select last_insert_id()
         
    

    
    
        insert into employee(
        
            
                name,
            
            
                gender,
            
            
                age,
            
            
                phone,
            
        
        ) values(
            
                
                    #{name},
                
                
                    #{gender},
                
                
                    #{age},
                
                
                    #{phone},
                
            
        )
    

    
        update employee set name = #{name},age = #{age},phone=#{phone} where id = #{id}
    

    
    
        delete from employee where id in
        
            #{id}
        
    
    
    
    
        delete from employee where id in 
        
            #{id}
        
    


    
    
        update employee
        
            
                name = #{name},
            
            
                gender = #{gender},
            
            
                age = #{age},
            
            
                phone = #{phone}
            
        
            where id = #{id}
    

第三部分:执行方法介绍

package com.dl.mybatis.test;

import com.dl.mybatis.dao.EmployeeDao;
import com.dl.mybatis.entity.Employee;
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.IOException;
import java.io.InputStream;

/**
 * \* Created with IntelliJ IDEA.
 * \* @author: DL代先生
 * \* Date: 2021/1/25
 * \* Time: 17:19
 * \* WeChat: D19121630l
 * \* Company: DL19121630工作室
 * \* Description: 原创代码,如需代写,微信联系!
 * \
 */
public class Demo1 {
    public static void main(String[] args) {
        try {
            //第一步:读取配置文件
            InputStream resource = Resources.getResourceAsStream("mybatis.xml");
            //第二步:创建SqlSessionFactory对象
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resource);
            //第三步:创建SqlSession对象
            SqlSession sqlSession = factory.openSession();
            //第四步:创建DAO的代理对象
            EmployeeDao mapper = sqlSession.getMapper(EmployeeDao.class);
            //第五步:用代理对象执行对应的方法
            mapper.addEmployee(new Employee("代先生","男",20,"13682304539"));
            //第六步:提交
            sqlSession.commit();
            //第七步:关闭SqlSession对象
            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


 

你可能感兴趣的:(Java知识分享)