搭建入门Mybatis程序


1.程序部署

  • 创建数据库和数据表

    CREATE TABLE `users` (
      `id` int NOT NULL AUTO_INCREMENT,
      `name` varchar(20) CHARACTER SET utf8 COLLATE utf8 NOT NULL,
      `pwd` int NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8
    

    注意写引擎(ENGINE)和字符集(CHARSET

    注意配置Maven的setting.xml和仓库

  • 创建一个MavenModel

    • 将其src删除,以当前Model为父工程,创建子工程,防止多次配置pom.xml

    • 在父工程pom.xml配置以下内容

      <?xml version="1.0" encoding="UTF-8"?>
      
          4.0.0
      
          
          com.dragon
          mybatisStudy
          pom
          1.0-SNAPSHOT
          
              mybatis-01
          
          
          
          
              
                  mysql
                  mysql-connector-java
                  8.0.19
              
              
              
                  org.mybatis
                  mybatis
                  3.5.9
              
              
              
                  junit
                  junit
                  4.12
              
          
          
              UTF-8
          
              
          
              
                  
                      src/main/resources
                      
                          **/*.properties
                          **/*.xml
                      
                      true
                  
                  
                      src/main/java
                      
                          **/*.properties
                          **/*.xml
                      
                      true
                  
              
          
      
      

      注意:一定要配置资源过滤,否则项目执行不成功

    • 该项目中包含三个包

      • pojo实体类

      • dao数据处理层

        1.接口

        2.userMapper.xml

      • utils工具包

        1.MybatisUtils.class

  • 链接数据库在IDEA中

  • 创建mybatis配置文件“mybatis-config.xml

    • 位于src/main/resources/

    • 配置内容如下

      <?xml version="1.0" encoding="UTF-8" ?>
      
      
          
          
              
                  
                  
                      
                      
                      
                      
                  
              
          
          
              
          
      
      

      注意:此处一定要配置mapper,找到userMapper.xml文件,否则执行不成功(userMapper.xml是实现接口的方法)

  • userMapper.xml内容如下

    <?xml version="1.0" encoding="UTF-8" ?>
    
    
    
    
        
    
    

    此处getUserList为接口中的方法名

  • MybatisUtils.class

    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;
    
    public class MybatisUtils {
        private static SqlSessionFactory sqlSessionFactory;
        static {
            //使用Mybatis第一步,获取sqlSessionFactory对象
            String resource = "mybatis-config.xml";
            InputStream inputStream = null;
            try {
                inputStream = Resources.getResourceAsStream(resource);
            } catch (IOException e) {
                e.printStackTrace();
            }
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }
        public static SqlSession getSqlSession(){
            return sqlSessionFactory.openSession();
        }
    }
    
  • Test测试类

    import org.apache.ibatis.session.SqlSession;
    import org.junit.Test;
    
    import java.util.List;
    
    public class MyBatisTest {
        @Test
        public void test(){
            //获取sqlSession对象
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            List userList = mapper.getUserList();
    
            for (User user : userList) {
                System.out.println(user);
            }
            sqlSession.close();
        }
    }
    
    

2.解决问题

2.1

  • mybatis-config连接驱动中,如果是mysql8版本以下的jar包/依赖,那么可以如下写

    
    
    
  • 相反,在mysql8以及8以上版本

    
    
    

2.2

  • 配置pom.xml一定要配置资源过滤,否则会出现找不到userMapper.xml文件。

     
         
             
                 src/main/resources
                 
                     **/*.properties
                     **/*.xml
                 
                 true
             
             
                 src/main/java
                 
                     **/*.properties
                     **/*.xml
                 
                 true
             
         
     
    
  • 在mybatis-config.xml文件中配置内容中一定要注册mapper,否则会出现“org.apache.ibatis.binding.BindingException: Type interface com.dragon.dao.UserDao is not known to the MapperRegistry.”这种错误。