mybatis環境搭建與增刪改查

開發環境

  1. IDE: Eclipse

  2. 數據庫: Mysql 5.7

  3. 數據庫連接工具:DBeaver

步驟

  1. 新建maven工程,命名為learnMybaits

  2. 在pom.xml中引入mybatis、 mysql驅動和Junit的依賴

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.auuid</groupId>
        <artifactId>mybatis</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <dependencies>
            <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.4.6</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.11</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/junit/junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>
    
  3. 新建一個Employee類,新建一張employee表

    Employee類

    package com.auuid.bean;
    
    public class Employee {
    
      private Integer id;
      private String name;
      private Integer age;
    
      public Integer getId() {
        return id;
      }
      public void setId(Integer id) {
        this.id = id;
      }
      public String getName() {
        return name;
      }
      public void setName(String name) {
        this.name = name;
      }
      public Integer getAge() {
        return age;
      }
      public void setAge(Integer age) {
        this.age = age;
      }
    }
    
    

    employee表

    CREATE TABLE employee (
      id INT PRIMARY KEY AUTO_INCREMENT,
      name VARCHAR(255),
      age SMALLINT
    )
    
  4. 新建EmployeeMapper接口
    package com.auuid.dao;
    
    import com.auuid.bean.Employee;
    
    public interface EmployeeMapper {
      public Employee getById(Integer id);
      public void save(Employee employee);
      public void update(Employee employee);
      public void deleteById(Integer id);
    }
    
  5. 新建配置文件EmployeeMapper.xml和mybatis-config.xml

    EmployeeMapper.xml

    <?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">
    <!-- namespace必須是EmployeeMapper類的全類名 -->
    <!-- select 標簽的id屬性必須是EmployeeMapper類的方法名 -->
    <mapper namespace="com.auuid.dao.EmployeeMapper">
       <!-- resultType 是返回值的全類名 -->
       <select id="getById" resultType="com.auuid.bean.Employee">
           select * from employee where id = #{id}
       </select>
       <!-- useGeneratedKeys: 使用數據庫生成的key
            keyProperty: 指定這個key賦給Employee對象的那個字段
       -->
       <insert id="save" useGeneratedKeys="true" keyProperty="id">
           insert into employee(name, age) values(#{name}, #{age})
       </insert>
       <update id="update">
           update employee set name=#{name}, age=#{age} where id=#{id}
       </update>
       <delete id="deleteById">
           delete from employee where id=#{id}
       </delete>
    </mapper>
    

    mybatis-config.xml

    <?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.cj.jdbc.Driver" />
                   <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8&useSSL=false" />
                   <property name="username" value="root" />
                   <property name="password" value="123456" />
               </dataSource>
           </environment>
       </environments>
       <mappers>
           <mapper resource="EmployeeMapper.xml" />
       </mappers>
    </configuration>
    
  6. 新建測試類EmployeeTestCase
    package com.auuid;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Random;
    import java.util.UUID;
    
    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 com.auuid.bean.Employee;
    import com.auuid.dao.EmployeeMapper;
    
    public class EmployeeTestCase {
    
      @Test
      public void save() throws IOException {
        SqlSession openSession = null;
    
        try {
          openSession = openSession();
          EmployeeMapper employeeMapper = openSession.getMapper(EmployeeMapper.class); //生產接口代理對象
          employeeMapper.save(newEmployee());
          openSession.commit();
        } finally {
          openSession.close();
        }
      }
    
      private SqlSession openSession() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); //讀取數據源
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //構建sessionFactory
        SqlSession openSession = sqlSessionFactory.openSession(); //構建session
        return openSession;
      }
    
      private Employee newEmployee() {
        Employee employee = new Employee();
        employee.setName(UUID.randomUUID().toString());
        employee.setAge(new Random().nextInt(100));
        return employee;
      }
    }
    

Git 本地命令

Git 本地命令

  1. 配置簽名
    git config --global user.email "[email protected]"
    git config --global user.name "brady"
    
  2. git init 初始化倉庫
    cd ~
    mkdir project1
    cd project1
    git init
    # Initialized empty Git repository in /home/ubuntu/project1/.git/
    
  3. git add 增加到暫存區 git commit 提交到本地庫
    touch file1.txt
    git add file1.txt
    git commit -m 'commit file1'
    
    touch file2.txt
    touch file3.txt
    git add -A # 所有文件加入暫存區
    git add -u # 除了新的文件,所有文件加入暫存區
    git commit -m 'commit file2'
    git commit --amend -m 'commit file2 and file3'
    
  4. 查看log
    git log
    git log --pretty=online
    git reflog
    
  5. 回到歷史版本
    vi file1.txt # 使用vi寫入new words
    git status # 查看狀態
    git add file1.txt # 修改后的文件加入暫存區
    git commit -m 'modified file1'#提交到本地庫
    git reflog # 查看提交歷史
    git reset --hard e6ff5fe # 移動指針到e6ff5fe
    git reset --mixed e6ff5fe # 不修改工作區
    git reset --soft e6ff5fe # 不修改工作區和暫存區
    
  6. git diff比較文件
    vi file1.txt # 使用vi寫入new words again
    git diff file1.txt # 工作區與暫存區比較
    git diff HEAD file1.txt # 工作區與本地庫最新提交比較
    git diff e6ff5fe file1.txt # 工作區與本地庫某個提交比較
    
  7. 新建分支
    git branch # 查看分支
    git branch dev # 新建分支
    git checkout dev # 切換分支
    git branch dev2
    git branch -d dev2 # 刪除分支
    git branch -D dev3 # 強制刪除分支
    
  8. 合并分支
    git checkout dev
    vi file1.txt # 使用vi寫入need to be merged to master
    git commit -a -m "finish work" # -a 自動加到暫存區
    git checkout master # 切換回master
    git merge dev # 將dev分支合并到master
    
  9. 沖突解決
    git checkout master
    vi file1.txt # 使用vi寫入master new line
    git commit -a -m "add master new line"
    git checkout dev
    vi file1.txt # 使用vi寫入dev new line
    git commit -am "add dev new line"
    git merge master # 把master分支合并到dev
    #CONFLICT (content): Merge conflict in file1.txt
    git status
    vi file1.txt # 解決沖突
    git commit -a -m "conflict resolved"
    

Linux賬號與組

存儲賬號的文件/etc/passwd

cat /etc/passwd
# root : x : 0:0 : root:/root :/bin/bash
root x 0 0 root /root /bin/bash
帳號名稱 密碼(換到的其他地方,這里用x占位) UID GID 解釋這個帳號 home目錄 默認的Shell

存儲密碼的文件/etc/shadow

cat /etc/shadow
# root:$6$zg......:17791:0:99999:7:::
root $6$zg…… 17791 0 99999 7
賬號名稱 加密密碼 密碼上次修改時間 密碼不可被更動的天數 密碼需要重新變更的天數 密碼需要變更期限前的警告天數 密碼失效日 帳號失效日期 保留

存儲組的文件/etc/group

cat /etc/group
# root:x:0:
root x 0
組名稱 密碼(換到的其他地方,這里用x占位) GID 屬于這個組的用戶

命令操作

  1. 添加用戶
    useradd brady
    cat /etc/passwd | grep brady
    # brady:x:1000:1000::/home/brady:/bin/bash
    cat /etc/group | grep brady
    # brady:x:1000:
    useradd -g root bradyRoot
    cat /etc/passwd | grep bradyRoot
    # bradyRoot:x:1001:0::/home/bradyRoot:/bin/bash
    

    可以使用 -g 參數來指定所屬的組,如果沒有指定,則新建一個和用戶名一樣的組

  2. 修改密碼
    passwd brady
    
  3. 刪除用戶
    userdel brady
    userdel -r bradyRoot
    

    可以使用 -r 參數來刪除home/bradyRoot

  4. 增加組,刪除組
    groupadd brady
    groupdel brady
    
  5. 切換用戶
    su - brady
    # Last login: Mon Sep 17 18:34:42 UTC 2018
    
  6. 切換為root權限執行
    su - brady
    sudo touch file1.txt
    ls -l file1.txt
    #-rw-r--r-- 1 root root 0 Sep 19 05:15 file1.txt
    

ubuntu 16使用阿里云鏡像安裝docker-ce

step 1: 安裝必要的一些系統工具

sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common

step 2: 安裝GPG證書

curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

Step 3: 寫入軟件源信息

sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

Step 4: 更新并安裝 Docker-CE

sudo apt-get -y update
sudo apt-get -y install docker-ce

安裝指定版本的Docker-CE:
Step 1: 查找Docker-CE的版本:

apt-cache madison docker-ce
docker-ce | 17.03.1~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
docker-ce | 17.03.0~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages

Step 2: 安裝指定版本的Docker-CE: (VERSION 例如上面的 17.03.1~ce~-0ubuntu-xenial)

sudo apt-get -y install docker-ce=[VERSION]