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(); } }
JOOQ 实战(二)
最新推荐文章于 2024-08-17 11:31:22 发布