Jooq对数据库的增删改查教程

纯手打,转载请附上本文网址!!!

实习,用到了SpringBoot + Jooq,要用Jooq去操作数据库,突然发现这个Jooq和Heribnate、Mybatis真的还有不小的区别

附上github的地址:https://github.com/xianzhixianzhixian/JooqDemo.git

首先,给大家看官网(http://www.jooq.org/)的几个图


Jooq对数据库的增删改查教程_第1张图片

是不是很有意思?SQL语句去哪啦,都可以写成Java代码,是不是感觉有点小清新?是不是感觉很不错?接下来进入正题


首先呢要新建一个maven的Java项目,这个就不多说了,附上项目结构:

Jooq对数据库的增删改查教程_第2张图片


接下来就是代码部分了,首先我们创建一个Student.java,这是我们要操作的实体类


package com.jooq.demo.bean;

/**
 * Student的bean
 * @author yufeng 2017/12/25
 */
public class Student {

    private Long id;
    private String name;

    public Student() {
    }

    public Student(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}


然后呢,我自己写的JooqUtil.java,用于操作数据库


package com.jooq.demo.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;

import com.jooq.demo.bean.Student;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.DeleteConditionStep;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.Table;
import org.jooq.UpdateQuery;
import org.jooq.impl.DSL;

/**
 * Jooq操作数据库的工具类
 * @author yufeng 2017/12/25
 */
public class JooqUtil {

	private static String driver="com.mysql.jdbc.Driver";
	private static String url="jdbc:mysql://127.0.0.1:3306/student";
	private static String username="root";
	private static String password="123456";
	
	private static Connection ct=null;
	private static DSLContext dslContext=null;
	private static Result result=null;
	private static Table table=null;

	static {
		try {
			Class.forName(driver);
			ct=DriverManager.getConnection(url, username, password);
			dslContext=DSL.using(ct);
			table=DSL.table("student");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static int add(Student student) {
		
	    Record record= dslContext.newRecord(table, student);
	    record.into(DSL.field("id"),DSL.field("id"));

	    int execute=dslContext.insertInto(table)
	    				.set(DSL.field("id"), student.getId())
	    				.set(DSL.field("name"), student.getName())
	    				.execute();

		return execute;
	}
	
	public static int update(Student student) {
		
		UpdateQuery updateQuery = dslContext.updateQuery(table); //获取更新对象
		updateQuery.addValue(DSL.field("name"), student.getName()); //
        Condition eq = DSL.field("id").eq(student.getId()); //
        updateQuery.addConditions(eq);  
        int execute = updateQuery.execute();
        
        return execute;
	}
	
	public static int delete(Long id) {
		
		DeleteConditionStep deleteConditionStep=dslContext.deleteFrom(table).where("id="+id);
		int execute=deleteConditionStep.execute();
		
		return execute;
	}
	
	public static List selectAll(){
		
		ArrayList list=null;
//		result=dslContext.selectFrom(table).fetch();
        result=dslContext.select().from(table).fetch();
		if(result!=null) {
			list=new ArrayList();
			for(Record record : result) {

				Student temp=new Student();
				temp.setId((Long) record.getValue("id"));
				temp.setName((String) record.getValue("name"));
				list.add(temp);
			}
		}
		return list;
	}
	
	public static Student selectByid(Long id){
		
		Student student=null;
		result=dslContext.selectFrom(table).where("id="+id).fetch();

		if(result!=null) {

            Record record=result.get(0);
            student=new Student();
            student.setId((Long) record.getValue("id"));
            student.setName((String) record.getValue("name"));
		}
		return student;
	}
	
	public static void close(Connection ct,Result result) {
		try {
			if(result!=null) result.clear();
			if(ct!=null) ct.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}


最后进行测试(已在本地测试通过)


package com.jooq.demo.test;

import com.jooq.demo.bean.Student;
import com.jooq.demo.util.JooqUtil;
import org.junit.Test;

import java.util.List;

/**
 * Jooq代码结果测试
 * @author yufeng 2017/12/25
 */
public class TestJooq {

    @Test
    public void test(){
        //增加
        Student student=new Student();
        student.setName("tom");
        JooqUtil.add(student);

        //查询一个
        student=JooqUtil.selectByid(7L);
        System.out.println(student);

        //查询全部
        List list=JooqUtil.selectAll();
        for(Student temp : list){
            System.out.println(temp);
        }

        //删除
        JooqUtil.delete(6L);

        //更新
        student.setId(7L);
        student.setName("jerry");
        JooqUtil.update(student);
    }
}

Jooq对数据库的增删改查教程_第3张图片


最后推荐大家看看这篇文章,挺不错的https://www.jianshu.com/p/46164f9ba53c

你可能感兴趣的:(Java,Maven)