MyBatis入门 Day10 2018-11-28

MyBatis

  • mybatis-3.4.6
  • jdk1.8+

一、mybatis入门

1.依赖jar包

mybatis基础包
  • pom.xml引入依赖
    
        
        
            org.mybatis
            mybatis
            3.4.6
        
        
            cglib
            cglib
            3.2.9
        
        
            commons-logging
            commons-logging
            1.2
        
        
            ognl
            ognl
            3.2.8
        
        
            org.slf4j
            slf4j-log4j12
            1.7.25
        
        
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.7
        
        
            junit
            junit
            4.12
        
        
            com.oracle
            ojdbc6
            11.2.0
        
        
            org.apache.logging.log4j
            log4j-core
            2.9.1
            
        
    

2. MyBatis 配置文件

  • mybatis-config.xml




    
    
    
    
        
        
        
        
    
    
    
        
    
    
    
        
        
            
            
            
                
                
                
                
                
            
        
    
    
    
        
    

3. Mapper映射文件

  • PhoneUserMapper.xml




  
    
    
    
    
    
    
    
    
  
  
      
        select SQ_PHONE_USER_ID.nextval as id from dual
      
    insert into PHONE_USER (ID, NAME, BIRTH_DAY, 
      ID_NO, MOBILE_TEL, E_MAIL, 
      PASSWD, CREATE_DATE)
    values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{birthDay,jdbcType=TIMESTAMP}, 
      #{idNo,jdbcType=VARCHAR}, #{mobileTel,jdbcType=VARCHAR}, #{eMail,jdbcType=VARCHAR}, 
      #{passwd,jdbcType=VARCHAR}, #{createDate,jdbcType=TIMESTAMP})
  
  

4.MyBatis完成数据操作代码

public class BasicMyBaitisTest {
    public static void main(String[] args) throws IOException {
        //读取mybatis-config.xml配置文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        //初始化mybatis.创建SQLSessionFactory实例
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //创建session实例
        SqlSession session = sqlSessionFactory.openSession();
        //创建phoneUser 对象
        PhoneUser user = new PhoneUser("test", "吴总", new Date(1992, 9, 26), "54232819800130824X", "15555555858", "[email protected]", "123456", new Date());
        //插入数据 
        //session.insert("com.zhougl.web.dao.mapper.PhoneUserMapper.insertPhoneUser", user);
        PhoneUser userOne = session.selectOne("com.zhougl.web.dao.mapper.PhoneUserMapper.selectPhoneUserById", "1");
        System.out.println(userOne.toString());
        //提交事务
        session.commit();
        //关闭session
        session.close();
    }
}
//打印结果
PhoneUser [id=1, name=测试, birthDay=Tue Oct 23 00:00:00 CST 1984, idNo=220724198410236590, mobileTel=199123425678, [email protected], passwd=1, createDate=Thu Nov 22 00:00:00 CST 2018]

5. log4j日志文件配置

#全局日志配置
#log4j.rootLogger=ERROR,stdout
log4j.rootLogger=DEBUG,Console
#MyBatis日志配置
#log4j.logger.com.zhougl.mapper.UserMapper=DEBUG
#控制台输出
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

6.mybatis-generator插件使用(快速生成代码)

  • generatorConfig.xml




    
    
    
    

    
        
        
        
        
        
        
        
        
            
        
        
        
                        
        
        
            
            
                               

        
        
            
            
            
            
        

        
        
            
            
        
        
        
            
        

        
        
        

你可能感兴趣的:(MyBatis入门 Day10 2018-11-28)