【mybatis】1、入门CURD基本操作(环境搭建)

#1、基本环境

环境 版本
jdk 1.7.0_10
ide eclipse-jee-luna-SR2-win32-x86_64
maven 3.3.3
mybatis 3.2.7
mysql 5.1.55

#2、所需jar包

maven的pom.xml

隐藏行号 复制代码 ? pom.xml
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3.     <modelVersion>4.0.0</modelVersion>
  4. 
    
  5.     <groupId>cn.kiwifly.mybatis</groupId>
  6.     <artifactId>mybatis_test</artifactId>
  7.     <version>0.0.1-SNAPSHOT</version>
  8.     <packaging>jar</packaging>
  9. 
    
  10.     <name>mybatis_test</name>
  11. 
    
  12. 
    
  13.     <properties>
  14.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15.     </properties>
  16. 
    
  17. 
    
  18. 
    
  19.     <dependencies>
  20. 
    
  21.         <dependency>
  22.             <groupId>ognl</groupId>
  23.             <artifactId>ognl</artifactId>
  24.             <version>2.6.9</version>
  25.             <scope>provided</scope>
  26. 
    
  27.         </dependency>
  28.         <dependency>
  29.             <groupId>org.slf4j</groupId>
  30.             <artifactId>slf4j-api</artifactId>
  31.             <version>1.7.5</version>
  32. 
    
  33.         </dependency>
  34.         <dependency>
  35.             <groupId>org.slf4j</groupId>
  36.             <artifactId>slf4j-log4j12</artifactId>
  37.             <version>1.7.5</version>
  38. 
    
  39.         </dependency>
  40.         <dependency>
  41.             <groupId>log4j</groupId>
  42.             <artifactId>log4j</artifactId>
  43.             <version>1.2.17</version>
  44. 
    
  45.         </dependency>
  46.         <dependency>
  47.             <groupId>org.apache.logging.log4j</groupId>
  48.             <artifactId>log4j-core</artifactId>
  49.             <version>2.0-rc1</version>
  50. 
    
  51.         </dependency>
  52.         <dependency>
  53.             <groupId>commons-logging</groupId>
  54.             <artifactId>commons-logging</artifactId>
  55.             <version>1.1.1</version>
  56. 
    
  57.         </dependency>
  58.         <dependency>
  59.             <groupId>org.javassist</groupId>
  60.             <artifactId>javassist</artifactId>
  61.             <version>3.17.1-GA</version>
  62. 
    
  63.         </dependency>
  64.         <dependency>
  65.             <groupId>cglib</groupId>
  66.             <artifactId>cglib</artifactId>
  67.             <version>2.2.2</version>
  68. 
    
  69.         </dependency>
  70.         <dependency>
  71.             <groupId>org.mybatis</groupId>
  72.             <artifactId>mybatis</artifactId>
  73.             <version>3.2.7</version>
  74.         </dependency>
  75. 
    
  76.         <dependency>
  77.             <groupId>mysql</groupId>
  78.             <artifactId>mysql-connector-java</artifactId>
  79.             <version>5.1.22</version>
  80.         </dependency>
  81. 
    
  82.         <dependency>
  83.             <groupId>junit</groupId>
  84.             <artifactId>junit</artifactId>
  85.             <version>3.8.1</version>
  86.             <scope>test</scope>
  87.         </dependency>
  88.     </dependencies>
  89. </project>

.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}
.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}
.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}
.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}
.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}
.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}
.number_show{ padding-left:52px !important; list-style:decimal outside !important}
.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}
.number_hide{ padding-left:0px !important; list-style-type:none !important}
.number_hide li{ list-style-type:none !important; border-left:0px}
ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}
ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}
.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}
ol.mainarea li pre.alt{ background-color:#f7f7ff !important}

#3、初始化sql

##a、建表sql

隐藏行号 复制代码 ? sql_table.sql
  1. /*
    
  2. SQLyog v10.2
    
  3. MySQL - 5.1.72-community : Database - mybatis
    
  4. *********************************************************************
    
  5. */
    
  6. 
    
  7. 
    
  8. /*!40101 SET NAMES utf8 */;
    
  9. 
    
  10. /*!40101 SET SQL_MODE=''*/;
    
  11. 
    
  12. /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
    
  13. /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
    
  14. /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
    
  15. /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
    
  16. /*Table structure for table `items` */
    
  17. 
    
  18. CREATE TABLE `items` (
    
  19.   `id` int(11) NOT NULL AUTO_INCREMENT,
    
  20.   `name` varchar(32) NOT NULL COMMENT '商品名称',
    
  21.   `price` float(10,1) NOT NULL COMMENT '商品定价',
    
  22.   `detail` text COMMENT '商品描述',
    
  23.   `pic` varchar(64) DEFAULT NULL COMMENT '商品图片',
    
  24.   `createtime` datetime NOT NULL COMMENT '生产日期',
    
  25.   PRIMARY KEY (`id`)
    
  26. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
    
  27. 
    
  28. /*Table structure for table `orderdetail` */
    
  29. 
    
  30. CREATE TABLE `orderdetail` (
    
  31.   `id` int(11) NOT NULL AUTO_INCREMENT,
    
  32.   `orders_id` int(11) NOT NULL COMMENT '订单id',
    
  33.   `items_id` int(11) NOT NULL COMMENT '商品id',
    
  34.   `items_num` int(11) DEFAULT NULL COMMENT '商品购买数量',
    
  35.   PRIMARY KEY (`id`),
    
  36.   KEY `FK_orderdetail_1` (`orders_id`),
    
  37.   KEY `FK_orderdetail_2` (`items_id`),
    
  38.   CONSTRAINT `FK_orderdetail_1` FOREIGN KEY (`orders_id`) REFERENCES `orders` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
    
  39.   CONSTRAINT `FK_orderdetail_2` FOREIGN KEY (`items_id`) REFERENCES `items` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
    
  40. ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
    
  41. 
    
  42. /*Table structure for table `orders` */
    
  43. 
    
  44. CREATE TABLE `orders` (
    
  45.   `id` int(11) NOT NULL AUTO_INCREMENT,
    
  46.   `user_id` int(11) NOT NULL COMMENT '下单用户id',
    
  47.   `number` varchar(32) NOT NULL COMMENT '订单号',
    
  48.   `createtime` datetime NOT NULL COMMENT '创建订单时间',
    
  49.   `note` varchar(100) DEFAULT NULL COMMENT '备注',
    
  50.   PRIMARY KEY (`id`),
    
  51.   KEY `FK_orders_1` (`user_id`),
    
  52.   CONSTRAINT `FK_orders_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
    
  53. ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
    
  54. 
    
  55. /*Table structure for table `user` */
    
  56. 
    
  57. CREATE TABLE `user` (
    
  58.   `id` int(11) NOT NULL AUTO_INCREMENT,
    
  59.   `username` varchar(32) NOT NULL COMMENT '用户名称',
    
  60.   `birthday` date DEFAULT NULL COMMENT '生日',
    
  61.   `sex` char(1) DEFAULT NULL COMMENT '性别',
    
  62.   `address` varchar(256) DEFAULT NULL COMMENT '地址',
    
  63.   PRIMARY KEY (`id`)
    
  64. ) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;
    
  65. 
    
  66. /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
    
  67. /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
    
  68. /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
    
  69. /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
    

.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}
.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}
.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}
.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}
.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}
.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}
.number_show{ padding-left:52px !important; list-style:decimal outside !important}
.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}
.number_hide{ padding-left:0px !important; list-style-type:none !important}
.number_hide li{ list-style-type:none !important; border-left:0px}
ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}
ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}
.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}
ol.mainarea li pre.alt{ background-color:#f7f7ff !important}

##b、初始化数据sql

隐藏行号 复制代码 ? sql_data.sql
  1. /*
    
  2. SQLyog v10.2
    
  3. MySQL - 5.1.72-community : Database - mybatis
    
  4. *********************************************************************
    
  5. */
    
  6. 
    
  7. 
    
  8. /*!40101 SET NAMES utf8 */;
    
  9. 
    
  10. /*!40101 SET SQL_MODE=''*/;
    
  11. 
    
  12. /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
    
  13. /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
    
  14. /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
    
  15. /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
    
  16. /*Data for the table `items` */
    
  17. 
    
  18. insert  into `items`(`id`,`name`,`price`,`detail`,`pic`,`createtime`) values (1,'台式机',3000.0,'该电脑质量非常好!!!!',NULL,'2015-02-03 13:22:53'),(2,'笔记本',6000.0,'笔记本性能好,质量好!!!!!',NULL,'2015-02-09 13:22:57'),(3,'背包',200.0,'名牌背包,容量大质量好!!!!',NULL,'2015-02-06 13:23:02');
    
  19. 
    
  20. /*Data for the table `orderdetail` */
    
  21. 
    
  22. insert  into `orderdetail`(`id`,`orders_id`,`items_id`,`items_num`) values (1,3,1,1),(2,3,2,3),(3,4,3,4),(4,4,2,3);
    
  23. 
    
  24. /*Data for the table `orders` */
    
  25. 
    
  26. insert  into `orders`(`id`,`user_id`,`number`,`createtime`,`note`) values (3,1,'1000010','2015-02-04 13:22:35',NULL),(4,1,'1000011','2015-02-03 13:22:41',NULL),(5,10,'1000012','2015-02-12 16:13:23',NULL);
    
  27. 
    
  28. /*Data for the table `user` */
    
  29. 
    
  30. insert  into `user`(`id`,`username`,`birthday`,`sex`,`address`) values (1,'王五',NULL,'2',NULL),(10,'张三','2014-07-10','1','北京市'),(16,'张小明',NULL,'1','河南郑州'),(22,'陈小明',NULL,'1','河南郑州'),(24,'张三丰',NULL,'1','河南郑州'),(25,'陈小明',NULL,'1','河南郑州'),(26,'王五',NULL,NULL,NULL);
    
  31. 
    
  32. /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
    
  33. /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
    
  34. /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
    
  35. /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
    

.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}
.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}
.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}
.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}
.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}
.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}
.number_show{ padding-left:52px !important; list-style:decimal outside !important}
.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}
.number_hide{ padding-left:0px !important; list-style-type:none !important}
.number_hide li{ list-style-type:none !important; border-left:0px}
ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}
ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}
.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}
ol.mainarea li pre.alt{ background-color:#f7f7ff !important}

#4、成功结果

【mybatis】1、入门CURD基本操作(环境搭建)

KIWI 标签: mybatissqlcurd入门
上一篇:Host 'XXX' is not allowed to connect to this MySQL server 解决方案/如何开启MySQL的远程帐号


下一篇:spring boot 1.视图解析器,2.开启静态资源访问