(1)struts的注解
(2)角色 管理(基本的增删改查)
(3)dbutils的使用
1:struts的注解
代码:
main.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
%>
<!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>
<pre>
4:struts的注解(annotation)
A:需要加入struts的注解jar包。struts2-convention-plugin-2.3.28.jar
B:注解的Action必须要放在包名中包含struts或者action的包中。
<a href="<%=path%>/testAnnoAction">注解的Action</a>
C:Action的配置都可以使用注解来替换。拦截器或者转换器的配置必须在struts.xml中配置。然后Action的注解引用。
</pre>
</body>
</html>
TestAnnoAction.java
package com.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.util.BaseAction;
@ParentPackage(value = "default")
@Namespace(value = "/")
@Action(value = "testAnnoAction", results = {
@Result(name = "succ", location = "/succ.jsp"),
@Result(name = "succ_redirect", location = "/succ_redirect.jsp", type = "redirect")
}, interceptorRefs = {
@InterceptorRef(value = "isLogin"),
@InterceptorRef(value = "defaultStack")
})
public class TestAnnoAction extends BaseAction {
@Override
public String execute() throws Exception {
System.out.println("执行注解的Action....");
return "succ_redirect";
}
}
3:dbutil的使用:
a:Commons DbUtils是Apache组织提供的一个对JDBC进行简单封装的开源工具类库,使用它能够简化JDBC应用程序的开发,同时也不会影响程序的性能。
b:
DBUtils包括3个包:
org.apache.commons.dbutils
org.apache.commons.dbutils.handlers
org.apache.commons.dbutils.wrappers
DBUtils封装了对JDBC的操作,简化了JDBC操作,可以少写代码。
org.apache.commons.dbutils
DbUtils 关闭链接等操作
QueryRunner 进行查询的操作
org.apache.commons.dbutils.handlers
ArrayHandler :将ResultSet中第一行的数据转化成
对象数组
ArrayListHandler将ResultSet中所有的数据转化成List,List中存放的是Object[]
BeanHandler :将ResultSet中第一行的数据转化成类对象
BeanListHandler :将ResultSet中所有的数据转化成List,List中存放的是类对象
ColumnListHandler :将ResultSet中某一列的数据存成List,List中存放的是Object对象
KeyedHandler :将ResultSet中存成映射,key为某一列对应为Map。Map中存放的是数据
MapHandler :将ResultSet中第一行的数据存成Map映射
MapListHandler :将ResultSet中所有的数据存成List。List中存放的是Map
ScalarHandler :将ResultSet中一条记录的其中某一列的数据存成Object
org.apache.commons.dbutils.wrappers
SqlNullCheckedResultSet :对ResultSet进行操作,改版里面的值
StringTrimmedResultSet :去除ResultSet中中字段的左右空格。Trim()
主要方法:
DbUtils类:启动类
ResultSetHandler接口:转换类型接口
MapListHandler类:实现类,把记录转化成List
BeanListHandler类:实现类,把记录转化成List,使记录为JavaBean类型的对象
Query Runner类:执行SQL语句的类
c:其实只是使用的话,只看两个类(DbUtils 和QueryRunner)和一个接口(ResultSethandler)就可以了。
例子:
RoleDao.java
package com.dao;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.bean.RoleBean;
import com.util.JdbcUtil;
public class RoleDao {
public List<RoleBean> getRoleBeanList() {
QueryRunner qRunner = new QueryRunner();
Connection conn = null;
BeanListHandler<RoleBean> beanListHandler = new BeanListHandler<>(RoleBean.class);
String sql = "Select * From t_role order by role_id desc";
List<RoleBean> roleBeans = null;
try {
conn = JdbcUtil.getConn();
roleBeans = qRunner.query(conn, sql, beanListHandler);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
DbUtils.closeQuietly(conn);
}
return roleBeans;
}
public void save(RoleBean roleBean) {
QueryRunner runner = new QueryRunner(true);
Connection conn = null;
StringBuffer insertSQL = new StringBuffer();
insertSQL.append("Insert Into t_role");
insertSQL.append("(role_id,role_name,");
insertSQL.append("role_remark)");
insertSQL.append(" values(?,?,?)");
String role_id = roleBean.getRole_id();
String role_name = roleBean.getRole_name();
String role_remark = roleBean.getRole_remark();
Object[] paramArray = new Object[] { role_id, role_name, role_remark };
try {
conn = JdbcUtil.getConn();
int rowCount = runner
.update(conn, insertSQL.toString(), paramArray);
} catch (Exception e) {
// e.printStackTrace();
throw new RuntimeException(e);
} finally {
DbUtils.closeQuietly(conn);
}
}
public boolean isExistRole(String role_id) {
// TODO Auto-generated method stub
return false;
}
public void deleteRole(String role_id) {
Connection conn = null;
Statement stmt = null;
String delete_1 = "";
String delete_2 = "";
delete_1 = "Delete From t_role where role_id = '" + role_id + "'";
delete_2 = "Delete From t_role_menu where roleid = '" + role_id
+ "'";
try {
conn = JdbcUtil.getConn();
stmt = conn.createStatement();
conn.setAutoCommit(false);
stmt.executeUpdate(delete_1);
// int i = 1/0;
stmt.executeUpdate(delete_2);
conn.commit();
} catch (Exception e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
throw new RuntimeException(e);
} finally {
DbUtils.closeQuietly(stmt);
DbUtils.closeQuietly(conn);
}
}
public RoleBean getRoleBeanById(String role_id) {
QueryRunner runner = new QueryRunner();
Connection conn = null;
String sql = "Select * From t_role where role_id = '" + role_id
+ "'";
BeanHandler<RoleBean> handler = new BeanHandler(RoleBean.class);
RoleBean roleBean = null;
try {
conn = JdbcUtil.getConn();
roleBean = runner.query(conn, sql, handler);
} catch (Exception e) {
e.printStackTrace();
} finally {
DbUtils.closeQuietly(conn);
}
return roleBean;
}
public void modify(RoleBean roleBean) {
QueryRunner runner = new QueryRunner(true);
Connection conn = null;
StringBuffer updateSQL = new StringBuffer();
updateSQL.append("update t_role");
updateSQL.append(" set role_name = ?,role_remark = ?");
updateSQL.append(" where role_id = ?");
updateSQL.append(" ");
String role_id = roleBean.getRole_id();
String role_name = roleBean.getRole_name();
String role_remark = roleBean.getRole_remark();
Object[] paramArray = new Object[] { role_name, role_remark,role_id };
try {
conn = JdbcUtil.getConn();
int rowCount = runner
.update(conn, updateSQL.toString(), paramArray);
} catch (Exception e) {
// e.printStackTrace();
throw new RuntimeException(e);
} finally {
DbUtils.closeQuietly(conn);
}
}
}