【中级篇】详细讲解使用SpringBoot + Jooq生成dao的用法(三)

之前我们讲解过Jooq的结构,其中生成了dao类,dao里面生成了很多方法,下面来讲解如何使用它:

使用dao的时候依赖注入的时候发现怎么也启动不起项目后来在同事的帮助下解决了这个问题?

加入了两个注解:@Repository和@Autowired

/*
 * This file is generated by jOOQ.
 */
package com.demo.main.jooq.tables.daos;


import com.demo.main.jooq.tables.SClass;
import com.demo.main.jooq.tables.records.SClassRecord;

import java.util.List;

import javax.annotation.Generated;

import org.jooq.Configuration;
import org.jooq.impl.DAOImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


/**
 * This class is generated by jOOQ.
 */
@Generated(
    value = {
        "http://www.jooq.org",
        "jOOQ version:3.11.9"
    },
    comments = "This class is generated by jOOQ"
)
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
@Repository
public class SClassDao extends DAOImpl {

    /**
     * Create a new SClassDao without any configuration
     */
    public SClassDao() {
        super(SClass.S_CLASS, com.demo.main.jooq.tables.pojos.SClass.class);
    }

    /**
     * Create a new SClassDao with an attached configuration
     */
    @Autowired
    public SClassDao(Configuration configuration) {
        super(SClass.S_CLASS, com.demo.main.jooq.tables.pojos.SClass.class, configuration);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected Integer getId(com.demo.main.jooq.tables.pojos.SClass object) {
        return object.getId();
    }

    /**
     * Fetch records that have id IN (values)
     */
    public List fetchById(Integer... values) {
        return fetch(SClass.S_CLASS.ID, values);
    }

    /**
     * Fetch a unique record that has id = value
     */
    public com.demo.main.jooq.tables.pojos.SClass fetchOneById(Integer value) {
        return fetchOne(SClass.S_CLASS.ID, value);
    }

    /**
     * Fetch records that have classname IN (values)
     */
    public List fetchByClassname(String... values) {
        return fetch(SClass.S_CLASS.CLASSNAME, values);
    }
}

1.查询用法

	@Autowired
	private SClassDao sclassdao;
	public void query(int id) {
		sclassdao.fetchById(id);
	}

2.新增用法 

	public void insert(SClass s) {
		sclassdao.insert(s);
	}

3.删除用法

	public void del(Integer id) {
		sclassdao.deleteById(id);
	}

 4.修改用法

	public void up(SClass s) {
		sclassdao.update(s);
	}

 

你可能感兴趣的:(Jooq从入门到精通)