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

Mybatis的入门(idea)

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

Mybatis的入门(idea)

Mybatis的配置
  • 一、Mybatis入门(在maven中操作)
  • 二、乱乱的整理知识点
  • 三、JUnit单元测试


一、Mybatis入门(在maven中操作)

1.创建数据库表

2.导入依赖

 
        
            org.mybatis
            mybatis
            3.5.5
        

        
        
            mysql
            mysql-connector-java
            5.1.46
        

3.导入mybatis 核心配置文件 – > (配置数据库连接信息)




    
    
        
    
    
    
        
            
            
                
                
                
                
            
        
    
    
    
        
        
    

4.编写mapper.xml 配置文件 —>(编写sql脚本)







    
    
        
        
        
        
        
        
    
    
        update tb_brand set status = #{status} where id=#{id}
    
     
    
    
    
        select * from tb_brand
    


mapper的接口类

package com.itheima.mapper;

import com.itheima.pojo.Brand;
import org.apache.ibatis.annotations.Param;

import java.util.List;


public interface BrandMapper {
    
    List findBrand();

    
    int updateBrandById(@Param("id") int id,@Param("status") int status);
}

使用单元测试进行测试
在使用单元测试之前要先在pom.xml中引入junit的坐标依赖

  
        
            junit
            junit
            4.13
            test
        
package com.itheima;

import com.itheima.mapper.BrandMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Brand;
import com.itheima.pojo.User;
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 org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class mapperTest {
    @Test
    public  void testMapper() throws Exception {
        InputStream resource = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);

        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper  mapper = sqlSession.getMapper(BrandMapper.class);
        List brand = mapper.findBrand();
        for (Brand brand1 : brand) {
            System.out.println(brand1);
        }
        int i = mapper.updateBrandById(2,1);
        System.out.println(i);
        sqlSession.commit();
        //5. 释放资源
        sqlSession.close();
        
    }
}

结果:

二、乱乱的整理知识点

1、pom.xml配置文件要导入相应的坐标索引
2、mybatis-config.xml配置文件中的标签是设置的是所要扫描的mapper.xml文件(该文件是存在于resources目录下)
3、想要将mapper.xml文件与mapper接口映射成功,需要在resources目录下建立与mapper接口相应的目录层级,在resources目录下建立目录层级,要用/隔开,不能使用 . 如:com/textcast/mapper
4、在mapper接口与mapper.xml映射成功之后,会在target目录层级中二个出现在一起,如:

5、在pom.xml文件中dependency标签中的scope标签代表坐标的依赖范围
如Junit坐标依赖

 
            junit
            junit
            4.13
            test
        
依赖范围编译classpath测试classpath运行classpath例子
compileYYYlogback
test-Y-Junit
providedYY-servlet-api
runtime-YYjdbc驱动
systemYY-存储在本地的jar包

6、使用mybatis代理的要求
定义与SQL映射文件同名的Mapper接口,并且将Mapper接口和SQL映射文件放置在同一目录下
设置SQL映射文件的namespace属性为Mapper接口全限定名
在 Mapper 接口中定义方法,方法名就是SQL映射文件中sql语句的id,并保持参数类型和返回值类型一致

7、可以在mybatis-config 中设置别名,注意标签要放在environments标签的上面,位置放不对会报错



        
 
三、JUnit单元测试

1、在pom.xml中导入坐标索引


        
            junit
            junit
            4.13
            test
        

2、JUnit的三个注释

  • @Test
    只能放在无参数、无返回值、非静态的方法上
  • @Before
    在执行被@Test注释的方法之前执行
  • @After
    在执行完@Test注释的方法之后执行
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/867489.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号