持久层开发,基于lucene,hibernate-search

本文深入探讨了使用Java POJO类和Hibernate框架进行数据持久化及全文检索的技术实操,包括类的设计、DAO接口实现及模糊查询功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、pojo类编写

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.search.annotations.Analyzer;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;
import org.wltea.analyzer.lucene.IKAnalyzer;

@Entity
@Table(name="student")
@Indexed(index="student")
public class Student extends DominObject {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9090499971596826674L;

	public Student() {
	}

	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}
	/**
	 * 业务主键ID,自增,增量1
	 */
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Id
	@DocumentId
	private Integer id;
	
	/**
	 * 学生姓名
	 */
	@Column(length = 10, name = "student_name", nullable = false)
	@Field(name = "content", store = Store.YES, index = Index.TOKENIZED, analyzer = @Analyzer(impl = IKAnalyzer.class))  
	private String name = "";
	
	/**
	 * 学生年龄
	 */
	@Column(length = 2, name = "student_age", nullable = false)
	private Integer age = 0;

	public Integer getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}
}

 2、dao接口

import java.util.List;

import com.zyn.ssh.pojo.Student;


public interface StudentDao extends BaseEntityDao<Student, Integer>{
	public List<Student> findStudentByLike(String name);
}

 3、dao接口实现

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.springframework.stereotype.Repository;
import org.wltea.analyzer.lucene.IKQueryParser;

import com.zyn.ssh.dao.StudentDao;
import com.zyn.ssh.pojo.Student;

@Repository("studentDao")
public class StudentDaoImpl extends BaseEntityDaoImpl<Student, Integer>
		implements StudentDao {
	//将数据保存并建立索引
	public Serializable save(Student entity) {
		FullTextSession fullTextSession = Search
				.getFullTextSession(getSession());
		return fullTextSession.save(entity);
	}

	@SuppressWarnings("unchecked")
	//通过名字模糊查询学生
	public List<Student> findStudentByLike(String name) {
		try {
			Query query = Search.getFullTextSession(getSession())
					.createFullTextQuery(IKQueryParser.parse("content", name),
							Student.class);
			List<Student> result = query.list();
			return result;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值