鍍金池/ 問(wèn)答/ 數(shù)據(jù)庫(kù)問(wèn)答

vagrant或MAMP了解一下...

青黛色 回答

可以把它簡(jiǎn)單理解為操作數(shù)據(jù)庫(kù)的工具,把數(shù)據(jù)庫(kù)查詢操作的邏輯,映射為函數(shù)方法。
例如SQL的where 映射為where()函數(shù)。

我昨天開源了的composer組件 https://github.com/AxiosCros/...
用它的Db類可以進(jìn)行數(shù)據(jù)庫(kù)的連接、查詢等操作。

DBA的作用是設(shè)計(jì)數(shù)據(jù)庫(kù)結(jié)構(gòu),管理維護(hù)因?yàn)闃I(yè)務(wù)變化而帶來(lái)的數(shù)據(jù)結(jié)構(gòu)變化,
同時(shí)更重要的任務(wù)是保證數(shù)據(jù)庫(kù)運(yùn)行的穩(wěn)定安全。

而ORM的作用,是讓編程語(yǔ)言以更友好的方式與數(shù)據(jù)庫(kù)進(jìn)行“溝通”。通過(guò)封裝的方法,讓開發(fā)者輕易的實(shí)現(xiàn)數(shù)據(jù)庫(kù)的查詢,而不需要直接寫SQL語(yǔ)句。

愚念 回答

1,如果程序不存在分布式,那在程序?qū)用?,直接加鎖,做成單線程,不需要做數(shù)據(jù)庫(kù)表鎖。
2,如果程序是分布式,可以放在緩存里(redis等),然后對(duì)存取緩存的方法加鎖。定時(shí)對(duì)緩存的數(shù)據(jù)同步到數(shù)據(jù)庫(kù)即可。
3,如果非要在mybatis里面做,建議分兩部分語(yǔ)句進(jìn)行調(diào)用。

a:select value from yh for update;
b:update yh set value = value + 1;
離人歸 回答
  1. F12瀏覽器調(diào)試模式,查看靜態(tài)資源時(shí)長(zhǎng)
  2. 開啟APP_DEBUG,開啟頁(yè)面Trace
  3. 查看其中SQL,在終端執(zhí)行SQL
瞄小懶 回答

mybatis 的話這個(gè)可以實(shí)現(xiàn)的, 我之前是寫過(guò)一個(gè)類似的

表結(jié)構(gòu):

CREATE TABLE `admin_menu` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id',
  `name` varchar(64) NOT NULL COMMENT '菜單名',
  `parent_id` bigint(3) NOT NULL DEFAULT 0 COMMENT '父菜單的id, 如果是父菜單這個(gè)值為0',
  `url` varchar(500) NOT NULL DEFAULT '' COMMENT '菜單的鏈接',
  `icon` varchar(100) NOT NULL DEFAULT '' COMMENT '圖標(biāo)',
  `menu_index` bigint(3) NOT NULL DEFAULT 0 COMMENT '展示的順序',
  `create_time` datetime NOT NULL COMMENT '創(chuàng)建時(shí)間',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新時(shí)間',
  PRIMARY KEY (`id`),
  KEY `uq_id` (`id`),
  KEY `uq_parent_id` (`parent_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 COMMENT='管理后臺(tái)的菜單';

其中parentId 跟你的typeParent類似, 記錄上一級(jí)的id


AdminMenu (Model):

public class AdminMenu implements Serializable {

    private static final long serialVersionUID = -6535315608269812875L;
    private int id;
    private String name;
    private int parentId;
    private String url;
    private String icon;
    private int menuIndex;
    private Date createTime;
    private Date updateTime;
    private List<AdminMenu> subMenus;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getParentId() {
        return parentId;
    }

    public void setParentId(int parentId) {
        this.parentId = parentId;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public int getMenuIndex() {
        return menuIndex;
    }

    public void setMenuIndex(int menuIndex) {
        this.menuIndex = menuIndex;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public List<AdminMenu> getSubMenus() {
        return subMenus;
    }

    public void setSubMenus(List<AdminMenu> subMenus) {
        this.subMenus = subMenus;
    }

    @Override
    public String toString() {
        return JsonUtil.toJson(this);
    }
}

Model的屬性跟表結(jié)構(gòu)一一對(duì)應(yīng), 最下面多了一個(gè)subMenu, 里面就是AdminMenu


下面是admin_menu.xml中的內(nèi)容

查詢SQL:

<select id="selectAllMenus" resultMap="adminMenuResult">
    SELECT
        id, name, parent_id, url, icon, menu_index, create_time, update_time
    FROM
      admin_menu
    WHERE parent_id=0
    ORDER BY menu_index
</select>

這里返回的就是adminMenuResult結(jié)果集:

<resultMap id="adminMenuResult" type="biz.menzil.admin.core.model.AdminMenu">
    <id column="id" property="id"/>
    <result column="name" property="name"/>
    <result column="parent_id" property="parentId"/>
    <result column="url" property="url"/>
    <result column="icon" property="icon"/>
    <result column="menu_index" property="menuIndex"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <association property="subMenus" column="id" select="selectSubMenus"/>
</resultMap>

其中這一行是最重要的

 <association property="subMenus" column="id" select="selectSubMenus"/>

這里用selectSubMenus來(lái)進(jìn)行了另一個(gè)查詢, 查詢的參數(shù)為id, 把查詢出來(lái)的結(jié)果放在Model中的subMenus屬性中.

selectSubMenus查詢SQL:

<select id="selectSubMenus" parameterType="long" resultMap="adminSubMenuResult">
    select
      id, name, parent_id, url, icon, menu_index, create_time, update_time
    from admin_menu
    where parent_id = #{id}
    order by menu_index
</select>

這里就是用第一層的id來(lái)查詢有沒有子菜單. 這里的#{id}就是上面那個(gè)結(jié)果集的column參數(shù).
因?yàn)槲抑挥袃蓪硬藛? 所以這里用了一個(gè)新的結(jié)果集,跟上面的區(qū)別就是沒有subMenus字段.

adminSubMenuResult:

<resultMap id="adminSubMenuResult" type="biz.menzil.admin.core.model.AdminMenu">
    <id column="id" property="id"/>
    <result column="name" property="name"/>
    <result column="parent_id" property="parentId"/>
    <result column="url" property="url"/>
    <result column="icon" property="icon"/>
    <result column="menu_index" property="menuIndex"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
</resultMap>

如果你有三,四級(jí)的話你可以一個(gè)結(jié)果集. (第一層查詢的時(shí)候用id去查詢第二層, 第二層查詢的時(shí)候用第二層的id去查詢第三層...)


下面我貼一下整個(gè)的admin_menu.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">

<mapper namespace="biz.menzil.admin.core.dao.AdminMenuDao">

    <resultMap id="adminMenuResult" type="biz.menzil.admin.core.model.AdminMenu">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="parent_id" property="parentId"/>
        <result column="url" property="url"/>
        <result column="icon" property="icon"/>
        <result column="menu_index" property="menuIndex"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <association property="subMenus" column="id" select="selectSubMenus"/>
    </resultMap>

    <resultMap id="adminSubMenuResult" type="biz.menzil.admin.core.model.AdminMenu">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="parent_id" property="parentId"/>
        <result column="url" property="url"/>
        <result column="icon" property="icon"/>
        <result column="menu_index" property="menuIndex"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <insert id="insertAdminMenu">
        INSERT INTO admin_menu(name, parent_id, url, icon, menu_index, create_time)
        VALUES (
        #{menu.name},
        #{menu.parentId},
        #{menu.url},
        #{menu.icon},
        #{menu.menuIndex},
        NOW()
        )
    </insert>

    <select id="selectById" resultMap="adminMenuResult">
        SELECT
            id, name, parent_id, url, icon, menu_index, create_time, update_time
        FROM
          admin_menu
        WHERE id = #{id}
    </select>

    <select id="selectAllMenus" resultMap="adminMenuResult">
        SELECT
            id, name, parent_id, url, icon, menu_index, create_time, update_time
        FROM
          admin_menu
        WHERE parent_id=0
        ORDER BY menu_index
    </select>

    <select id="selectSubMenus" parameterType="long" resultMap="adminSubMenuResult">
        select
          id, name, parent_id, url, icon, menu_index, create_time, update_time
        from admin_menu
        where parent_id = #{id}
        order by menu_index
    </select>

    <delete id="deleteAdminMenu">
        DELETE FROM
        admin_menu
        WHERE id=#{id}
    </delete>

    <update id="updateAdminMenu" >
        UPDATE admin_menu
        <set>
            <if test="menu.name != null and menu.name != ''">
                name=#{menu.name},
            </if>
            <if test="menu.parentId >= 0">
                parent_id=#{menu.parentId},
            </if>
            <if test="menu.url != null and menu.url != ''">
                url=#{menu.url},
            </if>
            <if test="menu.icon != null and menu.icon != ''">
                icon=#{menu.icon},
            </if>
            <if test="menu.menuIndex > 0">
                menu_index=#{menu.menuIndex},
            </if>
        </set>
        WHERE id=#{menu.id}
    </update>

</mapper>
#確保你的teacher_class表有unique index(teacherId, classId)
SELECT teacherId,COUNT(1) FROM teacher_class 
WHERE classId IN(1,2,3)
GROUP BY teacherId
HAVING COUNT(1)=3
  1. 首先確認(rèn)datadir位置,ibdata1默認(rèn)寫在datadir目錄下
  2. 刪除datadir,然后重新初始化mysql

/usr/sbin/mysqld --initialize-insecure --user=mysql --datadir=/data/mysql --sock=/var/lib/mysql/mysql.sock

短嘆 回答

從領(lǐng)域模型來(lái)看,余額不是用戶自身的屬性,【用戶】依賴【余額】,【余額】關(guān)聯(lián)【用戶】,所以分開存儲(chǔ)更合理。如果因?yàn)橛囝~的變更而引起用戶信息改變,或刪除用戶后造成用戶余額不可訪問(wèn),這個(gè)聽起來(lái)是有問(wèn)題的。況且用戶和余額可能分別有各自的狀態(tài)。

網(wǎng)妓 回答

買一本mongo的書。 然后配合mongo官網(wǎng)上的資料看。 然后在實(shí)際使用中,不斷的給自己提一些問(wèn)題,再去找解決方案。

clipboard.png

入她眼 回答

已解決.的確是線程的問(wèn)題.需要在controller層加上synchronized.

情已空 回答

如果是修改一個(gè)表的字符集的話,用這個(gè)去試試
alter table books convert to character set utf8mb4_unicode_ci;

瘋浪 回答

MySQL有個(gè)特殊的語(yǔ)法 INSERT ... ON DUPLICATE KEY UPDATE 應(yīng)該是最高效的了。
參考官方文檔。

另外,你的記錄是多條,要啟動(dòng)事務(wù),在一個(gè)事務(wù)里更新多條,比一次更新用一個(gè)事務(wù)要高效的多。

心悲涼 回答

tp 是單文件入口 通過(guò)路由分解URL 來(lái)跳轉(zhuǎn)到對(duì)應(yīng)的控制器

兔寶寶 回答

你可以使用 ORM的對(duì)象操作數(shù)據(jù)庫(kù),你也可以寫DQL操作數(shù)據(jù),你可以寫 SQL來(lái)操作數(shù)據(jù)庫(kù)。

ORM 無(wú)法涵蓋所有的SQL標(biāo)準(zhǔn),直接寫SQL又可能會(huì)有風(fēng)險(xiǎn)。就出了DQL,語(yǔ)法還和sql很像

命多硬 回答

如果你這么寫條件:WHERE user_name > 'haha' AND age = 50 AND gender = 1,那么索引就應(yīng)該建為:(age, gender, user_name)。

原因很簡(jiǎn)單:復(fù)合索引的第一個(gè)列就用大于或小于,后面的列就廢掉了。

哚蕾咪 回答

where `type`=101 or (`type`=102 and `value` like'3301%')
是這樣嗎?沒太讀懂問(wèn)題。

朕略萌 回答

沒看懂PageTitle和添加商品之間的關(guān)系啊。

如果這倆是并列的結(jié)構(gòu),那就直接寫<PageTitle title="商品列表" />就可以,不用單獨(dú)寫閉合標(biāo)簽。

如果是包含關(guān)系,那就在PageTitlereturn里面對(duì)應(yīng)的地方寫{this.props.children}

類似這樣,this.props.children就是你用PageTitle包起來(lái)的內(nèi)容

// PageTitle
render(){
    return(
        <section >
            <h1></h1>
            <ol></ol>
            {this.props.children}
        <section />
    )
}
冷咖啡 回答

其實(shí)這類問(wèn)題我比較好奇的是什么應(yīng)用場(chǎng)景必須要此類功能?還是僅僅是純技術(shù)探討?如果是解決實(shí)際業(yè)務(wù)場(chǎng)景,為何不是select出來(lái)后在內(nèi)存里通過(guò)代碼邏輯實(shí)現(xiàn)后面的功能?