Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the DAO class

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use DAO class". In daily operation, I believe many people have doubts about how to use DAO class. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use DAO class". Next, please follow the editor to study!

Dao uses SpringDataJPA technology.

Configuration file

The DAO of the system is configured in com.jspxcms.core.ContextConfig 's @ EnableJpaRepositories (basePackages = {"com.jspxcms.core.repository", "com.jspxcms.ext.repository"}, repositoryFactoryBeanClass = MyJpaRepositoryFactoryBean.class).

The DAO for this example is configured in com.jspxcms.plug.ContextConfig 's @ EnableJpaRepositories (basePackages = {"com.jspxcms.plug.repository"}, repositoryFactoryBeanClass = MyJpaRepositoryFactoryBean.class).

DAO class package com.jspxcms.plug.repository;public interface ResumeDao extends Repository, ResumeDaoPlus {public Page findAll (Specification spec, Pageable pageable); public List findAll (Specification spec, Limitable limitable); public Resume findOne (Integer id); public Resume save (Resume bean); public void delete (Resume bean); @ Modifying @ Query ("delete from Resume bean where bean.site.id in (1)") public int deleteBySiteId (Collection siteIds);}

Methods in the ResumeDao interface do not need to be implemented. The methods in the following interfaces can be placed in ResumeDao and do not need to be implemented:

Org.springframework.data.repository.CrudRepository

Org.springframework.data.repository.PagingAndSortingRepository

Org.springframework.data.jpa.repository.JpaRepository

Com.jspxcms.common.orm.MyJpaRepository

Where the public int deleteBySiteId (Collection siteIds); method is not in these interfaces, you need to use @ Query ("delete from Resume bean where bean.site.id in (? 1)") to specify the SQL statement, and since the SQL modifies the data in the database, add @ Modifying.

The dao method that needs to be implemented is put into the ResumeDaoPlus interface. The rule is to add Plus to the name of the DAO class. If the DAO class name is AbcDao, the class name of Plus is AbcDaoPlus.

Package com.jspxcms.plug.repository;public interface ResumeDaoPlus {public List getList (Integer [] siteId, Limitable limitable);}

The implementation class of the ResumeDaoPlus interface should be placed in the corresponding impl package.

Package com.jspxcms.plug.repository.impl;public class ResumeDaoImpl implements ResumeDaoPlus {@ SuppressWarnings ("unchecked") public List getList (Integer [] siteId, Limitable limitable) {JpqlBuilder jpql = new JpqlBuilder (); jpql.append ("from Resume bean where 1: 1"); if (ArrayUtils.isNotEmpty (siteId)) {jpql.append ("and bean.site.id in (: siteId)") Jpql.setParameter ("siteId", Arrays.asList (siteId));} return jpql.list (em, limitable);} private EntityManager em; @ PersistenceContext public void setEm (EntityManager em) {this.em = em;}}

Com.jspxcms.common.orm.JpqlBuilder is used to assemble jqpl statements, set parameters, and deal with paging problems.

At this point, the study on "how to use the DAO class" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report