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传输
相关文章推荐
- Java Reflection(二):Classes
- LeetCode_50---Pow(x, n)
- java使用UDP协议传输数据
- Java线程池使用说明
- 【转】windows上自动设置java环境变量的脚本
- java垃圾回收机制
- 转发路径下的反斜杠,文件系统中的反斜杠;struts中的servletpath路径与springmvc中servlet请求路径不一样
- 【Java编程思想】(6)内部类
- JDK,JRE,JVM区别与联系
- 【转】java jvm 线程 与操作系统线程
- Kettle5.4.0 java.lang.OutOfMemoryError
- Java Reflection(一):Java反射指南
- 《Java程序设计》第十六周周四(项目2)
- Java中list集合对比方法
- java.lang.RuntimeException: Camera is being used after Camera.release() was called
- Eclipse上的Android工程显示出错,但是其实没有异常解决
- 【Spring学习笔记-MVC-8.1】SpringMVC之类型转换@initBinder
- 把自己的jsp或html模板添加到myeclipse中
- JAVA的基本数据类型打印输出
- MyEclipse2014安装插件的几种方式(适用于Eclipse或MyEclipse其他版本)