海门项目框架使用总结
- spring
- springmvc
- mybatis
目录结构
- entity——对应数据库表的实体类
- dao——连接数据库的接口
- daoImpl——连接数据库接口的实现类
- service——controller调用的服务
- serviceImpl——controller调用服务的实现类,通过调用dao处理业务逻辑
- controller——通过调用service完成前端用户的请求
- WebRoot——存放前端文件,web-inf下放jsp
entity
注解对应的表(注解位置:类名上)
@TrueAnnotation(name = "T_NTTJ_FBGL_CYTREE")
注解对应字段,主键需要加id=“id”(注解位置:get方法)
@TrueAnnotation(name = "CYID", id="id")
dao
接口
daoImpl
注解对应dao接口的名称(首字母小写)
@Component("cyTreeDao")
public class CyTreeDaoImpl extends GenericDaoImpl implements CyTreeDao
注入jdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
增
@Override
public int insertCyTree(CyTree cyTree) throws Exception {
return insert(cyTree);
}
删
@Override
public int deleteCyTreeById(String id) throws Exception {
String sql = "DELETE FROM T_NTTJ_FBGL_CYTREE t WHERE t.cyid= ? ";
return this.jdbcTemplate.update(sql, new Object[] { id });
}
改
@Override
public int updateCyTree(CyTree cyTree) throws Exception {
return this.update(cyTree);
}
查
@Override
public CyTree findCyTreeById(String id) throws Exception {
String sql = "SELECT t.cyid, t.cyFatherId,t.cymc,t.dycyzxid, "
+ "t.px FROM T_NTTJ_FBGL_CYTREE t WHERE t.cyid = ?";
ActorMapper actorMapper = new ActorMapper(new String[] { "cyid",
"cyFatherId", "cymc", "dycyzxid", "px"}, ActorMapper.RESULT_JAVABEAN, CyTree.class);
List<CyTree> list = this.jdbcTemplate.query(sql, actorMapper, new Object[] { id });
if (list != null && list.size() > 0) {
return list.get(0);
}
return null;
}
service
接口
serviceImpl
注解对应service接口的名称(首字母小写)
@Component("cyTreeService")
public class CyTreeServiceImpl implements CyTreeService
注入需要用到的dao接口
@Autowired
private CyTreeDao cyTreeDao;
处理业务逻辑
@Override
public List<CyTree> findCyTreeList(HashMap<String, String> map) {
List<CyTree> list = new ArrayList<CyTree>();
try {
// 获取子节点有该查询部门根节点
List<CyTree> childCyTree = this.cyTreeDao.findChildCyTree(map.get("cyRootId"));
// 根据主节点一层一层获取子节点
this.getCyTree(list, childCyTree);
} catch (Exception e) {
try {
sysErrorLogDao.saveErrorLog("CyTreeServiceImpl", e.getMessage(), "findCyTreeList", e);
} catch (Exception e1) {
}
LOG.error("分页查询菜单出错;异常信息:" + e.getMessage());
}
return list;
}
controller
申明controller,申明进入该控制器的路径
@Controller
@RequestMapping("/cyTreeController")
public class CyTreeController extends BaseController{
}
注入需要用到的service方法
@Autowired
private CyTreeService cyTreeService;
申明进入该方法的路径,放入转发参数,转发参数到页面上用el表达式显示
@RequestMapping(value = "/toCytreeList", method = { RequestMethod.GET,RequestMethod.POST })
public ModelAndView toCytreeList(HttpServletRequest request) {
String cymc = request.getParameter("cymc");
HashMap<String, String> map = new HashMap<String, String>();
map.put("cymc", cymc);
map.put("cyRootId", cyRootId);
List<CyTree> list = this.cyTreeService.findCyTreeList(map);
request.setAttribute("cymc", cymc);
request.setAttribute("cyRootId", cyRootId);
request.setAttribute("list", list);
return new ModelAndView("srms/cyTree/cyTree_list");
}
view
异步刷新
var page = "listStudent.do";
$.ajax({
type:"post",
url: page,
cache : false,
async : false,
success: function(result){
result = eval("(" + result + ")");
var temp = $(".tempDiv").html();
var rep = "";
for(var i = 0; i < result.stus.length; i++){
rep += temp.replace("$no1$",result.stus[i].id).replace("$no2$",result.stus[i].id).replace("$name$",result.stus[i].name).replace("$age$",result.stus[i].age).replace("$sex$",result.stus[i].sex);
}
$(".tbody-replace").html(rep);
},
error: function(result) {
alert("页面初始化失败");
}
});
对应的controller
//查询出所有的学生
@RequestMapping("/listStudent")
public void listStudent(HttpServletResponse response) throws Exception {
List<Student_msm> stus = maoService.getStus();
JSONObject json= new JSONObject();
// 放入转发参数
json.put("stus", stus);
response.setCharacterEncoding("utf-8");
this.outWirter(json, response);
}