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

mybatis笔记【狂神说】纯代码

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

mybatis笔记【狂神说】纯代码

Mybatis(持久层)

数据持久化

持久化就是程序的数据在持久状态和瞬时状态转换的过程

持久层

完成持久化工作的代码块,层界限十分明显

1、依赖

    org.mybatis
    mybatis
    3.5.2


	mysql
    mysql-connector-java
    5.1.47



    junit
    junit
    4.12
    test

2、编写核心配置文件

​ [注]:最好不要添加中文注释

mybatis-config.xml





  
    
      
      
        
        
        
        
      
    
  

MybatisUtils

public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;
	// 在使用工具时初始化
    static {
        try {
            // 获取Mybatis获取sqlSessionFactory对象
            String resource = "mybatis-config.xml";
            InputStream resourceAsStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static SqlSession getSqlSession() {
        return sqlSessionFactory.openSession();
    }
}

XXXMapper.xml





    
        select * from mybatis.user[sql语句]
    

pom.xml


    
        
            src/main/resources
            
                ***.xml
            
            true
        
        
            src/main/java
            
                ***.xml
            
            true
        
    

3、CURD

    dao创建接口方法

    Mapper.xml写配置

    id:方法名
    parameterType:参数类型
    resultType:返回类型
    

    测试

    @Test
    public void Test(){
    	SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(接口类.class);
        int result = mapper.方法名();
        if (result > 0){
        	System.out.println("操作成功");
    	}
        // 增删改需要提交事务
        sqlSession.commit();
        sqlSession.close();
    }
    
4、Map和模糊查询 1、Map

​ 如果实体类或者数据库中的表,字段或者参数过多,可以考虑吧使用Map

5、配置解析 1、核心配置文件优化

db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/数据库名?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username=用户
password=密码

引入外部配置文件


    
	

2、类型别名(typeAliases)

    方式一:给特定实体类起别名(实体类少时)

    
        
    
    

    方式二:扫描实体类包,默认别名为类名,首字母小写

    
        
    
    

    注解

    @Alias("别名")
    public class User{}
    
3、设置(settings) 4、映射器(mappers)

    方式一:通过资源xml注册

    
        
    
    

    方式二:通过类注册(不建议)

    
        
    
    

    方式三:使用包扫描注册(需要取名为xxxMapper.java)

    
        
    
    
6、映射

​ 解决属性名和字段名不一致问题


    


    select * from mybatis.teacher where id=#{tid}

2、根据结果嵌套处理


    select t.id tid, t.name tname, s.id sid, s.name sname
    from mybatis.teacher t,
    mybatis.student s
    where t.id = s.tid



    
    
    
    
        
        
        
    

2、根据查询嵌套处理

    select * from mybatis.student where tid=#{id}

11&12 小结:
    关联 - association集合 - collectionjavaType & ofType
      javaType:用来指定实体类中属性的类型ofType:用来指定映射到List或者集合中的pojo类型,类型 泛型中的约束类型
13、动态SQL 1、if

    
        title = #{title}
    
    
        and author = #{author}
    

2、choose(when,otherwise)

​ 像switch


    
        title=#{title}
        author=#{author}
        
            views=#{views}
        
    

3、set

    update mybatis.blog
    
        title=#{title},
        author=#{author},
    
    where id=#{id}

4、trim

  ...


  ...

5、sql片段

抽取sql标签公共部分


    
        title = #{title}
    
    
        and author = #{author}
    

使用


注意事项:

最好基于单表来定义SQL片段不要存在where标签 6、foreach


14、缓存 1、一级缓存(sqlSession)

​ 缓存失效:

    查询不同的东西

    增删改操作

    查询不同的Mapper.xml

    手动清理缓存

    sqlSession.clearCache();
    

​ 小结:一级缓存默认开启,只在一次sqlSession中有效,也就是拿到连接池到关闭连接这个区间段!一次缓存就是一个Map。

2、二级缓存

    开启全局缓存

    
    

    在要使用二级缓存的mapper中开启

    
    
    
    

小结:

只要开启二级缓存,在同一个Mapper下就有效所有数据都会先放在一级缓存中只有当会话提交,才会提交到二级缓存中 3、ehcache自定义缓存

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/750353.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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