Loading

【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.     <groupId>cn.kiwifly.mybatis</groupId>
  5.     <artifactId>mybatis_test</artifactId>
  6.     <version>0.0.1-SNAPSHOT</version>
  7.     <packaging>jar</packaging>
  8.     <name>mybatis_test</name>
  9.     <properties>
  10.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  11.     </properties>
  12.     <dependencies>
  13.         <dependency>
  14.             <groupId>ognl</groupId>
  15.             <artifactId>ognl</artifactId>
  16.             <version>2.6.9</version>
  17.             <scope>provided</scope>
  18.         </dependency>
  19.         <dependency>
  20.             <groupId>org.slf4j</groupId>
  21.             <artifactId>slf4j-api</artifactId>
  22.             <version>1.7.5</version>
  23.         </dependency>
  24.         <dependency>
  25.             <groupId>org.slf4j</groupId>
  26.             <artifactId>slf4j-log4j12</artifactId>
  27.             <version>1.7.5</version>
  28.         </dependency>
  29.         <dependency>
  30.             <groupId>log4j</groupId>
  31.             <artifactId>log4j</artifactId>
  32.             <version>1.2.17</version>
  33.         </dependency>
  34.         <dependency>
  35.             <groupId>org.apache.logging.log4j</groupId>
  36.             <artifactId>log4j-core</artifactId>
  37.             <version>2.0-rc1</version>
  38.         </dependency>
  39.         <dependency>
  40.             <groupId>commons-logging</groupId>
  41.             <artifactId>commons-logging</artifactId>
  42.             <version>1.1.1</version>
  43.         </dependency>
  44.         <dependency>
  45.             <groupId>org.javassist</groupId>
  46.             <artifactId>javassist</artifactId>
  47.             <version>3.17.1-GA</version>
  48.         </dependency>
  49.         <dependency>
  50.             <groupId>cglib</groupId>
  51.             <artifactId>cglib</artifactId>
  52.             <version>2.2.2</version>
  53.         </dependency>
  54.         <dependency>
  55.             <groupId>org.mybatis</groupId>
  56.             <artifactId>mybatis</artifactId>
  57.             <version>3.2.7</version>
  58.         </dependency>
  59.         <dependency>
  60.             <groupId>mysql</groupId>
  61.             <artifactId>mysql-connector-java</artifactId>
  62.             <version>5.1.22</version>
  63.         </dependency>
  64.         <dependency>
  65.             <groupId>junit</groupId>
  66.             <artifactId>junit</artifactId>
  67.             <version>3.8.1</version>
  68.             <scope>test</scope>
  69.         </dependency>
  70.     </dependencies>
  71. </project>

 

#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 */;
    

##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 */;
    

 

#4、成功结果

QQ截图20150827120605

KIWI 标签:
posted @ 2015-08-27 14:02  KIWI的碎碎念  阅读(400)  评论(0编辑  收藏  举报