简单的jdbc封装+servlet的封装



简单的jdbc封装+servlet的封装

用于实体映射的类(Column.java,Tab.java)
Tab.java(表映射类)
package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 方法注解映射,映射调用的方法
* @author facebook
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface MethodAnnotation {
String method() default “”;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Column.java(字段映射类)
package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 实体类映射数据库表类
* @author facebook
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE})
public @interface Tab {
/**
* 用于修饰实体类,表示实体类名与数据库表名之间的映射
* @return
*/
String table() default “”;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Employee.java(员工实体类)
package entity;

import java.io.Serializable;

import annotation.Column;
import annotation.Tab;

@Tab(table = “employee”)
public class Employee implements Serializable{
private static final long serialVersionUID = 1L;

@Column(name=”emp_id”,type=Integer.class,isNull = false,isPrimary = true,isAutoIncrement=true,isUnique = false)
private Integer emp_id;

@Column(name=”emp_code”,type=String.class,len = 30,isNull = false,isUnique = false)
private String emp_code;

@Column(name=”emp_name”,type=String.class,len = 30,isNull = true)
private String emp_name;

public Employee(){
super();
}

public Employee(String emp_code, String emp_name) {
this();
this.emp_code = emp_code;
this.emp_name = emp_name;
}

public Integer getEmp_id() {
return emp_id;
}
public void setEmp_id(Integer emp_id) {
this.emp_id = emp_id;
}
public String getEmp_code() {
return emp_code;
}
public void setEmp_code(String emp_code) {
this.emp_code = emp_code;
}
public String getEmp_name() {
return emp_name;
}
public void setEmp_name(String emp_name) {
this.emp_name = emp_name;
}

/**
* 重写父类的toString方法,将对象属性以field&value的形式组装起来
*/
@Override
public String toString() {
return “emp_id=” + emp_id + “&emp_code=” + emp_code + “&emp_name=” + emp_name;
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
BaseException.java(自定义异常类)
package exception;

/**
* 自定义异常
* @author facebook
*
*/
@SuppressWarnings(“serial”)
public class BaseException extends RuntimeException{

public BaseException() {
super();
// TODO Auto-generated constructor stub
}

public BaseException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}

public BaseException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}

public BaseException(String message) {
super(message);
// TODO Auto-generated constructor stub
}

public BaseException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
BaseDao.java(数据库CURD)
package exception;


/**
* 自定义异常
* @author facebook
*
*/
@SuppressWarnings(“serial”)
public class BaseException extends RuntimeException{

public BaseException() {
super();
// TODO Auto-generated constructor stub
}

public BaseException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}

public BaseException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}

public BaseException(String message) {
super(message);
// TODO Auto-generated constructor stub
}

public BaseException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
DatabaseConnection.java(数据库连接与对象封装类)
package utils;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class DatabaseConnection {
private static String username = “root”;
private static String userpassword = “120607″;
private static String url = “jdbc:mysql://localhost:3307/employee?Unicode=true&characterEncoding=UTF-8″;
public static Connection conn;

static{
try {
Class.forName(“com.mysql.jdbc.Driver”);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取数据库连接
*/
public static void connection(){
try {
conn = (Connection) DriverManager.getConnection(url,username,userpassword);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 创建PreparedStatement对象
* @param sql
* @return
* @throws SQLException
*/
public static PreparedStatement preparedStatement(String sql) throws SQLException{
connection();
return (PreparedStatement) conn.prepareStatement(sql);
}
/**
* 创建ResultSet对象
* @param sql
* @return
* @throws SQLException
*/
public static ResultSet resultSet(String sql) throws SQLException{
return preparedStatement(sql).executeQuery();
}
/**
* 关闭数据库连接
* @param obj
* @throws SQLException
*/
public static void close(Object obj) throws SQLException{
if(obj==null){
return;
}
if(obj instanceof ResultSet){
((ResultSet) obj).close();
}
if(obj instanceof PreparedStatement){
((PreparedStatement) obj).close();
}
if(obj instanceof Connection){
((Connection) obj).close();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
MethodAnnotation.java(servlet方法映射类)
package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 方法注解映射,映射调用的方法
* @author facebook
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface MethodAnnotation {
String method() default “”;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
BaseServlet.java(servlet基类封装,提供自定义子类继承,使用反射和注解映射不同子servlet的方法,前端实现触发不同的功能使用同一个对象的不同方法)
package web;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import annotation.MethodAnnotation;

/**
* 封装servlet的顶层操作,根据注解映射,找到对应的方法,并执行操作
* @author facebook
*
*/
public class BaseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(“text/html;charset=utf-8″);
invoke(request,response);
}

/**
* 执行指定的方法
* @param request
* @param response
* @throws IOException
*/
protected void invoke(HttpServletRequest request, HttpServletResponse response) throws IOException{
Class<?> clz = this.getClass();
int count=0;
boolean flag = true;
String method = request.getParameter(“method”);
Method[] methods = clz.getDeclaredMethods();
for(Method m : methods){
if(m.isAnnotationPresent(MethodAnnotation.class)){
MethodAnnotation methodAnnotation = m.getAnnotation(MethodAnnotation.class);
if(methodAnnotation.method().equals(method)){
try {
m.invoke(this, request,response);
} catch (Exception e) {
e.printStackTrace();
}
flag = true;
break;
}else{
flag = false;
}
}else{
count++;
}
}
if(!flag){
response.getWriter().write(“<script>alert(‘没有找到相应的操作,请查看method中的参数是否配置出错!’)</script>”);
return;
}
if(count==methods.length){
response.getWriter().write(“<script>alert(‘没有添加任何方法映射!’)</script>”);
return;
}

}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
UserServlet.java(测试servlet类)
package web;

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import annotation.MethodAnnotation;

@WebServlet(“/UserServlet”)
public class UserServlet extends BaseServlet {
private static final long serialVersionUID = 1L;

@MethodAnnotation(method=”queryList”)
public void queryList(HttpServletRequest request, HttpServletResponse response){
try {
response.getWriter().write(“<script>alert(‘你调用了查询方法!……queryList’)</script>”);
} catch (IOException e) {
e.printStackTrace();
}
}
@MethodAnnotation(method=”saveObject”)
public void saveObject(HttpServletRequest request, HttpServletResponse response){
try {
response.getWriter().write(“<script>alert(‘你调用了保存方法!……saveObject’)</script>”);
} catch (IOException e) {
e.printStackTrace();
}
}
@MethodAnnotation(method=”delObject”)
public void delObject(HttpServletRequest request, HttpServletResponse response){
try {
response.getWriter().write(“<script>alert(‘你调用了删除方法!……delObject’)</script>”);
} catch (IOException e) {
e.printStackTrace();
}
}
@MethodAnnotation(method=”findObject”)
public void findObject(HttpServletRequest request, HttpServletResponse response){
try {
response.getWriter().write(“<script>alert(‘你调用了单个对象的查询方法!…..findObject’)</script>”);
} catch (IOException e) {
e.printStackTrace();
}
}
}
———————
作者:R峰
来源:CSDN
原文:https://blog.csdn.net/renfng/article/details/69980262
版权声明:本文为博主原创文章,转载请附上博文链接!