JOOQ 实战(二)

5 篇文章 0 订阅
package jooqtest;

import com.panda.core.db.DataHelper;
import com.panda.core.db.impl.DBService;
import org.jooq.*;
import org.jooq.impl.DSL;
import org.junit.Test;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Connection;

/**
 * Created by Lovell on 8/26/16.
 */

/*
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 @@@@  @@@@@@@@        @@  @@@@@@@  @@        @@  @@@@@@@@@  @@@@@@@@@@
 @@@@  @@@@@@@@  @@@@  @@@  @@@@@  @@@  @@@@@@@@  @@@@@@@@@  @@@@@@@@@@
 @@@@  @@@@@@@@  @@@@  @@@@  @@@  @@@@       @@@  @@@@@@@@@  @@@@@@@@@@
 @@@@  @@@@@@@@  @@@@  @@@@@  @  @@@@@  @@@@@@@@  @@@@@@@@@  @@@@@@@@@@
 @@@@        @@        @@@@@@   @@@@@@        @@        @@@        @@@@
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 */
public class JooqDao {
    private DSLContext dslContext = null;

    // 获取DSLContext对象
    private DSLContext getdslContext() throws IOException, SQLException {
        DataHelper.getInstance().start();
        Connection connection = DBService.getInstance().getConnection();
        dslContext = DSL.using(connection);
        return dslContext;
    }

    // 简单实体查询
    public void select(String add) throws IOException, SQLException {
        DSLContext dslContext = getdslContext();
        Table<Record> table = DSL.table("t_login");
        SelectQuery<Record> selectQuery = dslContext.selectQuery(table);//获取查询对象
        Condition eq = DSL.field("c_username").eq(add);//查询条件
        selectQuery.addConditions(eq);//添加查询条件
        Result<Record> fetch = selectQuery.fetch();
        for (Object aResult : fetch) {
            Record record = (Record) aResult;
            System.out.println(record);
            System.out.println(record.getValue("c_username"));
        }
    }

    // 实体更新
    public void update(String name) throws IOException, SQLException {
        DSLContext dslContext = getdslContext();
        Table<Record> table = DSL.table("t_login");
        UpdateQuery<Record> updateQuery = dslContext.updateQuery(table);//获取更新对象
        updateQuery.addValue(DSL.field("c_password"), "new_password");//更新c_password字段的值为password
        Condition eq = DSL.field("c_username").eq(name);//更新c_username为name的c_password字段
        updateQuery.addConditions(eq);
        int execute = updateQuery.execute();
        System.out.println(execute);
        select(name);
    }

    // 原生态的sql查询
    public void getVal() throws IOException, SQLException {
        DSLContext dslContext = getdslContext();
        Table<Record> table = DSL.table("t_login");//表名
        Result<Record> fetch = dslContext.select().from(table).where("c_username = 'Lovell'").and("c_id = 1").orderBy(DSL.field("c_id").asc()).fetch();
        for (Object aResult : fetch) {
            Record record = (Record) aResult;
            System.out.println(record);
        }
        /*
        Map<String, Object> fetchAnyMap = orderBy.fetchAnyMap();
        Set<String> keySet = fetchAnyMap.keySet();
        for(String s:keySet)
        {
            System.out.println("key--"+s+"--val:"+fetchAnyMap.get(s));
        }
        */
    }

    // 验证DSL.exists方法
    public void exits() throws IOException, SQLException {
        DSLContext dslContext = getdslContext();

        Condition condition = DSL.exists(DSL.select(DSL.field("c_username")));
        Table<Record> table = DSL.table("t_login");
        SelectQuery<Record> selectQuery = dslContext.selectQuery(table);
        selectQuery.addConditions(condition);
        Result<Record> fetch = selectQuery.fetch();
        for (Object aResult : fetch) {
            Record record = (Record) aResult;
            System.out.println(record);
            System.out.println(record.getValue("c_username"));
        }
    }

    @Test
    public void test1() throws Exception {
        // 查找
        JooqDao jooqDao = new JooqDao();
        jooqDao.select("Lovell");
    }

    @Test
    public void test2() throws Exception {
        // 更新
        JooqDao jooqDao = new JooqDao();
        jooqDao.update("Lovell");
    }

    @Test
    public void test3() throws Exception {
        // 验证DSL.exists方法
        JooqDao jooqDao = new JooqDao();
        jooqDao.exits();
    }

    @Test
    public void test4() throws Exception {
        // 原生sql语句查询
        JooqDao jooqDao = new JooqDao();
        jooqDao.getVal();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

VCHH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值