这篇博客其实很早就应该写,早在半年前,因为对SpringMVC感兴趣,便自学了一下Spring。一段时间的学习后,对Spring有了一个基本的了解,于是想着自己动手搭建一个SpringMvc的框架出来。搭建的过程中遇到了很多的问题,其实在网上有许多的SpringMvc的框架搭建教程,但使用那些教程搭建起来的框架往往有或多或少的问题,如jar包下载不完全、log日志无法使用等,经历了一段时间的尝试后终于将框架搭建了起来。
在搭建起来后就想着自己写一篇博客记录一下,以便日后需要的时候方便查找。
在这里,我使用的工具有:maven3.3.3、Eclipse luna、mysql 5、tomcat 8.0,使用的Spring为4.2版本
由于在框架的搭建过程中会使用到一些数据,所以在搭建框架前,首先将数据库设计好。下面是我从网上获取的一个数据库文件,将其导入mysql数据库中即可:
-- MySQL dump 10.13 Distrib 5.6.25, for Win64 (x86_64) -- -- Host: localhost Database: ebuy -- ------------------------------------------------------ -- Server version 5.6.25 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Table structure for table `firstsort` -- DROP TABLE IF EXISTS `firstsort`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `firstsort` ( `fid` int(11) NOT NULL AUTO_INCREMENT, `fname` varchar(20) NOT NULL, PRIMARY KEY (`fid`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `firstsort` -- LOCK TABLES `firstsort` WRITE; /*!40000 ALTER TABLE `firstsort` DISABLE KEYS */; /*!40000 ALTER TABLE `firstsort` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `goods` -- DROP TABLE IF EXISTS `goods`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `goods` ( `gid` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `description` varchar(50) NOT NULL, `image` varchar(100) NOT NULL, `alive` varchar(2) DEFAULT NULL, `recommend` varchar(1) DEFAULT NULL, `addtime` date DEFAULT NULL, `sid` int(11) NOT NULL, PRIMARY KEY (`gid`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `goods` -- LOCK TABLES `goods` WRITE; /*!40000 ALTER TABLE `goods` DISABLE KEYS */; /*!40000 ALTER TABLE `goods` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `leaveword` -- DROP TABLE IF EXISTS `leaveword`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `leaveword` ( `lid` int(11) NOT NULL AUTO_INCREMENT, `message` varchar(100) NOT NULL, `name` varchar(20) NOT NULL, `riqi` date NOT NULL, `gid` int(11) NOT NULL, PRIMARY KEY (`lid`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `leaveword` -- LOCK TABLES `leaveword` WRITE; /*!40000 ALTER TABLE `leaveword` DISABLE KEYS */; /*!40000 ALTER TABLE `leaveword` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `manager` -- DROP TABLE IF EXISTS `manager`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `manager` ( `mid` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `password` varchar(20) NOT NULL, PRIMARY KEY (`mid`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `manager` -- LOCK TABLES `manager` WRITE; /*!40000 ALTER TABLE `manager` DISABLE KEYS */; INSERT INTO `manager` VALUES (1,'superadmin','666666'); /*!40000 ALTER TABLE `manager` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `middle` -- DROP TABLE IF EXISTS `middle`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `middle` ( `mid` int(11) NOT NULL, `pid` int(11) NOT NULL, PRIMARY KEY (`mid`,`pid`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `middle` -- LOCK TABLES `middle` WRITE; /*!40000 ALTER TABLE `middle` DISABLE KEYS */; INSERT INTO `middle` VALUES (1,1),(1,2),(1,3),(1,4),(1,5); /*!40000 ALTER TABLE `middle` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `news` -- DROP TABLE IF EXISTS `news`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `news` ( `nid` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(50) NOT NULL, `content` varchar(1000) NOT NULL, `riqi` date NOT NULL, PRIMARY KEY (`nid`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=gbk; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `news` -- LOCK TABLES `news` WRITE; /*!40000 ALTER TABLE `news` DISABLE KEYS */; INSERT INTO `news` VALUES (1,'索爱W8 真正的Walkman Android音乐手机','一直传闻索爱发布Walkman加上Android的手机,这次终于成真了--索爱W8正式发布。索爱W8不但是Walkman品牌的音乐手机,也是Android 2.1版系统智能手机,提供有蓝色、红色、橙色三种颜色。','2011-04-23'),(2,'尼康D5100 新一代可翻转LCD的中端数码单反','虽然日本地震影响了不少3C厂商的新品发布计划,但尼康最近依然推出最新一代中端数码单反D5100。这款尼康D5100十分给力,带有3英寸的可翻转的大屏幕LCD,若含AF-S DX Zoom-NIKKOR 18-55mm f/3.5-5.6G ED VR镜头,套机售价约899美元。','2011-04-13'),(3,'iPad降价!价格暴跌到2888元','随着iPad 2的上市,旧款iPad开始降价促销,清理库存。虽然,iPad 2在国内上市时间没有公布,或许遇到某些问题,也因此未来较长时间还是只会继续销售iPad了。但庆幸的是,iPad降价没有漏到,iPad一代WIFI 16GB版本价格暴跌到2888元人民币,即便是3G版本的iPad价格也不过是3888元人民币,简直就是横扫千军,相信就算是旧款iPad,也会买断市了。','2011-03-03'),(4,'NEC N-04C 7.7mm的全球最薄手机','NEC最近在日本发布了一款拥有全球最薄称号的手机,型号就是NEC N-04C,这款全球最薄手机拥有7.7mm的机身,而且是预装Andorid操作系统,相信凭着如此性感的身材,必然能俘获不少的时尚粉丝。','2011-02-20'),(5,'摩托罗拉XOOM 首款平板电脑登场','摩托罗拉最近正式推出了首款平板电脑“MOTO XOOM”,一发布就获得了媒体极大的聚焦。作为摩托罗拉的第一款平板电脑,摩托罗拉XOOM预装了最新的Android 3.0操作系统,3G/4G版本售价为799美元,而WiFi版本,不支持3G网络的Xoom售价为600美元。','2011-03-03'),(6,'创新ZEN Touch 2 内置Android系统的播放器','Android系统越来越受追捧,创新就将Android系统引入到旗下的播放器中,推出了新款创新ZEN Touch 2,就是采用了Android 2.1系统,有8GB和16GB容量,售价约1600元','2010-12-10'); /*!40000 ALTER TABLE `news` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `orders` -- DROP TABLE IF EXISTS `orders`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `orders` ( `oid` int(11) NOT NULL, `ostate` varchar(1) NOT NULL, `name` varchar(20) NOT NULL, `address` varchar(50) NOT NULL, `tel` varchar(20) NOT NULL, `paytype` varchar(8) NOT NULL, `riqi` date NOT NULL, `money` double(10,3) NOT NULL, `userid` int(11) NOT NULL, PRIMARY KEY (`oid`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `orders` -- LOCK TABLES `orders` WRITE; /*!40000 ALTER TABLE `orders` DISABLE KEYS */; /*!40000 ALTER TABLE `orders` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `placard` -- DROP TABLE IF EXISTS `placard`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `placard` ( `placardid` int(11) NOT NULL AUTO_INCREMENT, `messages` varchar(300) NOT NULL, `riqi` date NOT NULL, PRIMARY KEY (`placardid`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `placard` -- LOCK TABLES `placard` WRITE; /*!40000 ALTER TABLE `placard` DISABLE KEYS */; /*!40000 ALTER TABLE `placard` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `purview` -- DROP TABLE IF EXISTS `purview`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `purview` ( `pid` int(11) NOT NULL AUTO_INCREMENT, `purview` char(1) NOT NULL, PRIMARY KEY (`pid`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=gbk; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `purview` -- LOCK TABLES `purview` WRITE; /*!40000 ALTER TABLE `purview` DISABLE KEYS */; INSERT INTO `purview` VALUES (1,'1'),(2,'2'),(3,'3'),(4,'4'),(5,'5'); /*!40000 ALTER TABLE `purview` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `sale` -- DROP TABLE IF EXISTS `sale`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `sale` ( `saleid` int(11) NOT NULL AUTO_INCREMENT, `gid` int(11) NOT NULL, `scount` int(11) NOT NULL, `oid` int(11) NOT NULL, `saleprice` double(10,3) NOT NULL, `state` char(1) NOT NULL, `riqi` date NOT NULL, PRIMARY KEY (`saleid`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `sale` -- LOCK TABLES `sale` WRITE; /*!40000 ALTER TABLE `sale` DISABLE KEYS */; /*!40000 ALTER TABLE `sale` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `secondsort` -- DROP TABLE IF EXISTS `secondsort`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `secondsort` ( `sid` int(11) NOT NULL AUTO_INCREMENT, `sname` varchar(20) NOT NULL, `fid` int(11) NOT NULL, PRIMARY KEY (`sid`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `secondsort` -- LOCK TABLES `secondsort` WRITE; /*!40000 ALTER TABLE `secondsort` DISABLE KEYS */; /*!40000 ALTER TABLE `secondsort` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `storage` -- DROP TABLE IF EXISTS `storage`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `storage` ( `storageid` int(11) NOT NULL AUTO_INCREMENT, `gid` int(11) NOT NULL, `inprice` double(10,3) NOT NULL, `outprice` double(10,3) NOT NULL, `marketprice` double(10,3) NOT NULL, `count` int(11) NOT NULL, PRIMARY KEY (`storageid`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `storage` -- LOCK TABLES `storage` WRITE; /*!40000 ALTER TABLE `storage` DISABLE KEYS */; /*!40000 ALTER TABLE `storage` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `transfer` -- DROP TABLE IF EXISTS `transfer`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `transfer` ( `tid` int(11) NOT NULL AUTO_INCREMENT, `account` char(19) NOT NULL, `password` char(6) NOT NULL, `balance` double(10,3) NOT NULL, PRIMARY KEY (`tid`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=gbk; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `transfer` -- LOCK TABLES `transfer` WRITE; /*!40000 ALTER TABLE `transfer` DISABLE KEYS */; INSERT INTO `transfer` VALUES (1,'1234567895841220','666666',10000.000),(2,'123456789520','666666',10000.000),(3,'123456520','666666',10000.000); /*!40000 ALTER TABLE `transfer` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `userinfo` -- DROP TABLE IF EXISTS `userinfo`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `userinfo` ( `userid` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `password` varchar(20) NOT NULL, `question` varchar(30) NOT NULL, `answer` varchar(30) NOT NULL, `realname` varchar(20) DEFAULT NULL, `sex` varchar(2) DEFAULT NULL, `email` varchar(20) NOT NULL, `id` char(18) NOT NULL, `postalcode` varchar(6) DEFAULT NULL, `address` varchar(30) DEFAULT NULL, `tel` varchar(11) DEFAULT NULL, `score` double(10,3) DEFAULT NULL, PRIMARY KEY (`userid`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `userinfo` -- LOCK TABLES `userinfo` WRITE; /*!40000 ALTER TABLE `userinfo` DISABLE KEYS */; INSERT INTO `userinfo` VALUES (1,'ppp','123456','qqqqq','ddddd','rrrr','男','123@QQ.com','dd','dfdf','eeee','1234567',123.100); /*!40000 ALTER TABLE `userinfo` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -- Dump completed on 2016-01-27 22:18:32
在该项目中,我是用的持久化工具是mybatis,而mybatis有一个工具(mybatis-generator-core-1.3.2),可以自动将数据库中的表映射成相对应的dao、model、model-db xml文件。
在使用该工具时,需要配置一份xml文件(generator.xml)以及一个数据库的驱动jar包
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!-- 数据库驱动包位置 --> <classPathEntry location="E:\JavaSmallTools\myBi\mysql-connector-java-5.1.26-bin.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true" /> </commentGenerator> <!-- 数据库链接URL、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/ebuy" userId="root" password="123456"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="com.welv.model" targetProject="E:\JavaSmallTools\myBi\com"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成的映射文件包名和位置 --> <sqlMapGenerator targetPackage="com.welv.mapping" targetProject="E:\JavaSmallTools\myBi\com"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO的包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.welv.dao" targetProject="E:\JavaSmallTools\myBi\com"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 数据库中需要生成java代码的表的映射关系 --> <table tableName="firstsort" domainObjectName="Firstsort" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="goods" domainObjectName="Goods" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="leaveword" domainObjectName="Leaveword" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="manager" domainObjectName="Manager" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="middle" domainObjectName="Middle" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="news" domainObjectName="News" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="orders" domainObjectName="Orders" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="placard" domainObjectName="Placard" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="purview" domainObjectName="Purview" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="sale" domainObjectName="Sale" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="secondsort" domainObjectName="Secondsort" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="transfer" domainObjectName="Transfer" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="userinfo" domainObjectName="Userinfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="storage" domainObjectName="Storage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
在该文件中,主要分为5部分,每部分的内容基本上在文件中都有说明,值得注意的是第五部分,也就是各个表的映射关系部分中,将大部分的值都赋false,其原因是,如果为true则会产生大量的无用文件,以及有用文件中产生大量的无用代码,全部使用false有利于generate clean code,便于后期的维护。
在修改完generator.xml文件后,将文件和工具(mybatis-generator-core-1.3.2)放在同一目录下。使用cmd命令,cd到该文件夹下,输入命令:
java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
命令执行完后,打开预先设置好的文件夹,可看到一下目录:
从该目录中可看出,dao、mapping、model都已经生成,并且通过该工具生成的代码,其依赖关系也已经关联好,不需要在修改。
值得注意的是,通过该方式生成的dao文件均为interface,即接口类型。而具体的sql实现均在mapping的xml文件中实现,并且在生成的dao中只包含基本的增删改查,所以很多的方法需要自己添加,而添加新的方法需要在dao和mapping中同时进行修改。
至此,基本的持久化层代码已经生成,留在以便待用,接下去便可以开始搭建框架了。