Java后台实现方法

时间:2021-01-20 17:22:25

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传输