欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mybatis入门

程序员文章站 2022-04-22 10:36:14
...

maven项目+MyBatis入门

第一步:在maven项目中导入MyBatis相应的jar包

<!-- 数据库的依赖  -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.30</version>
</dependency>
 
 <!-- mybatis 依赖  -->
 <dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.3.0</version>   
 </dependency>

第二步:数据库中表的建立

打开mysql数据库图形界面工具,点击新建查询,然后复制下面代码点击运行,然后刷新数据库,可以看到多了一个叫做sxt的数据库,里面有一张heros表。打开表里面有英雄联盟部分英雄的数据。(如果刷新后没有,可能是数据库已经更新了,而图形界面管理工具还没更新,重新打开工具或者重新连接数据库就能看到了)

/*
SQLyog Enterprise - MySQL GUI v8.12 
MySQL - 5.5.15 : Database - cslg
*********************************************************************
*/


/*!40101 SET NAMES utf8 */;

/*!40101 SET SQL_MODE=''*/;

/*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @[email protected]@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;

CREATE DATABASE /*!32312 IF NOT EXISTS*/`sxt` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `sxt`;

/*Table structure for table `heros` */

DROP TABLE IF EXISTS `heros`;

CREATE TABLE `heros` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `nickname` varchar(20) NOT NULL,
  `sex` char(1) NOT NULL,
  `first` varchar(10) DEFAULT NULL,
  `second` varchar(10) DEFAULT NULL,
  `Q` varchar(50) DEFAULT NULL,
  `W` varchar(50) DEFAULT NULL,
  `E` varchar(50) DEFAULT NULL,
  `R` varchar(50) DEFAULT NULL,
  `passivity` varchar(50) DEFAULT NULL,
  `img` varchar(20) DEFAULT NULL,
  `birthday` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=133 DEFAULT CHARSET=utf8;

/*Data for the table `heros` */

insert  into `heros`(`id`,`name`,`nickname`,`sex`,`first`,`second`,`Q`,`W`,`E`,`R`,`passivity`,`img`,`birthday`) values (1,'安妮','黑暗之女','0','法师','','碎裂之火','焚烧','熔岩护盾','提伯斯之怒','嗜火','Annie.png','0000-00-00'),(2,'亚托克斯','暗裔剑魔','1','战士','坦克','黑暗之跃','血之渴望/血之报偿','痛苦利刃','浴血屠戮','鲜血魔井','Aatrox.png','2015-10-10'),(3,'布里茨','蒸汽机器人','1','坦克','战士','机械飞爪','过载运转','能量铁拳','静电力场','法力屏障','Blitzcrank.png','0000-00-00'),(4,'蒙多医生','祖安狂人','1','战士','坦克','病毒屠刀','痛苦燃烧','潜能激发','背水一战','肾上腺激素','DrMundo.png','0000-00-00'),(5,'盖伦','德玛西亚之力','1','战士','坦克','致命打击','勇气','审判','德玛西亚正义','坚韧','Garen.png','0000-00-00'),(6,'贾克斯','武器大师','1','战士','刺客','跳斩','蓄力一击','反击风暴','宗师之威','无情连打','Jax.png','0000-00-00'),(7,'凯南','狂暴之心','1','法师','射手','奥义!千鸟','奥义!电刃','奥义!雷铠','秘奥义!万雷天牢引','忍法!雷缚印','Kennen.png','0000-00-00'),(8,'拉克丝','光辉女郎','0','法师','辅助','光之束缚','曲光屏障','透光奇点','终极闪光','光芒四射','Lux.png','0000-00-00'),(9,'内瑟斯','沙漠死神','1','战士','坦克','汲魂痛击','枯萎','灵魂烈焰','死神降临','吞噬灵魂','Nasus.png','0000-00-00'),(10,'拉莫斯','披甲龙龟','1','坦克','战士','动力冲刺','尖刺防御','破甲嘲讽','地动山摇','锥刺甲壳','Rammus.png','0000-00-00'),(11,'希瓦娜','龙血武姬','0','战士','坦克','龙牙突刺','烈火燎原','烈焰吐息','魔龙降世','龙族血统','Shyvana.png','0000-00-00'),(12,'泰隆','刀锋之影','1','刺客','战士','刺客诡道','刺斩草除根','割喉之战','暗影突袭','怜悯之心','Talon.png','0000-00-00'),(13,'殇之木乃伊','阿木木','1','坦克','法师','绷带牵引','绝望光环','阿木木的愤怒','木乃伊之咒','诅咒之触','Amumu.png','2015-02-20'),(14,'魔蛇之拥','卡西奥佩娅','0','法师','','瘟疫爆炸','剧毒迷雾','双生毒牙','石化凝视','魔蛇面容','Cassiopeia.png','0000-00-00'),(15,'探险家','依泽瑞尔','1','射手','法师','秘术射击','精华跃动','奥术跃迁','精准弹幕','咒能高涨','Ezreal.png','0000-00-00'),(16,'战争之影','赫卡里姆','1','战士','坦克','暴走','恐惧之灵','毁灭冲锋','暗影冲击','征战之路','Hecarim.png','0000-00-00'),(17,'复仇之矛','卡莉斯塔','0','射手','','穿刺','哨兵','撕裂','命运的召唤','武术姿态','Kalista.png','0000-00-00'),(18,'诡术妖姬','乐芙兰','0','刺客','法师','恶意魔印','魔影迷踪','幻影锁链','故技重施','乱花迷眼','Leblanc.png','0000-00-00'),(19,'无极剑圣','易','1','刺客','战士','阿尔法突袭','冥想','无极剑道','高原血统','双重打击','MasterYi.png','0000-00-00'),(20,'雪人骑士','努努','1','辅助','战士','吞噬','血之沸腾','冰雹','绝对零度','法术幻想','Nunu.png','0000-00-00'),(21,'放逐之刃','瑞文','0','战士','刺客','折翼之舞','震魂怒吼','勇往直前','放逐之锋','符文之刃','Riven.png','0000-00-00'),(22,'水晶先锋','斯卡纳','1','战士','坦克','水晶横扫','水晶蝎甲','晶体破碎','晶状毒刺','水晶尖塔','Skarner.png','0000-00-00'),(23,'麦林炮手','崔丝塔娜','0','射手','刺客','急速射击','火箭跳跃','爆炸火花','毁灭射击','瞄准','Tristana.png','0000-00-00'),(24,'虚空之眼','维克兹','1','法师','','有机体解构','等离子裂变','虚空裂隙','构造分解','生命形态瓦解射线','Velkoz.png','0000-00-00'),(25,'阿狸','九尾妖狐','0','法师','刺客','欺诈宝珠','妖异狐火','魅惑妖术','灵魄突袭','摄魂夺魄','Ahri.png','0000-00-00'),(26,'布兰德','复仇焰魂','1','法师','','火焰烙印','烈焰之柱','烈火燃烧','烈焰风暴','炽热之焰','Brand.png','0000-00-00'),(27,'艾克','时间刺客','1','刺客','战士','时间卷曲器','时间交错','相位俯冲','时空断裂','Z型驱动共振','Ekko.png','0000-00-00'),(28,'纳尔','迷失之牙','1','战士','坦克','投掷回力标 / 投掷顽石','亢奋 / 痛殴','轻跳 / 猛踏','呐啊!','狂怒基因','Gnar.png','0000-00-00'),(29,'杰斯','未来守护者','1','战士','射手','苍穹之跃/电能震荡','闪电领域/超能电荷','雷霆一击/加速之门','墨丘利之炮','海克斯科技电容','Jayce.png','0000-00-00'),(30,'卡兹克','虚空掠夺者','1','刺客','战士','品尝恐惧','虚空突刺','跃击','虚空来袭','无形威胁','Khazix.png','0000-00-00'),(31,'墨菲特','熔岩巨兽','1','坦克','战士','地震碎片','野蛮打击','大地震颤','势不可挡','花岗岩护盾','Malphite.png','0000-00-00'),(32,'诺提勒斯','深海泰坦','1','坦克','战士','疏通航道','泰坦之怒','暗流涌动','深海冲击','排山倒海','Nautilus.png','0000-00-00'),(33,'雷克塞','虚空遁地兽','0','战士','','女王之怒 / 猎物搜寻','遁地 / 破土而出','狂野之噬 / 挖掘隧道','虚空猛冲','艾克塞之怒','RekSai.png','0000-00-00'),(34,'辛吉德','炼金术士','1','坦克','战士','剧毒踪迹','强力粘胶','过肩摔','疯狂药剂','生命壁垒','Singed.png','0000-00-00'),(35,'塔里克','宝石骑士','1','辅助','战士','神圣洗礼','宝石护盾','炫光','辉耀','宝石*','Taric.png','0000-00-00'),(36,'韦鲁斯','惩戒之箭','1','射手','法师','穿刺之箭','枯萎箭袋','恶灵箭雨','腐败锁链','复仇之欲','Varus.png','0000-00-00'),(37,'安妮','黑暗之女','0','法师','','碎裂之火','焚烧','熔岩护盾','提伯斯之怒','嗜火','Annie.png','0000-00-00'),(38,'库奇','英勇投弹手','1','射手','','磷光炸弹','瓦尔基里俯冲','格林机枪','火箭轰击','海克斯科技军备','Corki.png','0000-00-00'),(39,'菲奥娜','无双剑姬','0','战士','刺客','破空斩','劳伦特心眼刀','夺命连刺','无双挑战','决斗之舞','Fiora.png','0000-00-00'),(40,'俄洛伊','海兽祭司','0','战士','坦克','触手猛击','触手猛击','灵魂试炼','过界信仰','古神先知','Illaoi.png','0000-00-00'),(41,'卡尔萨斯','死亡颂唱者','1','法师','','荒芜','痛苦之墙','*','安魂曲','死亡契约','Karthus.png','0000-00-00'),(42,'蕾欧娜','曙光女神','0','坦克','辅助','日光','日蚀','天顶之刃','日炎耀斑','日光','Leona.png','0000-00-00'),(43,'孙悟空','齐天大圣','1','战士','坦克','粉碎打击','真假猴王','腾云突击','大闹天宫','金刚不坏','MonkeyKing.png','0000-00-00'),(44,'奥莉安娜','发条魔灵','0','法师','辅助','指令:攻击','指令:杂音','指令:防卫','指令:冲击波','发条协奏','Orianna.png','0000-00-00'),(45,'瑞兹','流浪法师','1','法师','战士','超负荷','符文禁锢','法术涌动','绝望之力','奥术专精','Ryze.png','0000-00-00'),(46,'索拉卡','众星之子','1','辅助','法师','流星坠落','星之灌注','星体结界','祈愿','救赎','Soraka.png','0000-00-00'),(47,'泰达米尔','蛮族之王','1','战士','刺客','嗜血杀戮','蔑视','旋风斩','无尽怒火','战斗狂怒','Tryndamere.png','0000-00-00'),(48,'维克托','机械先驱','1','法师','','虹吸能量','重力场','死亡射线','混乱风暴','光荣进化','Viktor.png','0000-00-00'),(49,'阿兹尔','沙漠皇帝','1','法师','射手','狂沙猛攻','沙兵现身','流沙移形','禁军之墙','恕瑞玛的传承','Azir.png','0000-00-00'),(50,'黛安娜','皎月女神','0','战士','法师','新月打击','苍白之瀑','月之降临','月神冲刺','月银之刃','Diana.png','0000-00-00'),(51,'加里奥','哨兵之殇','1','坦克','法师','坚定重击','坚强壁垒','正义罡风','杜朗石像','符文石肤','Galio.png','0000-00-00'),(52,'迦娜','风暴之怒','0','辅助','法师','飓风呼啸','和风守护','风暴之眼','复苏季风','顺风而行','Janna.png','0000-00-00'),(53,'卡特琳娜','不祥之刃','0','刺客','法师','弹射之刃','不祥之刃','瞬步','死亡莲华','贪婪','Katarina.png','0000-00-00'),(54,'卢锡安','圣枪游侠','1','射手','','透体圣光','热诚烈弹','冷酷追击','圣枪游侠','圣光银弹','Lucian.png','0000-00-00'),(55,'莫甘娜','堕落天使','0','法师','辅助','暗之禁锢','痛苦腐蚀','黑暗之盾','灵魂镣铐','灵魂吸取','Morgana.png','0000-00-00'),(56,'波比','圣锤之毅','0','坦克','战士','圣锤猛击','坚定风采','英勇冲锋','持卫的裁决','钢铁大使','Poppy.png','0000-00-00'),(57,'萨科','恶魔小丑','1','刺客','','欺诈魔术','惊吓魔盒','双面毒刃','幻像','背刺','Shaco.png','0000-00-00'),(58,'辛德拉','暗黑元首','0','法师','辅助','暗黑法球','驱使念力','弱者退散','能量倾泻','卓尔不凡','Syndra.png','0000-00-00'),(59,'图奇','瘟疫之源','1','射手','刺客','埋伏','剧毒之桶','毒性爆发','火力全开','死亡毒液','Twitch.png','0000-00-00'),(60,'沃利贝尔','雷霆咆哮','1','战士','坦克','滚滚雷霆','狂野怒意','至尊咆哮','雷霆怒爪','风暴的抉择','Volibear.png','0000-00-00'),(61,'阿卡丽','暗影之拳','0','刺客','','奥义!绯叶','奥义!霞阵','奥义!散华','秘奥义!幻樱杀缭乱','忍法!苍绯印','Akali.png','0000-00-00'),(62,'布隆','弗雷尔卓德之心','1','辅助','坦克','寒冬之咬','挺身而出','坚不可摧','冰川裂隙','震荡猛击','Braum.png','0000-00-00'),(63,'伊莉丝','蜘蛛女皇','0','法师','战士','神经毒素 / 剧毒之蜇','自爆蜘蛛 / 掠行狂暴','结茧 / 盘丝','蜘蛛形态','蜘蛛女皇','Elise.png','0000-00-00'),(64,'古拉加斯','酒桶','1','战士','法师','滚动酒桶','醉酒狂暴','肉弹冲击','**酒桶','欢乐时光','Gragas.png','0000-00-00'),(65,'烬','戏命师','1','射手','刺客','曼舞手雷','致命华彩','万众倾倒','完美谢幕','低语','Jhin.png','0000-00-00'),(66,'千珏','永猎双子','0','射手','','乱箭之舞','狼灵*','横生惧意','羊灵生息','千珏之印','Kindred.png','0000-00-00'),(67,'玛尔扎哈','虚空先知','1','法师','刺客','虚空召唤','虚无盲区','煞星幻象','冥府之握','召唤虚灵','Malzahar.png','0000-00-00'),(68,'奈德丽','狂野女猎手','0','刺客','战士','标枪投掷','丛林伏击','野性奔腾','美洲狮','寻觅','Nidalee.png','0000-00-00'),(69,'雷克顿','荒漠屠夫','1','战士','坦克','暴君狂击','冷酷捕猎','横冲直撞','终极统治','怒之领域','Renekton.png','0000-00-00'),(70,'塞恩','亡灵战神','1','坦克','战士','残虐猛击','灵魂熔炉','杀手怒吼','蛮横冲撞','死亡荣耀','Sion.png','0000-00-00'),(71,'提莫','迅捷斥候','1','射手','刺客','致盲吹箭','小莫快跑','毒性射击','种蘑菇','隐形的翅膀','Teemo.png','0000-00-00'),(72,'薇恩','暗夜猎手','0','射手','刺客','闪避突袭','圣银弩箭','恶魔审判','终极时刻','暗夜猎手','Vayne.png','0000-00-00'),(73,'艾尼维亚','冰晶凤凰','0','法师','辅助','寒冰闪耀','寒冰屏障','霜寒刺骨','冰川风暴','寒霜涅槃','Anivia.png','0000-00-00'),(74,'科加斯','虚空恐惧','1','坦克','法师','破裂','野性尖叫','恐惧之刺','盛宴','肉食者','Chogath.png','0000-00-00'),(75,'费德提克','末日使者','1','法师','辅助','恐惧','生命吸取','黑暗之风','群鸦风暴','恐惧降临','FiddleSticks.png','0000-00-00'),(76,'黑默丁格','大发明家','1','法师','辅助','H-28G进化炮台','海克斯科技微型导弹','CH-2电子风暴手雷','升级!!!','科技魔法维修机器人','Heimerdinger.png','0000-00-00'),(77,'卡尔玛','天启者','0','法师','辅助','心灵烈焰','坚定不移','鼓舞','梵咒','聚能之炎','Karma.png','0000-00-00'),(78,'李青','盲僧','1','战士','刺客','天音波/回音击','金钟罩/铁布衫','天雷破/摧筋断骨','猛龙摆尾','疾风骤雨','LeeSin.png','0000-00-00'),(79,'厄运小姐','赏金猎人','0','射手','','一箭双雕','大步流星','枪林弹雨','弹幕时间','厄运的眷顾','MissFortune.png','0000-00-00'),(80,'奥拉夫','狂战士','1','战士','坦克','逆流投掷','残暴打击','鲁莽挥击','诸神黄昏','狂战之怒','Olaf.png','0000-00-00'),(81,'机械公敌','机械公敌','1','战士','法师','纵火盛宴','破碎护盾','电子鱼叉','恒温灼烧','机械重组','Rumble.png','0000-00-00'),(82,'娑娜','琴瑟仙女','0','辅助','法师','英勇赞美诗','坚毅咏叹调','迅捷奏鸣曲','狂舞终乐章','能量和弦','Sona.png','0000-00-00'),(83,'特朗德尔','巨魔之王','1','战士','坦克','利齿撕咬','冰封领域','寒冰之柱','强权至上','国王的贡品','Trundle.png','0000-00-00'),(84,'蔚','皮城执法官','0','战士','刺客','强能冲拳','爆弹重拳','透体之劲','天霸横空烈轰','爆裂护盾','Vi.png','0000-00-00'),(85,'星界游神','巴德','1','辅助','法师','星界束缚','游神圣坛','神奇旅程','调和命运','旅者的召唤','Bard.png','0000-00-00'),(86,'荣耀行刑官','德莱文','1','射手',NULL,'旋转飞斧','血性冲刺','开道利斧','冷血追命','德莱文联盟','Draven.png',NULL),(87,'海洋之灾','普朗克','1','战士',NULL,'枪火谈判','坏血病疗法','火药桶','加农炮幕','烈火审讯','Gangplank.png',NULL),(88,'德玛西亚皇子','嘉文四世','1','坦克','战士','巨龙撞击','黄金圣盾','德邦军旗','天崩地裂','战争律动','JarvanIV.png',NULL),(89,'审判天使','凯尔','0','战士','辅助','清算','神圣祝福','正义之怒','神圣庇护','圣焰','Kayle.png',NULL),(90,'仙灵女巫','璐璐','0','辅助','法师','闪耀长枪','奇思妙想','帮忙,皮克斯!','狂野生长','皮克斯,仙灵伙伴','Lulu.png',NULL),(91,'唤潮鲛姬','娜美','0','辅助','法师','碧波之牢','冲击之潮','唤潮之佑','怒涛之啸','踏浪之行','Nami.png',NULL),(92,'德玛西亚之翼','奎因','0','射手','战士','炫目攻势','敏锐感知','旋翔掠杀','深入敌后','侵扰','Quinn.png',NULL),(93,'暮光之眼','慎','1','坦克',NULL,'奥义!暮刃','奥义!魂佑','奥义!影缚','秘奥义!慈悲度魂落','忍法!气合盾','Shen.png',NULL),(94,'河流之王','塔姆','1','辅助','坦克','巨舌鞭笞','大快朵颐','厚实表皮','深渊潜航','培养品味','TahmKench.png',NULL),(95,'兽灵行者','乌迪尔','1','战士','坦克','猛虎姿态','灵龟姿态','巨熊姿态','火凤姿态','灵猴敏捷','Udyr.png',NULL),(96,'嗜血猎手','沃里克','1','战士','坦克','','','','','','Warwick.png',NULL),(97,'艾希','寒冰射手','0','射手','辅助','射手的专注','万箭齐发','鹰击长空','魔法水晶箭','冰霜射击','Ashe.png','0000-00-00'),(98,'德莱厄斯','诺克萨斯之手','1','战士','坦克','大杀四方','致残打击','无情铁手','诺克萨斯断头台','出血','Darius.png','0000-00-00'),(99,'菲兹','潮汐海灵','1','刺客','战士','淘气打击','海石三叉戟','古灵/精怪','巨鲨强袭','伶俐斗士','Fizz.png','0000-00-00'),(100,'艾瑞莉娅','刀锋意志','0','战士','刺客','利刃冲击','飞天姿态','均衡打击','至尊锋刃','艾欧尼亚热诚','Irelia.png','0000-00-00'),(101,'卡萨丁','虚空行者','1','刺客','法师','虚无法球','虚空之刃','能量脉冲','虚空行走','虚空之石','Kassadin.png','0000-00-00'),(102,'丽桑卓','冰霜女巫','0','法师','','寒冰碎片','冰霜之环','冰川之径','冰封陵墓','寒冰血脉','Lissandra.png','0000-00-00'),(103,'莫德凯撒','铁铠冥魂','1','战士','','铲击之锤','悲伤收割','毁灭虹吸','死亡之子','铁人','Mordekaiser.png','0000-00-00'),(104,'潘森','战争之王','1','战士','刺客','长矛飞掷','圣盾打击','穿心长矛','堕天一击','圣盾庇护','Pantheon.png','0000-00-00'),(105,'瑟庄妮','凛冬之怒','0','坦克','战士','极寒突袭','北风链枷','永冻领域','极冰寒狱','冰霜护甲','Sejuani.png','0000-00-00'),(106,'斯维因','策士统领','1','法师','战士','恶鸦来袭','鸦爪之缚','苦难折磨','邪鸦附体','腐肉再生','Swain.png','0000-00-00'),(107,'崔斯特','卡牌大师','1','法师','','万能牌','选牌','卡牌骗术','命运','灌铅骰子','TwistedFate.png','0000-00-00'),(108,'弗拉基米尔','猩红收割者','1','法师','坦克','鲜血转换','血红之池','血之潮汐','血之瘟疫','血色契约','Vladimir.png','0000-00-00'),(109,'牛头酋长','阿利斯塔','1','坦克','辅助','大地粉碎','野蛮冲撞','胜利怒吼','坚定意志','践踏','Alistar.png',NULL),(110,'皮城女警','凯特琳','0','射手','','和平使者','约德尔诱捕器','90口径绳网','让子弹飞','爆头','Caitlyn.png',NULL),(111,'寡妇制造者','伊芙琳','0','刺客','法师','憎恨之刺','暗黑狂暴','毁灭打击','痛苦之拥','暗影迷踪','Evelynn.png',NULL),(112,'法外狂徒','格雷福斯','1','射手','','穷途末路','烟幕弹','快速拔枪','终极爆弹','新命运','Graves.png',NULL),(113,'暴走萝莉','金克丝','0','射手','','枪炮交响曲','震荡电磁波','嚼火者手雷','超究极死神飞弹','罪恶快感','Jinx.png',NULL),(114,'深渊巨口','克格莫','1','射手','法师','腐蚀唾液','生化弹幕','虚空淤泥','活体大炮','来自艾卡西亚的惊喜','KogMaw.png',NULL),(115,'扭曲树精','茂凯','1','坦克','法师','奥术重击','扭曲突刺','树苗投掷','复仇漩涡','吸元秘术','Maokai.png',NULL),(116,'永恒梦魇','魔腾','1','刺客','战士','梦魇之径','黑暗庇护','无言恐惧','鬼影重重','暗影之刃','Nocturne.png',NULL),(117,'傲之追猎者','雷恩加尔','1','刺客','战士','残忍无情','战争咆哮','套索打击','狩猎律动','无形掠食者','Rengar.png',NULL),(118,'战争女神','希维尔','0','射手','','回旋之刃','弹射','法术护盾','狩猎','敏锐疾行','Sivir.png',NULL),(119,'魂锁典狱长','锤石','1','辅助','战士','死亡判决','魂引之灯','厄运钟','幽冥监牢','地狱诅咒','Thresh.png',NULL),(120,'邪恶小法师','维迦','1','法师','','黑暗祭祀','黑暗物质','扭曲空间','能量爆裂','均衡','Veigar.png',NULL),(121,'远古巫灵','泽拉斯','1','法师','刺客','奥能脉冲','毁灭之眼','冲击法球','奥术仪式','法力澎湃','Xerath.png','0000-00-00'),(122,'德邦总管','赵信','1','战士','刺客','三重爪击','狂战怒吼','无畏冲锋','新月横扫','战个痛快','XinZhao.png','0000-00-00'),(123,'疾风剑豪','亚索','1','战士','刺客','斩钢闪','风之障壁','踏前斩','狂风绝息斩','浪客之道','Yasuo.png','0000-00-00'),(124,'掘墓者','约里克','1','战士','法师','战争预兆','瘟疫预兆','饥荒预兆','死亡预兆','邪恶契约','Yorick.png','0000-00-00'),(125,'生化魔人','扎克','1','坦克','战士','延伸打击','不稳定物质','橡筋弹弓','动感弹球','细胞分裂','Zac.png','0000-00-00'),(126,'影流之主','劫','1','刺客','战士','影奥义!诸刃','影奥义!分身','影奥义!鬼斩','禁奥义!瞬狱影杀阵','影忍法!灭魂劫','Zed.png','0000-00-00'),(127,'**鬼才','吉格斯','1','法师','','弹跳炸弹','定点**','海克斯**雷区','科学的地狱火炮','一触即发','Ziggs.png','0000-00-00'),(128,'时光守护者','基兰','1','辅助','法师','定时炸弹','穿梭未来','时光发条','时光倒流','瓶中时光','Zilean.png','0000-00-00'),(129,'荆棘之兴','婕拉','0','法师','辅助','致命绽放','迅猛成长','缠绕之根','绞杀之藤','婕拉','Zyra.png','0000-00-00');

/*!40101 SET [email protected]_SQL_MODE */;
/*!40014 SET [email protected]_FOREIGN_KEY_CHECKS */;

第三步:在src/main/java文件夹下新建com.gyx.entity包,在包下新建一个Hero类。

package com.gyx.entity;

import java.util.Date;

public class Hero {

private Integer id;
private String name;
private String nickname;
private String sex;
private String first;
private String second;
private String q;
private String w;
private String e;
private String r;
private String passivity;
private String img;
private Date birthday;

public Hero() {}

public Hero(Integer id, String name, String nickname, String sex, String first, String second, String q, String w,
		String e, String r, String passivity, String img, Date birthday) {
	super();
	this.id = id;
	this.name = name;
	this.nickname = nickname;
	this.sex = sex;
	this.first = first;
	this.second = second;
	this.q = q;
	this.w = w;
	this.e = e;
	this.r = r;
	this.passivity = passivity;
	this.img = img;
	this.birthday = birthday;
}

public Hero(String name, String nickname, String sex, String first, String second, String q, String w, String e,
		String r, String passivity, String img, Date birthday) {
	super();
	this.name = name;
	this.nickname = nickname;
	this.sex = sex;
	this.first = first;
	this.second = second;
	this.q = q;
	this.w = w;
	this.e = e;
	this.r = r;
	this.passivity = passivity;
	this.img = img;
	this.birthday = birthday;
}
public Integer getId() {
	return id;
}
public void setId(Integer id) {
	this.id = id;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getNickname() {
	return nickname;
}
public void setNickname(String nickname) {
	this.nickname = nickname;
}
public String getSex() {
	return sex;
}
public void setSex(String sex) {
	this.sex = sex;
}
public String getFirst() {
	return first;
}
public void setFirst(String first) {
	this.first = first;
}
public String getSecond() {
	return second;
}
public void setSecond(String second) {
	this.second = second;
}
public String getQ() {
	return q;
}
public void setQ(String q) {
	this.q = q;
}
public String getW() {
	return w;
}
public void setW(String w) {
	this.w = w;
}
public String getE() {
	return e;
}
public void setE(String e) {
	this.e = e;
}
public String getR() {
	return r;
}
public void setR(String r) {
	this.r = r;
}
public String getPassivity() {
	return passivity;
}
public void setPassivity(String passivity) {
	this.passivity = passivity;
}
public String getImg() {
	return img;
}
public void setImg(String img) {
	this.img = img;
}
public Date getBirthday() {
	return birthday;
}
public void setBirthday(Date birthday) {
	this.birthday = birthday;
}
@Override
public String toString() {
	return "Hero [id=" + id + ", name=" + name + ", nickname=" + nickname + ", sex=" + sex + ", first=" + first
			+ ", second=" + second + ", q=" + q + ", w=" + w + ", e=" + e + ", r=" + r + ", passivity=" + passivity
			+ ", img=" + img + ", birthday=" + birthday + "]\n";
}
}

第四步:在com.gyx.entity包下新建一个heroMapper.xml映射文件

注意:在select标签中,id用来唯一标识当前语句,parameterType表示输入类型,resultType表示返回类型;在配置文件中用#{id}来对应Hero实体类中的id属性

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  
<mapper namespace="gyx">
<select id="getHeroById" parameterType="java.lang.Integer" resultType="com.gyx.entity.Hero">
	select * from heros where id = #{id}
</select>
<select id="getHeros" resultType="com.gyx.entity.Hero">
	select * from heros
</select>
<select id="getHeroCount" resultType="long">
	select count(*) from heros
</select>
<select id="getEmpDept" resultType="map">
	select empno,ename,dname 
	from emp e
	INNER JOIN dept d ON e.deptno = d.deptNo
</select>

<insert id="insertHero" parameterType="com.gyx.entity.Hero">
	insert into heros(name,nickname,sex,first,second,q,w,e,r,passivity,img,birthday) 
	value(#{name},#{nickname},#{sex},#{first},#{second},#{q},#{w},#{e},#{r},#{passivity},#{img},#{birthday})
</insert>

<delete id="deleteHero" parameterType="int">
	delete from heros where id=#{id}
</delete>

<!-- 多个参数,删除 -->
<delete id="delHero" parameterType="map">
	delete from heros where id=#{id} or name=#{name}
</delete>

<update id="updateHero" parameterType="com.gyx.entity.Hero">
	update heros 
	set name=#{name},nickname=#{nickname},sex=#{sex},first=#{first},second=#{second},
	q=#{q},w=#{w},e=#{e},r=#{r},passivity=#{passivity},img=#{img},birthday=#{birthday}
	where id=#{id}
</update>
</mapper>

第五步:在src/main/resources文件夹下新建一个mybatis-config.xml配置文件

注意:配置文件中的数据库,用户名,密码配置

<!-- 配置数据源 -->
<dataSource type="POOLED">
	<property name="driver" value="com.mysql.jdbc.Driver" />
	<property name="url" value="jdbc:mysql:///sxt" />
	<property name="username" value="root" />
	<property name="password" value="root" />
</dataSource>

把映射文件heroMapper.xml配置进总配置文件mybatis-config.xml中

<!-- 指定sql映射文件的路径 -->
<mappers>
   <mapper resource="com/gyx/entity/heroMapper.xml"/>
</mappers>

mybatis-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

<environments default="development">
    <!-- 配置数据库连接环境 -->
	<environment id="development">
	    <!-- 事务管理器使用JDBC事务 -->
		<transactionManager type="JDBC" />
		<!-- 配置数据源 -->
		<dataSource type="POOLED">
			<property name="driver" value="com.mysql.jdbc.Driver" />
			<property name="url" value="jdbc:mysql:///sxt" />
			<property name="username" value="root" />
			<property name="password" value="root" />
		</dataSource>
	</environment>	
</environments>

<!-- 指定sql映射文件的路径 -->
<mappers>
   <mapper resource="com/gyx/entity/heroMapper.xml"/>
</mappers>
</configuration>

第六步:在src/main/java文件夹下新建一个com.gyx.util包,并在该包下新建一个DbUtil类(工具类,方便我们使用session)

package com.gyx.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class DbUtil {

public static SqlSession getSession(){
	
	SqlSession session = null;
	String resource = "mybatis-config.xml";
	InputStream inputStream;
	try {
		inputStream = Resources.getResourceAsStream(resource);
		session = new SqlSessionFactoryBuilder().build(inputStream).openSession();
	} catch (IOException e) {
		e.printStackTrace();
	}
	return session;
}
}

第七步:在src/test/java文件夹下新建一个com.gyx.MyBaits包,在该包下新建一个Test类(Test类用来测试)

这里是通过导入junit的TestCase来进行测试的。测试的时候记得选中方法名右键 Run As --Junit Test

package com.gyx.MyBaits;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.gyx.entity.Hero;
import com.gyx.util.DbUtil;

import junit.framework.TestCase;

public class Test1 extends TestCase {
//测试数据库连接,通过id查询
public void testConn(){
	SqlSession session = DbUtil.getSession();
	Hero hero = session.selectOne("gyx.getHeroById", 100);
	System.out.println(hero);
	session.close();
}

//查询所有
public void testSelectAll(){
	SqlSession session = DbUtil.getSession();
	List<Hero> heros = session.selectList("gyx.getHeros");
	System.out.println(heros);
	session.close();
}

//多表联查
public void testEmpDept(){
	SqlSession session = DbUtil.getSession();
	List list = session.selectList("gyx.getEmpDept");
	Iterator it = list.iterator();
	while(it.hasNext()){
		Map map = (Map) it.next();
		System.out.println(map);
	}
	session.close();
}

//查询行数
public void testCount(){
	SqlSession session = DbUtil.getSession();
	long l = session.selectOne("gyx.getHeroCount");
	System.out.println(l);
	session.close();
}

//增加
public void testAdd(){
	SqlSession session = DbUtil.getSession();
	Hero hero = new Hero("派克", "血港鬼影", "1", "辅助", "上单", "q", "w", "e", "r", "哈哈", "paike.png", new Date());
	try {
		int result = session.insert("gyx.insertHero",hero);
		session.commit();
		System.out.println(result);
	} catch (Exception e) {
		e.printStackTrace();
		session.rollback();
	}finally{
		session.close();
	}
}

//删除
public void testDelete(){
	SqlSession session = DbUtil.getSession();
	try {
		int result = session.delete("gyx.deleteHero",130); // 删除前请先确定有id为130的数据
		session.commit();
		System.out.println(result);
	} catch (Exception e) {
		e.printStackTrace();
		session.rollback();
	}finally{
		session.close();
	}
}

//删除 多个参数
public void testDel(){
	SqlSession session = DbUtil.getSession();
	Map map = new HashMap();
	map.put("id", 137);        // 删除前请先确定有id为137的数据
	map.put("name", "公务员"); //删除前请先插入name为公务员的数据
	try {
		int result = session.delete("gyx.delHero",map);
		session.commit();
		System.out.println(result);
	} catch (Exception e) {
		e.printStackTrace();
		session.rollback();
	}finally{
		session.close();
	}
}

//修改
public void testUpdate(){
	Hero hero = new Hero(161,"派克", "血港鬼影", "1", "上单", "辅助", "q", "w", "e", "r", "哈哈", "paike.png", new Date());
	SqlSession session = DbUtil.getSession();
	try {
		int result =session.update("gyx.updateHero",hero);
		session.commit();
		System.out.println(result);
	} catch (Exception e) {
		e.printStackTrace();
		session.rollback();
	}finally{
		session.close();
	}
}
}
相关标签: mybatis