Skip to content

Commit

Permalink
怎样通过参数判断
Browse files Browse the repository at this point in the history
  • Loading branch information
weishiji committed Apr 27, 2017
1 parent b41bd59 commit 941d933
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
12 changes: 6 additions & 6 deletions app/controllers/rest/CategoryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
public class CategoryController extends Controller{

private FormFactory formFactory;

@Inject
public CategoryController(FormFactory formFactory) {
this.formFactory = formFactory;
Expand All @@ -37,12 +38,9 @@ public CategoryController(FormFactory formFactory) {
* @return List
* */
public Result list(){
Category category = new Category();

List<Category> categories = category.list();

List<Category> categories = Category.list();
return ok(ResponseJson.format(categories,OK));

}
/**
* @apiNote 创建分类
Expand Down Expand Up @@ -104,7 +102,9 @@ public Result update(Long category_id){

Transaction txn = Ebean.beginTransaction();
try {
Category savedCategory = Category.find.byId(category_id);

//Category savedCategory = Category.find.byId(category_id);
Category savedCategory = Category.getCategoryById(category_id);
if(savedCategory != null){
Category newCategoryData = categoryForm.get();
savedCategory.setStatus(newCategoryData.getStatus());
Expand All @@ -121,5 +121,5 @@ public Result update(Long category_id){
txn.end();
}
}

}
24 changes: 17 additions & 7 deletions app/models/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.avaje.ebean.FetchConfig;
import com.avaje.ebean.Model;
import com.avaje.ebean.PagedList;
import com.avaje.ebean.annotation.JsonIgnore;
import com.avaje.ebean.annotation.WhenCreated;
import com.avaje.ebean.annotation.WhenModified;
import javafx.beans.DefaultProperty;
import org.springframework.context.annotation.Conditional;
import org.springframework.format.annotation.DateTimeFormat;
import play.data.format.Formats;
import play.data.validation.Constraints;
Expand Down Expand Up @@ -50,6 +52,11 @@ public class Category extends Model {
@WhenModified
private Date date_modified;

//尼玛,关键是这两组属性都要写,浪费老子一周的事件
@JoinColumn(name = "category_id",referencedColumnName = "category_id")
@OneToMany(mappedBy = "category")
public List<ProductToCategory> product_to_category;


public Long getCategory_id(){
return category_id;
Expand All @@ -72,21 +79,17 @@ public void setStatus(byte aStatus){
status = aStatus;
}

//尼玛,关键是这两组属性都要写,浪费老子一周的事件
@JoinColumn(name = "category_id",referencedColumnName = "category_id")
@OneToMany(mappedBy = "category")
public List<ProductToCategory> product_to_category;

/**
* Generic query helper for entity Category with id Long
*/
public static Find<Long,Category> find = new Find<Long,Category>(){};

public List<Category> list(){

public static List<Category> list(){
//product_to_category = null;
List<Category> category = Category.find
.fetch("product_to_category",new FetchConfig().lazy())
//.fetch("product_to_category.product")
.fetch("product_to_category.product")
.where()
.eq("status",1)
//.eq("category_id",1)
Expand All @@ -112,4 +115,11 @@ public static PagedList<Category> page(int page, int pageSize, String sortBy, St
.findPagedList(page, pageSize);
}

public static Category getCategoryById(Long category_id){
//product_to_category = null;
Category category = Category.find.byId(category_id);

return category;
}
}

0 comments on commit 941d933

Please sign in to comment.