package com.action;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.alibaba.fastjson.JSONArray;
import com.dto.Student;
import com.dto.Teacher;
import com.service.StudentService;
import com.util.PageUtils;
public class Action {
private StudentService service;
private List<Student> studentList;
private Student student;
private String mohu;
private PageUtils pu;
private Integer cpage;
public String list(){
if(cpage==null){
cpage=1;
}
Integer pageSize = 2;
Integer count = service.getCount(mohu);
pu = new PageUtils(cpage, pageSize, count);
studentList = service.getStudentList(pu,mohu);
return "list";
}
public void getData() throws IOException{
List<Teacher> teacherList = service.findTeacherList();
student = service.getStudent(student);
Map<String, Object> map = new HashMap<String, Object>();
map.put("teacherList", teacherList);
map.put("student", student);
String jsonString = JSONArray.toJSONString(map);
System.out.println(jsonString);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
response.getWriter().print(jsonString);
}
public void saveOrUpdate() throws IOException{
int i = service.saveOrUpdate(student);
HttpServletResponse response = ServletActionContext.getResponse();
response.getWriter().print(i);
}
public void del() throws IOException{
int i = service.delete(student);
HttpServletResponse response = ServletActionContext.getResponse();
response.getWriter().print(i);
}
public StudentService getService() {
return service;
}
public void setService(StudentService service) {
this.service = service;
}
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String getMohu() {
return mohu;
}
public void setMohu(String mohu) {
this.mohu = mohu;
}
public PageUtils getPu() {
return pu;
}
public void setPu(PageUtils pu) {
this.pu = pu;
}
public Integer getCpage() {
return cpage;
}
public void setCpage(Integer cpage) {
this.cpage = cpage;
}
}
package com.dao;
import java.util.List;
import org.hibernate.Query;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.dto.Student;
import com.dto.Teacher;
import com.util.PageUtils;
public class StudentDao extends HibernateDaoSupport{
public List<Student> studentList(PageUtils pu, String mohu) {
String hql = " from Student ";
if(mohu!=null){
hql+=" where name like '%"+mohu+"%' ";
}
Query query = getSession().createQuery(hql).setFirstResult(pu.getStartIndex()).setMaxResults(pu.getPageSize());
List list = query.list();
return list;
}
public List<Teacher> findTeacherList() {
return getHibernateTemplate().find("from Teacher");
}
public Student getStudent(Student student) {
return (Student) getHibernateTemplate().get(Student.class, student.getId());
}
public int saveOrUpdate(Student student) {
// TODO Auto-generated method stub
try {
getHibernateTemplate().saveOrUpdate(student);
return 1;
} catch (DataAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
}
public int delete(Student student) {
// TODO Auto-generated method stub
try {
getHibernateTemplate().delete(student);
return 1;
} catch (DataAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
}
public Integer getCount(String mohu) {
// TODO Auto-generated method stub
String hql = " select count(*) from Student ";
if(mohu!=null){
hql+=" where name like '%"+mohu+"%' ";
}
Query query = getSession().createQuery(hql);
Integer count = Integer.valueOf(query.uniqueResult().toString());
return count;
}
}
人总是会喜欢自己擅长领域的东西,那是因为你有足够的成就感和满足感。但人,还是要学习着,并尝试会去消化那些你所不擅长且天生笨拙的领域。