博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring抽象JDBC,使用JdbcTemplate
阅读量:5031 次
发布时间:2019-06-12

本文共 6854 字,大约阅读时间需要 22 分钟。

 

Dept.java

package com.icss.hr.dept.pojo;/** * 部门类 * @author Administrator * */public class Dept {	private int dept_id;	private String dept_name;	private String dept_loc;			public Dept() {		super();	}	public Dept(int dept_id, String dept_name, String dept_loc) {		super();		this.dept_id = dept_id;		this.dept_name = dept_name;		this.dept_loc = dept_loc;	}	public int getDept_id() {		return dept_id;	}	public void setDept_id(int dept_id) {		this.dept_id = dept_id;	}	public String getDept_name() {		return dept_name;	}	public void setDept_name(String dept_name) {		this.dept_name = dept_name;	}	public String getDept_loc() {		return dept_loc;	}	public void setDept_loc(String dept_loc) {		this.dept_loc = dept_loc;	}	@Override	public String toString() {		return "Dept [dept_id=" + dept_id + ", dept_name=" + dept_name + ", dept_loc=" + dept_loc + "]";	}}

Dao.java

package com.icss.hr.dept.dao;import java.util.List;import org.springframework.stereotype.Repository;import com.icss.hr.dept.pojo.Dept;@Repositorypublic interface DeptDao {	public boolean addDept(Dept dept);	public boolean updateDept(Dept dept);	public boolean deleteDept(int dept_id);		public List
queryAllDept(); public Dept queryDeptById(int dept_id); }

DaoImpl.java

package com.icss.hr.dept.dao;import java.sql.ResultSet;import java.sql.SQLException;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.RowMapper;import org.springframework.stereotype.Repository;import com.icss.hr.dept.pojo.Dept;@Repositorypublic class DeptDaoImpl implements DeptDao {		@Autowired	private JdbcTemplate jdbcTemplate;			public JdbcTemplate getJdbcTemplate() {		return jdbcTemplate;	}	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {		this.jdbcTemplate = jdbcTemplate;	}	@Override	public boolean addDept(Dept dept) {		StringBuilder sql = new StringBuilder()				.append("INSERT INTO dept ")				.append("VALUES(?,?,?)");		System.out.println(sql);		int res = jdbcTemplate.update(sql.toString(),dept.getDept_id(),dept.getDept_name(),dept.getDept_loc());				return res > 0 ? true : false;	}	@Override	public boolean updateDept(Dept dept) {		StringBuilder sql = new StringBuilder()				.append("update dept ")				.append("SET dept_name=?,dept_loc=? ")				.append("WHERE dept_id=?");		System.out.println(sql);		int res = jdbcTemplate.update(sql.toString(),dept.getDept_name(),dept.getDept_loc(),dept.getDept_id());				return res > 0 ? true : false;	}	@Override	public boolean deleteDept(int dept_id) {		StringBuilder sql = new StringBuilder()				.append("DELETE FROM dept ")				.append("WHERE dept_id=?");		System.out.println(sql);		int res = jdbcTemplate.update(sql.toString(),dept_id);				return res > 0 ? true : false;	}	@Override	public List
queryAllDept() { StringBuilder sql = new StringBuilder() .append("SELECT * ") .append("FROM dept"); return jdbcTemplate.query(sql.toString(), new RowMapper
() { @Override public Dept mapRow(ResultSet rs, int arg1) throws SQLException { //解析行 Dept d = new Dept(rs.getInt("dept_id"),rs.getString(2),rs.getString(3)); return d; } }); } @Override public Dept queryDeptById(int dept_id) { // TODO Auto-generated method stub return null; }}

DeptService.java

package com.icss.hr.dept.service;import java.util.List;import org.springframework.stereotype.Service;import com.icss.hr.dept.pojo.Dept;@Servicepublic interface DeptService {	public boolean addDept(Dept dept);	public boolean updateDept(Dept dept);	public boolean deleteDept(int dept_id);	public List
queryDepts();}

DeptServiceImpl.java

package com.icss.hr.dept.service;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Service;import com.icss.hr.dept.dao.DeptDao;import com.icss.hr.dept.pojo.Dept;@Servicepublic class DeptServiceImpl implements DeptService {	@Autowired	@Qualifier(value="deptDaoImpl")	private DeptDao deptDao;		public DeptDao getDeptDao() {		return deptDao;	}	public void setDeptDao(DeptDao deptDao) {		this.deptDao = deptDao;	}	@Override	public boolean addDept(Dept dept) {		//日志文件				return deptDao.addDept(dept);	}	@Override	public boolean updateDept(Dept dept) {		// TODO Auto-generated method stub		return deptDao.updateDept(dept);	}	@Override	public boolean deleteDept(int dept_id) {		// TODO Auto-generated method stub		return deptDao.deleteDept(dept_id);	}	@Override	public List
queryDepts() { // TODO Auto-generated method stub return deptDao.queryAllDept(); }}

DeptAction.java

package com.icss.hr.dept.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import com.icss.hr.dept.pojo.Dept;import com.icss.hr.dept.service.DeptService;@Controller@RequestMapping("/dept")public class DeptAction {	@Autowired	@Qualifier(value="deptServiceImpl")	private DeptService deptService;			public DeptService getDeptService() {		return deptService;	}	public void setDeptService(DeptService deptService) {		this.deptService = deptService;	}		@RequestMapping("/add.action")	public String addDept(Model mo,Dept dept){		boolean res = deptService.addDept(dept);				return res ? "redirect:/dept/show.action" : "../error";			}	@RequestMapping("/show.action")	public String showDept(Model mo){		List
depts = deptService.queryDepts(); mo.addAttribute("depts",depts); return "../show"; } @RequestMapping("/delete.action") public String deleteDept(int dept_id){ boolean res = deptService.deleteDept(dept_id); return res ? "redirect:/dept/show.action" : "../error"; } @RequestMapping("/update.action") public String updateDept(Dept dept){ System.out.println(dept.getDept_id()); boolean res = deptService.updateDept(dept); System.out.println(res); return res ? "redirect:/dept/show.action" : "../error"; }}

TestDept.java

package com.icss.hr.dept.test;import java.util.List;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.icss.hr.dept.pojo.Dept;import com.icss.hr.dept.service.DeptService;public class TestDept {	@Test	public void test(){		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");		DeptService service = (DeptService)ac.getBean("deptServiceImpl");		boolean b = service.updateDept(new Dept(7,"国防部","北京"));		System.out.println(b);	}}

  

  

  

 

转载于:https://www.cnblogs.com/1020182600HENG/p/6907568.html

你可能感兴趣的文章
邮件中的样式问题
查看>>
AJAX 状态值与状态码详解
查看>>
php面向对象编程(oop)基础知识示例解释
查看>>
1.在数组中找到与给定总和的配对
查看>>
树的子结构
查看>>
关于根据Build Platform或者OS 加载x86或者x64 dll的问题
查看>>
程序员高效开发的几个技巧
查看>>
js-权威指南学习笔记19.2
查看>>
hexo 搭建博客
查看>>
关于 UIWebView 几个高级用法
查看>>
maven创建的项目中无法创建src/main/java 解决方案
查看>>
华为软件开发云测评报告二:代码检查
查看>>
集合1
查看>>
关键词 virtual
查看>>
建造者模式(屌丝专用)
查看>>
UVALive 4730 Kingdom +段树和支票托收
查看>>
[APIO2010]特别行动队
查看>>
SpringBoot 集成ehcache
查看>>
初步swift语言学习笔记2(可选类型?和隐式可选类型!)
查看>>
Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点
查看>>