您的位置:首页 > 编程语言 > Java开发

Java后台实现方法

2015-06-25 11:21 561 查看

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传输
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: