在上一篇文章中,我们通过框架,实现了最基本的数据存储的功能,这一篇将实现一个最基本的数据查询功能。
首先,在Address接口和AddressImpl类中新增selectAll()方法,用来获取数据库中的数据:Address中的代码:
package Service;
import com.dojo.UserEntity;
import java.util.List;
public interface Address {
void add(UserEntity ue);
List<UserEntity> selectAll();
}
AddressImpl中的代码:
package Service;
import com.dojo.UserEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.List;
public class AddressImpl implements Address{
@Qualifier("sessionFactory")
@Autowired
private SessionFactory sessionFactory;