这个是对前面技术的一个小总结吧,用到的大概技术有:
MVC框架,加注解,Struts框架的思想,动态代理,线程管理对象ThreadLocal,Connection对象池,Properties文件读取,EL表达式,JSTL,JavaBean,Java访问MySQL数据库,增删改查…
其实做出来界面挺简单:
完整的项目链接:
https://github.com/chenhaoxiang/Java/tree/master/myMvcWeb2
这里只写出一些核心的类的代码:
Book.java:
package cn.hncu.domain;
/*
alter table book add constraint fk_book foreign key (studid) references stud(id);
设置studid为book的外键(fk_book)为stud的主键id
*/
public class Book {
private Integer id;
private String namem;
private Double price;
// ※一对多关系中的多方(外键字段): 在写JavaBean时,要包含对方(一方)的整个值对象类型的变量
private Stud stud;//设置书的主人
public Book() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNamem() {
return namem;
}
public void setNamem(String namem) {
this.namem = namem;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Stud getStud() {
return stud;
}
public void setStud(Stud stud) {
this.stud = stud;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Book other = (Book) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
Stud.java
package cn.hncu.domain;
import java.util.ArrayList;
import java.util.List;
public class Stud {
private String id;
private String name;
//※一对多中的一方: 添加一个集合(用set也行)以表示该关系
private List<Book > books = new ArrayList<Book>();
public Stud() {
super();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Stud other = (Stud) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
//注意,如果要添加toString()方法,注意一对多关系中的信息输出嵌套
}
TxProxy.java:
package cn.hncu.pubs.tx;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import cn.hncu.pubs.ConnsUtil;
public class TxProxy implements InvocationHandler{
private Object srcObj;
public TxProxy(Object srcObj) {
this.srcObj = srcObj;
}
/* 版本1
public static Object getProxy(Object srcObj){
Object proxiedObj = Proxy.newProxyInstance(TxProxy.class.getClassLoader(),
srcObj.getClass().getInterfaces(),
new TxProxy(srcObj));
return proxiedObj;
}
*/
/* 版本2
public static<T> T getProxy(Object srcObj, Class<T> cls){
Object proxiedObj = Proxy.newProxyInstance(TxProxy.class.getClassLoader(),
srcObj.getClass().getInterfaces(),
new TxProxy(srcObj));
return (T)proxiedObj;
}
*/
/* 版本3
public static<T> T getProxy(Class<T> cls){
Object srcObj = null;
try {
srcObj = cls.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Object proxiedObj = Proxy.newProxyInstance(TxProxy.class.getClassLoader(),
srcObj.getClass().getInterfaces(),
new TxProxy(srcObj));
return (T)proxiedObj;
}
*/
//版本4
public static<T> T getProxy(T srcObj){
Object proxiedObj = Proxy.newProxyInstance(TxProxy.class.getClassLoader(),
srcObj.getClass().getInterfaces(),
new TxProxy(srcObj));
return (T)proxiedObj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
//判断method方法是不是添加了注解
if(method.isAnnotationPresent(Transaction.class)){
Connection con=null;
Object returnValue =null;
try {
con = ConnsUtil.getConnection();
con.setAutoCommit(false);
System.out.println("在动态代理中开启一个事务....");
returnValue = method.invoke(srcObj, args);
con.commit();
System.out.println("在动态代理中提交一个事务....");
} catch (Exception e) {
con.rollback();
System.out.println("在动态代理中回滚一个事务....");
}finally{
if(con!=null){
try {
con.setAutoCommit(true);
con.close();
} catch (Exception e) {
throw new RuntimeException("数据库连接关闭失败", e);
}
}
}
return returnValue;
}else{//没有添加注解,直接放行
return method.invoke(srcObj, args);
}
}
}
Transaction.java:注解
package cn.hncu.pubs.tx;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(value=ElementType.METHOD)
public @interface Transaction {
}
BaseServlet.java:
package cn.hncu.pubs;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 陈浩翔
*
* 2016-8-15
*/
public abstract class BaseServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String cmd = req.getParameter("cmd");
if(cmd==null){
cmd="execute";
}
try {
System.out.println("this:"+this);
//下面这句不能访问私有方法
//Method m = this.getClass().getMethod(cmd, HttpServletRequest.class,HttpServletResponse.class );
//如果要用类反射实现访问类的私有方法,则需要这2步,这样子类的方法就不用设置成public了。
//1获得私有方法
Method m = this.getClass().getDeclaredMethod(cmd, HttpServletRequest.class,HttpServletResponse.class );
//2设置私有方法可以被访问
m.setAccessible(true);
m.invoke(this, req,resp);//这里的this是子类对象,谁调用这里的,这个this就是谁
//下面这几句是给大家启发Struts框架的思想
// String str =(String) m.invoke(this, request,response);
// String resPage = "";//从配置struts.xml文件中读取str所对应的结果页面的地址
// request.getRequestDispatcher(resPage).forward(request,response);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
//如果没有参数cmd为null,转向的界面,由子类自己实现
public abstract void execute(HttpServletRequest request, HttpServletResponse response);
}
ConnsUtil.java
package cn.hncu.pubs;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class ConnsUtil {
private static List<Connection> pool = new ArrayList<Connection>();
private static final int NUM = 3;
private static ThreadLocal<Connection> t = new ThreadLocal<Connection>();
static{
try {
//读取配置文件
Properties p = new Properties();
p.load(ConnsUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
String driver = p.getProperty("driver");
String url = p.getProperty("url");
String user = p.getProperty("username");
String password = p.getProperty("password");
Class.forName(driver);
for(int i=0;i<NUM;i++){
final Connection conn = DriverManager.getConnection(url, user, password);
//System.out.println(conn.getClass().getClassLoader());//AppClassLoader
//使用动态代理,代理conn对象,实现对close方法的拦截
Object obj = Proxy.newProxyInstance(ConnsUtil.class.getClassLoader(),
new Class[]{Connection.class},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
//拦截close方法
if(method.getName().equalsIgnoreCase("close") &&(args==null||args.length==0) ){
pool.add((Connection)proxy);
//proxy为代理后的对象
t.set(null);//把存储在本地线程管理类中的当前线程对应的对象设为空
return null;
}
//conn为被代理的对象
return method.invoke(conn, args);
}
});
pool.add( (Connection)obj);//把代理后的conn对象即obj加入池中
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static synchronized Connection getConnection() throws InterruptedException{
//从本地线程管理对象t中拿
Connection con = t.get();
if(con==null){
if(pool.size()<=0){
Thread.sleep(50);
return getConnection();
}
con=pool.remove(0);
//放入到t中
t.set(con);
}
return con;
}
}
StudServlet.java:
package cn.hncu.stud;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.hncu.domain.Book;
import cn.hncu.domain.Stud;
import cn.hncu.pubs.BaseServlet;
import cn.hncu.pubs.tx.TxProxy;
import cn.hncu.stud.service.IStudService;
import cn.hncu.stud.service.StudService;
public class StudServlet extends BaseServlet {
//版本1
//private IStudService service = (IStudService) TxProxy.getProxy(new StudService());
//版本2
//private IStudService service = TxProxy.getProxy(new StudService(),IStudService.class);
//版本3
//private IStudService service = TxProxy.getProxy(StudService.class);//用实现类的class
//版本4
private IStudService service = TxProxy.getProxy(new StudService());//用实现类的class
private void save(HttpServletRequest request, HttpServletResponse response) throws InterruptedException, ServletException, IOException {
//收集并封装stud
String name = request.getParameter("name");
if(name==null||name.trim().length()<=0){
response.sendRedirect("index.jsp");//重定向
return ;
}
Stud stud = new Stud();
stud.setName(name);
//收集并封装所有book
String bookNames[] = request.getParameterValues("bookname");//获取多个值
String prices[] = request.getParameterValues("price");
if(bookNames!=null){
for(int i=0;i<bookNames.length;i++){
Book book = new Book();
if(bookNames[i]==null || bookNames[i].trim().length()<=0){
continue;
}
book.setNamem(bookNames[i]);
if(prices[i]!=null && prices[i].trim().length()!=0 ){
Double price=0.0;
try {
price = Double.parseDouble(prices[i]);
} catch (NumberFormatException e) {
price=0.0;
}
book.setPrice(price);
}
book.setStud(stud);
//继续封装stud----为stud中的books属性赋值--一方中的集合
stud.getBooks().add(book);
}
}
try {
service.save(stud);
} catch (SQLException e) {
e.printStackTrace();
}
}
private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Map<String, String>> studs = service.query();
request.setAttribute("studs", studs);
request.getRequestDispatcher("/jsps/show.jsp").forward(request, response);
}
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
System.out.println("调用默认业务处理方法....");
}
}
BookDAOJdbc.java:
package cn.hncu.stud.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import cn.hncu.domain.Book;
import cn.hncu.pubs.ConnsUtil;
public class BookDAOJdbc implements IBookDAO {
@Override
public void save(List<Book> books) throws InterruptedException, SQLException {
Connection con = ConnsUtil.getConnection();
String sql = "insert into book(name,price,studid) values(?,?,?)";
PreparedStatement pst = con.prepareStatement(sql);
for(Book book:books){
pst.setString(1, book.getNamem());
pst.setDouble(2, book.getPrice());
//stud中设置了uuid这里才能设置成功
pst.setString(3, book.getStud().getId());
pst.addBatch();
}
pst.executeBatch();
}
}
StudDAOJdbc.java
package cn.hncu.stud.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import cn.hncu.domain.Stud;
import cn.hncu.pubs.ConnsUtil;
public class StudDAOJdbc implements StudDAO {
@Override
public List<Map<String, String>> query() {
List<Map<String, String>> list = new ArrayList<Map<String,String>>();
Connection con = null;
try {
con = ConnsUtil.getConnection();
String sql="select * from stud";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
Map<String, String> map = new HashMap<String, String>();
map.put("id", rs.getString("id"));
map.put("name", rs.getString("name"));
//一个map就是一条记录(数据行)
list.add(map);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(con!=null){
try {
con.close();
} catch (SQLException e) {
throw new RuntimeException("数据库连接关闭失败", e);
}
}
}
return list;
}
@Override
public void save(Stud stud) throws InterruptedException, SQLException {
Connection con = ConnsUtil.getConnection();
String sql = "insert into stud values(?,?)";
PreparedStatement pst = con.prepareStatement(sql);
String uuid = UUID.randomUUID().toString().replaceAll("-","");
pst.setString(1, uuid);
pst.setString(2, stud.getName());
pst.executeUpdate();
//在这里为book补外键字段的值
stud.setId(uuid);
//这一句执行在前,这样在BookDAOJdbc中会调用book.getStud().getId()就可以拿到该id值了
}
}
IStudService.java:
package cn.hncu.stud.service;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import cn.hncu.domain.Stud;
import cn.hncu.pubs.tx.Transaction;
public interface IStudService {
public abstract List< Map<String, String> > query();
@Transaction//注意,注解必须加在接口上,加在实现类上是无效的!,因为我们的动态代理是面向接口的
public abstract void save(Stud stud) throws InterruptedException, SQLException;
}
StudService.java:
package cn.hncu.stud.service;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import cn.hncu.domain.Stud;
import cn.hncu.stud.dao.BookDAOJdbc;
import cn.hncu.stud.dao.IBookDAO;
import cn.hncu.stud.dao.StudDAO;
import cn.hncu.stud.dao.StudDAOJdbc;
public class StudService implements IStudService {
//注入
private StudDAO studDao = new StudDAOJdbc();
private IBookDAO bookDao = new BookDAOJdbc();
@Override
public List<Map<String, String>> query() {
return studDao.query();
}
//※利用动态代理在背后帮忙实现事务功能,注意:该方法内部的异常必须抛出来给动态代理捕捉处理
@Override
public void save(Stud stud) throws InterruptedException, SQLException {
studDao.save(stud);//在这个内部为stud对象补id--先执行
bookDao.save(stud.getBooks());//通过book拿到stud对象,进而拿到studid,完成外键字段的赋值
}
}
jdbc.properties:
##MySQL
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=utf-8
username=root
password=1234
##Oracle
#driver=oracle.jdbc.driver.OracleDriver
#url=jdbc:oracle:thin:@localhost:1521:orcl
#username=scott
#password=tiger
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>mvc示例演示</title>
</head>
<body>
<br/>
<a href='<c:url value="/StudServlet?cmd=query"></c:url>'>学生信息查询</a>
<hr/>
<form action="<c:url value='/StudServlet?cmd=save'/>" method="post">
姓名:<input type="text" name="name" /><br/>
<fieldset style="width: 200px">
<legend>图书1</legend>
书名:<input type="text" name="bookname" /><br/><br/>
价格:<input type="text" name="price" />
</fieldset>
<fieldset style="width: 200px">
<legend>图书2</legend>
书名:<input type="text" name="bookname" /><br/><br/>
价格:<input type="text" name="price" />
</fieldset><br/>
<input type="submit" value="保存">
</form>
<br/>
<a href="<c:url value='/StudServlet'/>">默认请求</a>
</body>
</html>
show.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>学生信息查询</title>
<style type="text/css">
table,tr,td,th{
border: 1px;
border-style: solid;
margin: auto;
}
</style>
</head>
<body style="text-align: center;">
<h2>学生信息</h2>
<table>
<tr> <th>ID</th> <th>姓名</th></tr>
<c:forEach items="${studs}" var="stud">
<tr> <td>${stud.id }</td> <td>${stud.name }</td> </tr>
</c:forEach>
</table>
</body>
</html>
web.xml
<servlet>
<servlet-name>StudServlet</servlet-name>
<servlet-class>cn.hncu.stud.StudServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StudServlet</servlet-name>
<url-pattern>/StudServlet</url-pattern>
</servlet-mapping>
嗯,我就不做过多的解释啦,这个只是自己对以前知识点的一个小总结吧。
转载请附上原文博客链接:
http://blog.csdn.net/qq_26525215