Mbatis

架包和日志的配置文件不一一列举了
一.核心配置文件

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">














    
        
        
        
        
            
            
            
            
            
        
    



    
    
    
    

二 sql配置文件

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">



三.链接数据库的文件
DRIVER=com.mysql.jdbc.Driver
DBURL=jdbc:mysql://localhost:3306/demo06?useUnicode=true&characterEncoding=utf8

DBNAME=ORCL

USERNAME=root
USERPWD=940906

四.如何利用Mybatis进行数据交互
//利用mybatis的API根数据库进行交互
//加载核心配置文件,参数值是核心配置文件相对于src的路径
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
//构建连接池
SqlSessionFactory sFactory = new SqlSessionFactoryBuilder().build(reader);
//获取链接对象
SqlSession session = sFactory.openSession();
//调用mapper中的接口 mapper也就是原来的DAO
UserMapper uMapper = session.getMapper(UserMapper.class);
//调用方法
User user = uMapper.getSelectId(1);
System.out.println();
List list = uMapper.selectAll();
for (User user2 : list) {
System.out.println(user2.getPhone());
}
System.out.println(user.getPossword());

五.一对一





    
    
    
    
    
    


    
    

    
    
    
    
    
    

六.增删改查



insert into user(username,possword,email,phone) values(#{username},#{possword},#{email},#{phone})



delete from user where userid = #{userid}



    delete from user where userid in
    
    
        #{userid}
    
   


update user set username = #{username},possword = #{possword},email = #{email} where userid = #{userid} 

七.一对多





    
    
    
    
    
    
    
    

八.动态SQL







图片.png

你可能感兴趣的:(Mbatis)