PrivateWen


  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

计算机操作系统

发表于 2021-03-16 | 更新于: 2023-02-06

学习方法

  • 教学资源
  1. 网易云课堂—哈工大李志军老师的os。配套的实验环境
  2. 学堂在线清华OS课程练习题
  3. 国内的xv6 lab-清华大学ucore OS 链接
  • 书籍
  1. <操作系统真相还原>

疑难问题

  1. 进程互斥与同步的问题
    • 读者-写者问题:可以多个进程同时读文件,但只能一个进程写文件。并且读文件的操作不会改变资源的值。
    • 火车进站出站问题:可以多个火车同向运行,但是不允许火车相向运行。

      两个问题的区别在于写文件只能允许一个进程,而火车不管是同向还是相向,都可以多个进程同时运行.

  2. 读者写者问题和多人过桥问题
    对比读者写者问题,尽管可能同时运行在wait(s),但由于两者本来就互斥,故有以下逻辑
    1
    2
    if(count1==1)
    wait(s)
    1
    2
    if(count==1)
    wait(s)

图片

实验

  1. GCC编译器
  2. GDB调试器

MyBatis

发表于 2021-03-14 | 更新于: 2023-02-06

MyBatis的作用

可以简化JDBC操作,实现数据的持久化

  • ORM: Object Relational Mapping
  • MyBatis: 是ORM的一个实现(与之相同的还有Hibernate)
  • ORM使得开发人员像操作对象一样操作数据库。

    MyBatis程序的步骤

  • 引入mybatis.jar 和 jdbc.jar
  • 配置: conf.xml文件配置数据库的信息和需要加载的映射文件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    <environments default="development">
    <!-- 开发环境(自己的计算机) -->
    <environment id="development">
    <transactionManager type="JDBC"/>
    <dataSource type="POOLED">
    <property name="driver" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/football?serverTimezone=UTC"/>
    <property name="username" value="root"/>
    <property name="password" value="MyNewPass"/>
    </dataSource>
    </environment>
    </environments>
    <mappers>
    <!-- 放映射文件 -->
    <mapper resource="org/pvtwen/entity/personMapper.xml"/>
    </mappers>
    </configuration>
  • 映射文件 xxMapper.xml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="org.pvtwen.entity.personMapper">
    <select id="queryPersonById" resultType="org.pvtwen.entity.Person" parameterType="int">
    select * from Person where id = #{id}
    </select>
    </mapper>
  • 测试类:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Reader reader = Resources.getResourceAsReader("conf.xml");
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);

    SqlSession session = sessionFactory.openSession();
    String statement = "org.pvtwen.entity.personMapper.queryPersonById";

    //MyBatis底层通过动态代理将Object对象变成一个Person对象
    Person person = session.selectOne(statement, 1);
    session.commit();
    session.close();

    注意: 因为transaction的代理模式为JDBC,所以在执行完毕后应该commit和close.

    conf.xml文件中各种参数

  • dataSource数据源类型 :
  • POOLED:使用数据库连接池
  • UNPOOLED:传统的JDBC模式
  • JNDI:从tomcat中获取一个内置的数据库连接池(数据库连接池-数据源)
  • transactionManager事务提交的方式
  • type=”JDBC”: 利用JDBC方式处理事务(commit rollback close)
  • type=”MANAGED”: 将事务交给其他组件去托管(Spring, jobss)

xxMapper.xml文件中各种参数

  • Namespace: 该mapper.xml映射文件的唯一标识符
  • 增删改查等标签当中 各参数的含义:
    parameterType: 输入参数的类型
    resultType: 查询返回结果的类型
  • 如果输入参数: 是简单类型 (8个基本类型+String) 可以使用任何占位符, #{xx}
  • 如果是对象类型(例如Student),则必须是对象的属性#{属性名}

多个参数的SQL语句

1
2
3
<insert id=”addStudent” parameter=”org.pvtwen.entity.Student”>
Insert into student(stuno,sname,sage) values(#{stuNo},#{stuName},#{})
</insert>

mapper动态代理方式的crud(也叫mybatis开发)
原则: 约定优于配置

  • 动态代理的几种配置方式
  1. 配置方式(abc.xml)
    1
    <name>myProject</name>
  2. 硬编码方式(abc.java)
    1
    2
    3
    Configuration conf=new Configuration();
    Con.setName(“myProject”);

  3. 约定方式(默认值就是myProject)
    具体实现步骤
    一:基础环境:mybatis.jar/connector.jar、conf.xml、mapper.xml
    二:不同之处:约定的目标:省略掉statement,根据定位,可以直接定位sql语句
    三:定义接口:
    (a) 方法名和mapper.xml中sql标签的id值相同
    (b) 方法的输入参数和mapper.xml文件中标签的parameterType类型一致
    (c) 方法的返回值和mapper.xml文件中标签的resultType类型一致
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <mapper namespace="org.pvtwen.mapper.StudentMapper">
    <select id="queryPersonById" resultType="org.pvtwen.entity.Person" parameterType="int">
    select * from Person where id = #{id}
    </select>

    <update id="updatePersonById" parameterType="org.pvtwen.entity.Person">
    update Person set pname=#{pname},age=#{age} where id=#{id}
    </update>

    <insert id="addPerson" parameterType="org.pvtwen.entity.Person">
    insert into Person values(#{id},#{pname},#{age})
    </insert>

    <select id="queryAllPerson" resultType="org.pvtwen.entity.Person">
    select * from Person
    </select>
    </mapper>
    1
    2
    3
    4
    5
    6
    7
    	public interface StudentMapper {
    Person queryPersonById(int id);
    List<Person> queryAllPerson();
    void updatePersonById(Person person);
    void addPerson(Person person);
    }

    (d) Mapper.xml文件中的namespace的值,就是接口的全类名(接口-mapper)
    一般将mapper配置文件和接口放在同一个包中

优化配置

  • 步骤
  1. 创建属性文件:db.properties,用于保存数据库的信息

mybatis的全局参数

conf.xml

1
2
3
4
5
6
7
8
9
10
<settings>
<!-- 开启日志,并指定使用的具体日志 -->
<setting name="logImpl" value="LOG4J"></setting>
<!-- 开启延迟加载 -->
<setting name="lazyLoadingEnabled" value="true"></setting>
<!-- 关闭立即加载 -->
<setting name="aggressiveLazyLoading" value="false"></setting>
<!-- 开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings>

阅读全文 »
<1…12131415>

29 日志
2 分类
11 标签
GitHub Gitee LeetCode
© 2025 Pvtwen
由 Hexo 强力驱动
|
主题 — NexT.Pisces v5.1.3