栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mybatis的环境配置与简单使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mybatis的环境配置与简单使用

环境配置

参考官网:入门_MyBatis中文网

  1. 创建一个空的maven项目

  2. 在pom.xml文件添加mybatis,mysql,junit等依赖

    		
            
                org.mybatis
                mybatis
                3.5.5
            
    
            
                mysql
                mysql-connector-java
                5.1.32
            
    
            
                junit
                junit
                4.13
                test
            
    
  3. 在IDEA中安装MybatisX插件

  4. 创建mybatis配置文件:mybatis-config.xml,并且放入resource目录

    
    
    
        
        
            
        
    
        
        
            
            
                
                
                    
                    
                    
                    
                
            
    
        
        
            
    		
    
            
            
    
        
    
    
  5. 创建pojo包,创建User类并且放入

  6. 使用mapper进行代理开发

    1.在resource文件夹内创建和pojo包前缀相同的目录,如(com.xzh.pojo包),目录为comxzh
    2.在resource文件夹创建comxzhmapper目录,创建UserMapper.xml,存放sql语句
    
        
        
            select * from tb_user;
        
    
    
    3.在java文件夹创建com.xzh.mapper包,创建同名的UserMapper接口
    public interface UserMapper {
    
        List selectAll();
    
    }
            
    4.配置好后,对应接口和sql语句位置会出现蓝红鸟对应(mybatisx插件功能),这样就配置好了
    
  7. 在测试类里面创建com.xzh.test包,创建对应测试类。

        @Test
        public void testSelectAll() throws IOException {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
            List brands = brandMapper.selectAll();
            System.out.println(brands);
            
            sqlSession.close();
        }
    
  8. 如果数据库字段和pojo里面定义类的字段不一致,需要用resultMap进行封装

    
        
        
            
            
        
        
        
            select * from tb_brand;
        
    
  9. 执行方法,测试

查询-根据id查询

sql映射文件:

    
        select * from tb_brand
        
            
                and status = #{status}
            
            
                and brand_name like #{brandName}
            
            
                and company_name like #{companyName}
            
        
    
多参数设置的方法
  1. 散装参数:需要用@Param(“SQL中的参数名称”) *使用麻烦

    List selectByCondition(@Param("status") int status, @Param("companyName") String companyName, @Param("brandName") String brandName);
    
  2. 实体类封装参数 *只需要保证SQL中的参数名和实体类属性名对应上就行

    List selectByCondition(Brand brand);
    
        @Test
        public void testSelectByCondition() throws IOException {
            int status = 1;
            String companyName = "华为";
            String brandName = "华为";
            //模糊查询需要用%
            companyName = "%" + companyName + "%";
            brandName = "%" + brandName + "%";
            Brand brand = new Brand();
            brand.setStatus(status);
            brand.setCompanyName(companyName);
            brand.setBrandName(brandName);
    
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
            List brands = brandMapper.selectByCondition(brand);
            System.out.println(brands);
    
            sqlSession.close();
        }
    
  3. map集合 *只需要保证SQL中的参数名和map集合的键对的上即可

    List selectByCondition(Map map);
    
        @Test
        public void testSelectByCondition() throws IOException {
            int status = 1;
            String companyName = "华为";
            String brandName = "华为";
            companyName = "%" + companyName + "%";
            brandName = "%" + brandName + "%";
    
            Map map = new HashMap<>();
            map.put("status", status + "");
            map.put("companyName", companyName);
            map.put("brandName", brandName);
     
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
            List brands = brandMapper.selectByCondition(map);
            System.out.println(brands);
    
            sqlSession.close();
        }
    
动态sql where关键字

以下语句如果用户只给了brandName的值 那么语句就会出错(where and brand_name like #{brandName})

    
        select * from tb_brand
        
            
                and status = #{status}
            
            
                and brand_name like #{brandName}
            
            
                and company_name like #{companyName}
            
        
    
choose关键字