一、MyBatis核心配置文件mybatis-config.xml
1 2 45 6 167 158 9 1410 11 12 13 17 1918
二、映射文件×××Mapper.xml
1 2 45 6 10 14 18 19 21 insert into customer(username,jobs,phone)22 values(#{username},#{jobs},#{phone})23 24 2526 32 3327 select last_insert_id()28 29 insert into customer(id,username,jobs,phone)30 values(#{id},#{username},#{jobs},#{phone})3134 update customer set35 username=#{username},jobs=#{jobs},phone=#{phone}36 where id=#{id}37 38 3940 delete from customer where id=#{id}41 42
三、MyBatis工作原理
1 //1.读取配置文件 2 String resource = "mybatis-config.xml"; 3 InputStream inputStream = Resources.getResourceAsStream(resource); 4 //2.根据配置文件构建SqlSessionFactory 5 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 6 //3.通过SqlSessionFactory创建SqlSession 7 SqlSession sqlSession = sqlSessionFactory.openSession(); 8 //4.SqlSession执行映射文件中定义的SQL语句,并返回映射结果 9 //即:调用映射文件中不同的子元素id,执行相应的操作10 {11 Customer customer = sqlSession.selectOne("com.itheima.mapper"12 + “.CustomerMapper.findCustomerById”, 1); 13 } 14 //5.关闭SqlSession15 sqlSession.close();
log4j.properties日志文件:
log4j.rootLogger=ERROR, stdoutlog4j.logger.com.wxy=DEBUGlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n