Java后台實現方法
首先后台結構分為四個部分(以表schedule為例)
entity>mapper>service>controller
1. 在entity里面寫好實體,新建目錄schedule,再建子文件Schedule.java,在里面定義好全部表名的字段
package com.eisp.eoms.entity.schedule;
import java.sql.Timestamp;
//日志信息
public class Schedule {
private Long scheId;
private String schCode;
private String userCode;
private String orgCode;
private String scheduleContent;
private Timestamp startDate;
private Timestamp endDate;
private String comments;
}
2.在mapper里面寫好接口
建立schedule目錄,再建立ScheduleMapper.java文件
里面寫sql語句
/**
* @author Administrator
*
*/
package com.eisp.eoms.mapper.schedule;
import java.util.List;
import com.eisp.eoms.entity.schedule.Schedule;
public interface ScheduleMapper {
List<Schedule>selectByUserCode(String userCode);
List<Schedule>select();
}
//select和selectByUserCode名稱不能一樣
再建立ScheduleMapper.xml文件,里面寫sql語
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.eisp.eoms.mapper.schedule.ScheduleMapper">
<select id="select" resultType="com.eisp.eoms.entity.schedule.Schedule">
select * from EOMS_SCHEDULE
</select>
<select id="selectByUserCode" parameterType="String"
resultType="com.eisp.eoms.entity.schedule.Schedule">
select * from EOMS_SCHEDULE where USERCODE=#{param1}
</select>
</mapper>
//id相應ScheduleMapper.java中的select方法
3.在service層建立schedule目錄。再建立ScheduleService.java文件,里面寫的語句例如以下
package com.eisp.eoms.service.schedule;
import java.util.List;
import com.eisp.eoms.entity.schedule.Schedule;
public interface ScheduleService {
List<Schedule> selectByUserCode(String userCode);
List<Schedule> select();
}
再建立impl文件,里面建立ScheduleServiceImpl.java文件
package com.eisp.eoms.service.schedule.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.eisp.eoms.entity.schedule.Schedule;
import com.eisp.eoms.mapper.schedule.ScheduleMapper;
import com.eisp.eoms.service.schedule.ScheduleService;
@Service
public class ScheduleServiceImpl implements ScheduleService {
@Autowired
private ScheduleMapper scheduleMapper;
public List<Schedule> selectByUserCode(String userCode) {
return scheduleMapper.selectByUserCode(userCode);
}
public List<Schedule> select() {
return scheduleMapper.select();
}
}
4.在controller新建schedule文件件。里面建業務文件SonntagController.java文件用於接收數據
package com.eisp.eoms.controller.schedule;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.eisp.eoms.service.schedule.ScheduleService;
@Controller
@RequestMapping("/schedule/sonntag")
public class SonntagController {
@Autowired
private ScheduleService scheduleService;
@RequestMapping("/showIndex")
public ModelAndView scheduleIndex() {
return new ModelAndView("schedule/index");
}
@RequestMapping("/list")
@ResponseBody
public Map<String, Object> listAll() {
Map<String, Object> data = new HashMap<String, Object>();
data.put("list", scheduleService.select());
data.put("AAA", scheduleService.select());
System.out.println(data);
return data;
}
}
總結,經過以上幾個環節。后台數據庫表的數據可以成功調取出來。當然假設要載入到頁面上。還須要js中用ajax傳輸