zoukankan      html  css  js  c++  java
  • 初始mybatis

    1.session.commit()能够提交事务
    2.session.close()回滚事务
    二 .模糊查询的三种方法
    1.select * from studentInfo where stuname like '%' #{stuname} '%'
    2.select * from studentInfo where stuname like concat{'%',#{stuname},'%'}
    3.select * from studentInfo where stuname like '%${value}%'
    三 多条件查询 固定的sql语句实现多条件查询的写法
    多条件查询问题 根据map 查询

    多条件查询的map方案 查询 姓名 还有 成绩的 的 同时存在的话 在dao层的实现
    public list<StudentInfo> findStudentInfoByManyCondtion(map< String object>map);
    实现类里面写的是 因为写的时候传参可以省略 所以就不写
    <select id = "findStudentInfoByManyCondtion" resultType ="StudentInfo">
    Select * from studentinfo where stuname like '%' #{stuname} '%'and stuage?#{stuage}
    </select>
    然后单侧 多条件查询map
    public void t2(){
    SqlSession session = MyBatisUtil.getSession();
    IStudentDAO mapper = session.getMapper(IStudentDAO.class);
    Map <String,Objecct> map = new Hashmap<String,Object>();
    map.put("stuname","雨);
    map.put("stuage",20)
    List<UserInfo> list= session.selectList("findAllBooks");
    System.out.println(list.toString());
    for (UserInfo info:list
    ) {
    System.out.println(info.getuName());
    }
    }


    复习下::::
    1.删除部门 注意的点 要传入一个id 要提交事务
    2.别名 在大配置里面配
    <typeAliases>
    /////////1.1.1.
    <typeAlias alias = "Dept" type = "cn.happy.entity.Dept"/>
    ///////2.2.2
    <package name="cn.happy.entil"></package>
    </typeAliases>
    可以在小配置中直接写名称 而不用写全路径
    3.工具类
    都是静态的创建方法的时候
    public class MyBatisUtil {
    //1写入大配置的文件名
    static String path = "MyBatis-Config.xml";
    //2输入流
    static InputStream is ;
    //3创建sqlSessionFactory
    static SqlSessionFactory factory ;
    static {
    try {
    is= Resources.getResourceAsStream(path);
    factory = new SqlSessionFactoryBuilder().build(is);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    public static SqlSession getSession(){
    return factory.openSession();
    }
    }
    4.
    resultmap 手工映射
    1.实体类中属性名和数据表字段不一致的问题
    2.提示 :表中的字段比较多 我要的显示的少 我用他设置我要的属性 单个映射文件中 automapping = “false”修改成功
    3.大配置中的settings节点事是对全局进行配置的 propertres和<typealiases>之间4

    5.
    模糊查询
    1.select * from studentInfo where stuname like '%' #{stuname} '%'
    2.select * from studentInfo where stuname like concat{'%',#{stuname},'%'}
    3.select * from studentInfo where stuname like '%${value}%'
    6.
    insert和delete底层都是 update

    7.opensession 到底做了什么
    1.sqlsessionfactory 是一个接口 opensession是他的方法
    2.defaultsqlSessionFactory 是 sqlsessionFactory实体类 找他的opensession方法
    3. 调度一个有三个参数的方法 事务 隔离 是否自动提交
    Environment environment = this.configuration.getEnvironment(); //大配置的根节点
    TransactionFactory transactionFactory = this.getTransactionFactoryFromEnvironment(environment);//通过环境事务工厂
    tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); //根据事务工厂创建 事务
    //opensession session事务可以commit 底层维护一个事务
    Executor executor = this.configuration.newExecutor(tx, execType);//执行器 mybatis原理图
    //大配置文件对象 打通数据库的东西
    var8 = new DefaultSqlSession(this.configuration, executor, autoCommit); //返回一个DefaultSqlSession
    DefaultSqlSession的底层的 方法 无非就是自己调用给自己创造方法
    private Configuration configuration;
    private Executor executor; //执行器
    private boolean autoCommit;// 自动提交
    private boolean dirty; //脏的 方法

    public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
    this.configuration = configuration;
    this.executor = executor;
    this.dirty = false; //方法为false 证明他是干净的 就可以认为他是和数据库中的数据是一致的
    this.autoCommit = autoCommit;
    }

    4.将配置文件中的节点变成了 程序中的的对象进行分析解析
    5.对成员变量进行初始化


    一 添加完成后返回id
    //ideatity 投影到添加到的序号
    insert into grade values("y2169");
    select @@ideatity


    8.多条件查询索引方案
    相当于占位符
    public List<StudentInfo>
    mysql select * from student where stuname like '%' #{0}'%'and stuage>#{1}
    oracle select * from student where stuname like '%'||#{0}||'%'and stuage>#{1}


    9.报错的话 no parameter 0 arg1.arg()
    解决方案是 select * from student where stuname like '%' #{arg0}'%'and stuage>#{arg1}

    10.只能标签
    1.if
    2.where
    3.choose
    4.foreach
    .数组
    .list
    .自定义list泛型
    智能if标签
    .public List<StudentInfo>findbyif(StudentInfo stu);
    智能标签choose
    .public List<StudentInfo> findbyChoose(StudenInfo stu);
    智能标签foreach array
    .public List<StudentInfo>findbycharray<int[] ids>;
    智能foreach liste<integer>
    .public list<StudentInfo>findbyforeachList(list<integer> list);
    智能标签foreach list<StudentInfo>
    .public list<StudentInfo>findbyforeachlistStudent(list < StudentInfo>list);

    11.小配置里面的写法 关于智能标签
    select * from studentinfo
    <where>
    //如果stuname不等于空的时候
    <if test = "stuname!=null">
    and stuname like '%'#{stuname} '%'
    </if>
    </where>
    choose智能标签
    //如果满足的话走的是
    <choose> 如果条件不为空的时候
    <when test = ""!=null>
    sql语句
    </when>
    //如果条件不成立的话
    <othewise>
    //
    <othewise>
    </choose>

    数组 的智能标签
    public List<StudentInfo>findbycharray<int[] ids>;
    映射文件
    select * from emp
    <where>
    empno in
    <if test = "array.length>0">
    <foreach open ="(" seperator="," item = "myid">
    #{myid}
    </foreach>
    </where>
    12.数组用length 集合用size

    13.为什么session.commit()引起事务的提交
    session java程序金额数据库交互的入口,他的提交可以让事务提交
    1.session.commit(); 是 sqlsession接口
    2.sqlsession接口的实现类是defaultSqlsession
    3.找到实现类的commit
    public void commit(){
    commit(false); //方法调用
    }
    4.调用的方法是
    public void commit(boolean fore){
    try{
    exector.commit(isCommitOrRollbackquired(fore));去执行调度commit 入参进来的是true
    dirty = false;//事务结束后恢复dirty的值
    }
    }
    5.public void commit(boolean required) throws SQLexception{required 为真
    if(required){
    thransaction.commit();//引起事务提交
    }

    }

    sql片段
    就是 在查询的时候把*全部 不用* 而提炼成一个具体的方法!
    用一个<sql id="columns">代替*的地方的 数据库的字段</sql>
    然后在sql语句写的时候就是 用一个<inclide>指令 给他写进去
    例如:::::: select <include refid = "columns"></include> from emp


    14.关于一对多的关联(获取某个制定部分下所有员工),多对一的关联(获取员工的同时获取到员工所在的部门)
    自关联:无限极菜单 多对多:中间表植入
    一对一 例子 一个公民只能有一个身份证 一个身份证只能有一个公民

    一对多的例子
    在实体类里面 建立一个实体类集合 在实体类的时候首先给new出来 以防报空指针
    使用多表联查
    select 1表名.属性,2表明.属性from 1表明,2表名 where 1表名的值关联的2表名的值
    and跟着条件
    在xml中呢 使用自己规定的映射 <resultMap></resultMap> 查询定义的实体类的集合的时候
    里面有一个
    <connection property="emps" oftype="emp">
    <id></id>
    <result></result>
    </connection>节点 就相当于一个新的resultMap 测试方法的 时候直接用foreacth直径二给便利出来

    多条sql的方案
    select * from dept where deptno=#{条件}映射到resultmap里面 然后呢 resultmap里面有一个<collection>的节点
    节点里面的属性有一个select 节点 然后在外面写入第二条sql语句 他的id跟collection里面的select的id是保持一致
    的 collection里面的 column属性写的值是跟你的第二条sql语句传入的值是一致的
    例如:
    <select id = "getDeptById" resultMape="DeptMapper">
    select * from dept where deptno=#{条件}
    </select>
    <resultMap>
    //写入第二个sql语句的关联的id column传入的参
    <collection property="emps" oftype="emp"select="getEmpsByDeptNo" column="depno">

    </collection>
    </resultMap>
    //写入的第二个sql语句
    <select id = "getEmpsByDeptNo" resultType="emp">
    select * from emp where deotno = #{depno} //与上面的传参是一样的
    </select>


    一对多是集合 多对一时关联
    如果是多对一的单挑sql例子:
    在实体类中定义一个对象 然后在sql中调用跟上面的套路都是一样的主要是在映射的时候

    这俩个名 就是他本身的
    调用的方法是 <association property="关联的部门对象别名" javaType= "实体类的名"></association>

    延迟加载:
    动态加载 MyBatis 中的延迟加载 也称为懒加载 进行关联对象进行加载 只对 关联对象有延迟加载
    主加载 不能是延迟加载
    配置延迟加载


    二级缓存的属性
    eviction:缓存中保存对象的 清理策略
    FIFO :first in first out 先进先出 队列
    数据结构
    Spring 框架
    ioc Inverse of Control 强制反转
    aop

    /* *//**
    * 序列化必须依赖于Serializable() 这个接口
    * @param args
    * @throws Exception
    *//*
    public static void main(String[] args) throws Exception {
    serializeFlyPig();
    deserializeFlyPig();
    }
    //序列话的意思就是输出流
    private static void serializeFlyPig() throws Exception {
    FlyPig flyPig = new FlyPig();
    flyPig.getAge(12);
    flyPig.getName("adas");
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("e:/wz.txt")));
    oos.writeObject(flyPig);
    System.out.println("FlyPig 对象序列化成功!");
    oos.close();
    }
    //输入流
    private static FlyPig deserializeFlyPig() throws Exception {
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("e:/wz.txt")));
    FlyPig person = (FlyPig) ois.readObject();
    System.out.println("FlyPig 对象反序列化成功!");
    return person;
    }


    public boolean getInfo() {
    return false;
    }*/




























  • 相关阅读:
    Prometheus组件
    任务和实例
    初识Prometheus
    Prometheus简介【转】
    MySQL定时备份数据库(全库备份)
    lvextend 扩容后, df -h 看到的却还是原来的大小
    修复VSAN无法看到主机磁盘
    RocketMQ 单机部署(单master模式)
    强制找回GitLab管理员账户密码的方法
    php iis 上传图片后401无法访问
  • 原文地址:https://www.cnblogs.com/BaoWangZe/p/9913109.html
Copyright © 2011-2022 走看看