码迷,mamicode.com
首页 > 其他好文 > 详细

maven各种类的基本写法

时间:2016-06-28 12:22:44      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

一、model类

        专业类:(专业类和学校类做了多对多映射)

  

package com.cqvie.model;

import java.sql.Date;
import java.util.HashSet;
import java.util.Set;

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

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;

@Table(name = "major")
@Entity
public class Major {
	private int majorID;// 专业ID
	private int majorCode;// 专业代码
	private String majorName;// 专业名称
	private Date createDate;// 创建时间
	private Set<School> schools =new HashSet<School>();

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "majorID", nullable = false, length = 10)
	public int getMajorID() {
		return majorID;
	}

	public void setMajorID(int majorID) {
		this.majorID = majorID;
	}

	@Column(name = "majorCode", nullable = false, length = 10)
	public int getMajorCode() {
		return majorCode;
	}

	public void setMajorCode(int majorCode) {
		this.majorCode = majorCode;
	}

	@Column(name = "majorName", nullable = false, length = 20)
	public String getMajorName() {
		return majorName;
	}

	public void setMajorName(String majorName) {
		this.majorName = majorName;
	}

	@Column(name = "createDate", nullable = false, length = 10)
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	@ManyToMany(mappedBy = "majors")
	@JsonIgnore
	public Set<School> getSchools() {
		return schools;
	}

	
	public void setSchools(Set<School> schools) {
		this.schools = schools;
	}

	@Override
	public String toString() {
		return "Major [majorID=" + majorID + ", majorCode=" + majorCode + ", majorName=" + majorName + ", createDate="
				+ createDate + "]";
	}

}

 

 

学校类:

 

package com.cqvie.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name = "school")
public class School {
	private int schoolID;// 高校ID
	private String schName;// 高校名称
	private String category;// 高校类别,专科或者本科
	private String position;// 地址
	private String web;// 高校官网
	private List<Major> majors = new ArrayList<Major>();// 学校对应的专业

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "schoolID", nullable = false, length = 10)
	public int getSchoolID() {
		return schoolID;
	}

	public void setSchoolID(int schoolID) {
		this.schoolID = schoolID;
	}

	@Column(name = "schoolName", nullable = true, length = 10)
	public String getSchName() {
		return schName;
	}

	public void setSchName(String schName) {
		this.schName = schName;
	}

	@Column(name = "category", nullable = false, length = 20)
	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	@Column(name = "position", nullable = false, length = 9)
	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	@Column(name = "web", nullable = false, length = 50)
	public String getWeb() {
		return web;
	}

	public void setWeb(String web) {
		this.web = web;
	}

	@ManyToMany(cascade = CascadeType.ALL)
	@JoinTable(name = "school_major", joinColumns = { @JoinColumn(name = "schoolID") }, inverseJoinColumns = {
			@JoinColumn(name = "MajorId") })
	public List<Major> getMajors() {
		return majors;
	}

	public void setMajors(List<Major> majors) {
		this.majors = majors;
	}

	@Override
	public String toString() {
		return "School [schoolID=" + schoolID + ", schName=" + schName + ", category=" + category + ", position="
				+ position + ", web=" + web + ", majors=" + majors + "]";
	}

}


二、Dao层类:

 

       (一)类名前加注解:@Repository("xxx")作为调用该类的ID

 

     (二)激活sessionFactory:得到当前线程

 

     @Resource
	protected SessionFactory sessionFactory;

 

 

     (三)方法内部通过sessionFactory获得session

 

	Session session = sessionFactory.getCurrentSession();


实例:

 

 

package com.cqvie.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.cqvie.model.Major;

@Repository("majorDao")
public class MajorDao {
	@Resource
	protected SessionFactory sessionFactory;

	// 关键字模糊查询
	public List<Major> fuzzyQuery(String keyword) {
		Session session = sessionFactory.getCurrentSession();
		String sql = "from Major where majorName like ? ";
		Query query = session.createQuery(sql);
		query.setString(0, "%" + keyword + "%");
		List<Major> majors = query.list();
		return majors;
	}
}


三、service层类

 

     (一)、类名前加注解:@Transactional   声明这个service所有方法需要事务管理。每一个业务方法开始时都会打开一个事务。 
                                            @Service("xxx)  service唯一标识

   (二)、激活需要使用的dao层类:@Resource
                                                    private MajorDao majorDao;

实例:

package com.cqvie.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cqvie.dao.MajorDao;
import com.cqvie.model.Major;
import com.cqvie.model.School;

@Transactional
@Service("majorService")
public class MajorService {
	@Resource
	private MajorDao majorDao;

	// 关键字模糊查询
	public List<Major> fuzzyQuery(String keyword) {
		return majorDao.fuzzyQuery(keyword);
	}

	// 关键字模糊查询并获得最多5个数据
	public List<Major> fuzzyQueryMaxFive(String keyword) {
		List<Major> majors = majorDao.fuzzyQuery(keyword);
		List<Major> retMajors;
		// 如果查询出的数据大于5,截取前5个数据,反之,直接获取全部
		retMajors = majors.size() > 5 ? majors.subList(0, 4) : majors;
		return retMajors;
	}
	
}

 

 

四、控制层类

  (一)、类名前加注释:

                  @Controller
                @RequestMapping("xxx")
 
    (二)、激活需要使用的service类

              @Resource
      private MajorService majorService;

实例:

 

package com.cqvie.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqvie.model.Major;
import com.cqvie.model.School;
import com.cqvie.service.MajorService;
import com.cqvie.service.SchoolService;

@Controller
@RequestMapping("/major")
public class MajorController {
	@Resource
	private MajorService majorService;

	// 模糊查询
	@ResponseBody
	@RequestMapping(value = "/fuzzyQueryMajor")
	public List<Major> fuzzyQuerySchool(String keyword) {
		return majorService.fuzzyQuery(keyword);
	}

	// 输入框的模糊匹配
	@ResponseBody
	@RequestMapping("/inputFuzzyQuery")
	public List<Major> inputFuzzyQuery(String keyword) {
		return majorService.fuzzyQueryMaxFive(keyword);
	}
}




 

maven各种类的基本写法

标签:

原文地址:http://www.cnblogs.com/SunAutumn/p/5622773.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!