applicationContext.xml:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 引入jdbc配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 启动注解扫描 base-package:要扫描的包-->
<context:component-scan base-package="com.kc.mybatis"/>
<!-- 启动注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 事务管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置 dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${db.driverClassName}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:configuration.xml"></property>
</bean>
<!-- 配置Dao,由于Mybatis中,Dao没有实现类 ,因此配置的时候要将class属性指向MapperFactory工厂-->
<bean id="empDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
<property name="mapperInterface" value="com.kc.mybatis.dao.IEmployeeDao"></property>
</bean>
<!--配置哪些方法,什么情况下需要回滚 -->
<tx:advice id="serviceAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--当代理的service层中的方法抛出异常的时候才回滚,必须加rollback-for参数 -->
<tx:method name="add*" propagation="REQUIRED" rollback-for="java.sql.SQLException" />
<tx:method name="del*" propagation="REQUIRED" rollback-for="java.sql.SQLException" />
<tx:method name="upd*" propagation="REQUIRED" rollback-for="java.sql.SQLException" />
<!--除了上面标识的方法,其他方法全是只读方法 -->
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置哪些类的方法需要进行事务管理 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="servicePointcut" expression="execution(* com.kc.mybatis.dao.impl.*.*(..))" />
<aop:advisor pointcut-ref="servicePointcut" advice-ref="serviceAdvice" />
</aop:config>
</beans>
configuration.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- sql的配置文件的别名移入这个文件 -->
<typeAliases>
<typeAlias alias="Emp" type="com.kc.mybatis.bean.Employee" />
<typeAlias alias="Dept" type="com.kc.mybatis.bean.Department" />
</typeAliases>
<mappers>
<mapper resource="com/kc/mybatis/bean/data/employee.xml" />
</mappers>
</configuration>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>myBatisHr</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="json-default">
<action name="*_*" class="{1}ActionBean" method="{2}">
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="listEmp">listemp.jsp</result>
<result name="empAdd">addemp.jsp</result>
<result name="empUpd">updemp.jsp</result>
<result name="empque">displayemp.jsp</result>
<result type="json">
<param name="root"></param>
</result>
</action>
</package>
</struts>
db.properties:
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/hr
db.username=root
db.password=520520t
EmpAction:
package com.kc.mybatis.action;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.kc.mybatis.bean.Employee;
import com.kc.mybatis.service.IEmployeeService;
@Scope("prototype") /* 指定Bean的作用域为原型 */
@Controller("EmpActionBean") /**/
public class EmpAction {
@Resource /* 注入对象,默认按名称注入,当找不到与名称匹配的bean才会按类型注入首选。 */
private IEmployeeService service;
private List<HashMap<String, String>> employeeList = new ArrayList<HashMap<String, String>>();
private Employee employee;
private List<HashMap<String, String>> deptList = new ArrayList<HashMap<String, String>>();
public String queryAllEmployee() {
employeeList = service.queryAllEmployee(employee);
return "listEmp";
}
public List<HashMap<String, String>> getEmployeeList() {
return employeeList;
}
public void setEmployeeList(List<HashMap<String, String>> employeeList) {
this.employeeList = employeeList;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public List<HashMap<String, String>> getDeptList() {
return deptList;
}
public void setDeptList(List<HashMap<String, String>> deptList) {
this.deptList = deptList;
}
public void setService(IEmployeeService service) {
this.service = service;
}
}
employee.xml:
<?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.kc.mybatis.dao.IEmployeeDao">
<select id="queryAllEmployee" resultType="java.util.HashMap"
parameterType="Emp">
SELECT e.empid as empid,e.ename as ename,d.dname as dname,e.money as
money FROM TblEmp
e LEFT JOIN TblDept
d ON e.deptid = d.deptid
<where>
<if test="ename != null">
AND e.ename like concat('%',#{ename},'%')
</if>
</where>
</select>
</mapper>
Employee.java:
package com.kc.mybatis.bean;
import org.springframework.context.annotation.Scope;
@Scope("prototype")
public class Employee {
private Integer empId;
private String ename;
private Integer egendar;
private Integer deptId;
private Double money;
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Integer getEgendar() {
return egendar;
}
public void setEgendar(Integer egendar) {
this.egendar = egendar;
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
}
EmployeeServiceImpl.java:
package com.kc.mybatis.service.impl;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.kc.mybatis.bean.Employee;
import com.kc.mybatis.dao.IEmployeeDao;
import com.kc.mybatis.service.IEmployeeService;
@Service("EmployeeServiceBean")
public class EmployeeServiceImpl implements IEmployeeService {
@Resource
private IEmployeeDao dao;
@Override
public List<HashMap<String, String>> queryAllEmployee(Employee employee) throws SQLException {
return dao.queryAllEmployee(employee);
}
public void setDao(IEmployeeDao dao) {
this.dao = dao;
}
}
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="Emp_queryAllEmployee.action">操作员工</a>
</body>
</html>
listemp.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="js/jquery-3.0.0.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript" src="js/jquery-3.0.0.js"></script>
<style>
table, tr, td {
border: 1px solid black;
}
</style>
<body id="body">
<form action="Emp_queryAllEmployee.action" method="post">
<table>
<tr>
<td colspan="5" align="center">
<input type="text" id="ename" name="employee.ename">
<input type="submit" value="查姓名">
</td>
</tr>
<tr>
<td>员工编号</td>
<td>姓名</td>
<td>部门名称</td>
<td>金额</td>
<td>修改</td>
<td>删除</td>
</tr>
<c:forEach items="${employeeList}" var="e" varStatus="status">
<tr>
<td>${status.count}</td>
<td>
<a href="Emp_querySingleEmployee.action?employee.empId=${e.empid}">${e.ename}</a>
</td>
<td>${e.dname}</td>
<td>${e.money}</td>
<td><a
href="Emp_querySingleEmployee.action?employee.empId=${e.empid}">修改</a></td>
<td><a href="Emp_delEmployee.action?employee.empId=${e.empid}"
οnclick="return validate()">删除</a></td>
</tr>
</c:forEach>
<tr>
<td colspan="5"><a href="Emp_queryAllDept.action">添加</a></td>
</tr>
</table>
</form>
</body>
<script type="text/javascript">
function validate() {
return confirm("你确定要删除吗?");
}
</script>
</html>
SQL:
CREATE TABLE TblDept(
deptid int primary key auto_increment,
dname varchar(50) unique
)
INSERT INTO TblDept VALUES(101,'人力资源部');
INSERT INTO TblDept VALUES(102,'市场部');
INSERT INTO TblDept VALUES(103,'财务部');
INSERT INTO TblDept VALUES(104,'软件部');
drop table TblEmp;
CREATE TABLE TblEmp(
empid int primary key auto_increment,
ename varchar(50) not null,
egendar tinyint not null default 0,
deptid int not null,
money double(7,2)
)
INSERT INTO TblEmp VALUES(101,'杨谷',1,101,2000);
INSERT INTO TblEmp VALUES(102,'讲会东',1,102,6000);
INSERT INTO TblEmp VALUES(103,'李效林',1,101,3000);
INSERT INTO TblEmp VALUES(104,'刘凡',1,103,8000);
JAR包过多,不一一列举了。。。