Mybatis学习笔记

Mybatis学习笔记,第1张

1.初步搭建Mybatis

1.创建maven工程

 2.引入依赖


    
    
        org.mybatis
        mybatis
        3.5.7
    
    
    
        junit
        junit
        4.12
        test
    
    
    
        mysql
        mysql-connector-java
        5.1.3
    

3.创建Mybatis核心配置文件mybatis-config.xml




    
    
    
    
        
            
            
            
            
        
    
    
	
    
    	
    

4.创建mapper接口

public interface CustomerMapper {
  
    List selectAll();

    int insert(Customer customer);

    Customer selectById(int id);

    int updateRelNameById(Customer customer);

    int  deleteById(int  id);

    List selectByCondition(Customer condition);
}

5.创建mybatis映射文件




    
    
    
    
        insert into db_quote.tb_customer( name, tel, address, rel_name, other)
        VALUES(#{name},#{tel},#{address},#{relName},#{other})
    
    
        update db_quote.tb_customer
        set rel_name= #{relName}
        where id= #{id}
    

    
        delete from db_quote.tb_customer where id = #{id}
    

 

6. 加入log4J日志功能

1)加入依赖



log4j
log4j
1.2.17

2) 加入 log4j 的配置文件

    
        
        
        
    
2.Mybatis的增删查改 

1.添加数据

    
        insert into db_quote.tb_customer( name, tel, address, rel_name, other)
        VALUES(#{name},#{tel},#{address},#{relName},#{other})
    

2.删除数据

 
        delete from db_quote.tb_customer where id = #{id}
    

 3.查询数据

    
    

4.修改数据

    
        update db_quote.tb_customer
        set rel_name= #{relName}
        where id= #{id}
    
3.Mybatis获取数据的方式 

1.MyBatis 获取参数值的两种方式:${}和 #{}

2.${}的本质就是字符串拼接,#{}的本质就是占位符赋值

3.${}使用字符串拼接的方式拼接 sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单引号;但是 #{}使用占位符赋值的方式拼接sql.此时为字符串类型或日期类型的字段进行赋值时,可以自动添加单引号

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/756341.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-30
下一篇 2022-04-30

发表评论

登录后才能评论

评论列表(0条)

保存