什么是DAO?
可能很多同学都听说过DAO,有些人念D、A、O,有些人念'到'。跟上课点名一样吧。
DAO原单词: Database access object 翻译过来大概是数据访问对象的意思。
可以理解为访问数据库的接口。
创建各个数据表的dao
我们前面添加的依赖是spring的jpa,但是同学们在学校里学的应该是myBatis,SSM嘛,springmvc、spring、mybatis。我们这里使用spring的jpa
一般来说,我们为每天个表创建一个DAO类,我们要操作哪张表的时候,直接通过对应的dao操作即可。
比如说,我们User表
UserDao
整一个UserDao接口
package net.sunofbeaches.dao;
import net.sunofbeaches.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface UserDao extends JpaSpecificationExecutor<User>, JpaRepository<User, String> {
}
继承JpaSpecificationExecutor,我们可以看看里面有什么方法
/**
* Interface to allow execution of {@link Specification}s based on the JPA criteria API.
*
* @author Oliver Gierke
* @author Christoph Strobl
*/
public interface JpaSpecificationExecutor<T> {
/**
* Returns a single entity matching the given {@link Specification} or {@link Optional#empty()} if none found.
*
* @param spec can be {@literal null}.
* @return never {@literal null}.
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one entity found.
*/
Optional<T> findOne(@Nullable Specification<T> spec);
/**
* Returns all entities matching the given {@link Specification}.
*
* @param spec can be {@literal null}.
* @return never {@literal null}.
*/
List<T> findAll(@Nullable Specification<T> spec);
/**
* Returns a {@link Page} of entities matching the given {@link Specification}.
*
* @param spec can be {@literal null}.
* @param pageable must not be {@literal null}.
* @return never {@literal null}.
*/
Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable);
/**
* Returns all entities matching the given {@link Specification} and {@link Sort}.
*
* @param spec can be {@literal null}.
* @param sort must not be {@literal null}.
* @return never {@literal null}.
*/
List<T> findAll(@Nullable Specification<T> spec, Sort sort);
/**
* Returns the number of instances that the given {@link Specification} will return.
*
* @param spec the {@link Specification} to count instances for. Can be {@literal null}.
* @return the number of instances.
*/
long count(@Nullable Specification<T> spec);
}
可以做什么事情呢?可以查询是吧!
- 可以查询单个
- 可以分页查询
- 还可以分页查询
- 可以统计个数
另外,我们也继承JpaRepository,有两个泛型,一个是T,一个是ID,T,也就是我们的对象类型,ID则是id的类型。我们的User里的id是字符串类型,所以我们这里是JpaRepository<User,String>
这个接口还继承了其他的接口,同学们可以点进去看看。主要包括一些增删该查的方法。
如果需要自定义的查询我们后面在使用的时候再去编写即可。
CategoryDao
以同样的方式,创建CategoryDao
package net.sunofbeaches.dao;
import net.sunofbeaches.pojo.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface CategoryDao extends JpaSpecificationExecutor<Category>, JpaRepository<Category, String> {
}
ArticleDao
package net.sunofbeaches.dao;
import net.sunofbeaches.pojo.Article;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface ArticleDao extends JpaSpecificationExecutor<Article>, JpaRepository<Article, String> {
}
LooperDao
package net.sunofbeaches.dao;
import net.sunofbeaches.pojo.Looper;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface LooperDao extends JpaSpecificationExecutor<Looper>, JpaRepository<Looper, String> {
}
SettingsDao
package net.sunofbeaches.dao;
import net.sunofbeaches.pojo.Settings;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface SettingsDao extends JpaSpecificationExecutor<Settings>, JpaRepository<Settings, String> {
}
KpiDao
package net.sunofbeaches.dao;
import net.sunofbeaches.pojo.KpiDaily;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface KpiDao extends JpaSpecificationExecutor<KpiDaily>, JpaRepository<KpiDaily, String> {
}
ok啦,创建完了,我们后面接下去写controller吧!