首页 > 数据库 > MySQL >

MySQL---数据库从入门走向大神系列(十二)-构建MVC项目

2016-08-20

MySQL---数据库从入门走向大神系列(十二)-构建MVC项目。这个是对前面技术的一个小总结吧,用到的大概技术有: MVC框架,加注解,Struts框架的思想,动态代理,线程管理对象ThreadLocal,Connection对象池,Properties文件读取,EL表达式,JSTL,JavaBean等。

MySQL---数据库从入门走向大神系列(十二)-构建MVC项目。这个是对前面技术的一个小总结吧,用到的大概技术有:
MVC框架,加注解,Struts框架的思想,动态代理,线程管理对象ThreadLocal,Connection对象池,Properties文件读取,EL表达式,JSTL,JavaBean,Java访问MySQL数据库,增删改查…

其实做出来界面挺简单:
\

这里只写出一些核心的类的代码:

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 books = new ArrayList();

    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 getBooks() {
        return books;
    }

    public void setBooks(List 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 getProxy(Object srcObj, Class cls){
        Object proxiedObj = Proxy.newProxyInstance(TxProxy.class.getClassLoader(),
                            srcObj.getClass().getInterfaces(),
                            new TxProxy(srcObj));
        return (T)proxiedObj;
    }
    */
    /* 版本3
    public static T getProxy(Class 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 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},="" invocationhandler()="" {="" @override="" public="" 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为被代理的对象="" method.invoke(conn,="" args);="" });="" pool.add(="" (connection)obj);="" 把代理后的conn对象即obj加入池中="" catch="" (ioexception="" e)="" e.printstacktrace();="" (classnotfoundexception="" (sqlexception="" static="" synchronized="" getconnection()="" interruptedexception{="" 从本地线程管理对象t中拿="" con="t.get();" if(con="=null){" if(pool.size()<="0){" thread.sleep(50);="" getconnection();="" 放入到t中="" t.set(con);="" con;="" <="" pre="">
 
 
 
<h2 id="studservletjava">StudServlet.java:</h2>
 
<pre class="brush: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();="" 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="" {="" catch="" (numberformatexception="" e)="" book.setprice(price);="" book.setstud(stud);="" 继续封装stud----为stud中的books属性赋值--一方中的集合="" stud.getbooks().add(book);="" service.save(stud);="" (sqlexception="" 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("调用默认业务处理方法....");
    }
}
</booknames.length;i++){></pre>
 
 
 
<h2 id="bookdaojdbcjava">BookDAOJdbc.java:</h2>
 
 
 
<pre class="brush: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();
    }
}
</book></pre>
 
 
 
<h2 id="studdaojdbcjava">StudDAOJdbc.java</h2>
 
 
 
<pre class="brush: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值了
    }
 
}
</string,></string,></map<string,string></map<string,></map<string,></pre>
 
 
 
<h2 id="istudservicejava">IStudService.java:</h2>
 
 
 
<pre class="brush: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;
}
</string,></pre>
 
 
 
<h2 id="studservicejava">StudService.java:</h2>
 
<pre class="brush: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,完成外键字段的赋值
    }
 
}
</map<string,></pre>
 
 
 
<h2 id="jdbcproperties">jdbc.properties:</h2>
 
<pre class="brush:java;">##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</pre>
 
 
 
<h2 id="indexjsp">index.jsp:</h2>
 
 
 
<pre class="brush:java;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 
<%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
 
 
 
   
    <title>mvc示例演示</title>
   
 
   
    <br>
    学生信息查询
    <hr>
    <form action="<c:url value=&#39;/StudServlet?cmd=save&#39;/>" method="post">
        姓名:<input type="text" name="name"><br>
        <fieldset>
            <legend>图书1</legend>
            书名:<input type="text" name="bookname"><br><br>
            价格:<input type="text" name="price">
        </fieldset>
        <fieldset>
            <legend>图书2</legend>
            书名:<input type="text" name="bookname"><br><br>
            价格:<input type="text" name="price">
        </fieldset><br>
        <input type="submit" value="保存">
    </form>
    <br>
    默认请求
   
 
</pre>
 
 
 
<h2 id="showjsp">show.jsp</h2>
 
<pre class="brush:java;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 
 
 
   
    <title>学生信息查询</title>
    <style type="text/css">
        table,tr,td,th{
            border: 1px;
            border-style: solid;
            margin: auto;
        }
    </style>
   
 
   
    <h2>学生信息</h2>
    <c:foreach items="${studs}" var="stud">
        </c:foreach><table>
        <tbody><tr> <th>ID</th> <th>姓名</th></tr>
        <tr> <td>${stud.id }</td> <td>${stud.name }</td> </tr>
         
    </tbody></table>
   
 
</pre>
 
 
 
<h2 id="webxml">web.xml</h2>
 
<pre class="brush:java;"><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>    </pre>
 
<p>嗯,我就不做过多的解释啦,这个只是自己对以前知识点的一个小总结吧。</p>
 
<p>转载请附上原文博客链接: <br>
http://blog.csdn.net/qq_26525215</p>
         
    
                        </num;i++){></connection></connection></connection></connection>
相关文章
最新文章
热点推荐