<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <context:component-scan base-package="com.icss.hr"></context:component-scan> <!-- 1.配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hr"/> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="user" value="root" /> <property name="password" value="admin" /> <property name="initialPoolSize" value="3" /> <property name="maxPoolSize" value="10" /> <property name="minPoolSize" value="1" /> <property name="acquireIncrement" value="3" /> <property name="maxIdleTime" value="60" /> </bean> <!-- 2.JDBCTemplate --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 3.配置事务 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:advice id="txa" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/> <tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/> <tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/> <tx:method name="query*" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* com.icss.hr.*.service.*.*(..))" id="txCut"/> <aop:advisor advice-ref="txa" pointcut-ref="txCut"/> </aop:config> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 跳转路径 字符串前面加上路径 --> <!-- view里面的字符串加上后缀 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
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; @Repository public interface DeptDao { public boolean addDept(Dept dept); public boolean updateDept(Dept dept); public boolean deleteDept(int dept_id); public List<Dept> 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; @Repository public 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<Dept> queryAllDept() { StringBuilder sql = new StringBuilder() .append("SELECT * ") .append("FROM dept"); return jdbcTemplate.query(sql.toString(), new RowMapper<Dept>() { @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; @Service public interface DeptService { public boolean addDept(Dept dept); public boolean updateDept(Dept dept); public boolean deleteDept(int dept_id); public List<Dept> 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; @Service public 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<Dept> 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<Dept> 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); } }
时间: 2024-11-05 11:41:00