步骤一:新建一个maven项目,在pom.xml中加入相应的依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cque</groupId>
<artifactId>Spring-Mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<!--项目打包为war包-->
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--统一管理spring的版本-->
<spring.version>5.0.5.RELEASE</spring.version>
</properties>
<!--依赖管理-->
<dependencies>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<!--spring依赖-->
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring操作数据库-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring事务-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!--Spring和mybatis集成的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.3</version>
</dependency>
<!--spring测试-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--mySql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
<scope>runtime</scope>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!--mybatis分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
<!--测试-->
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>80</port>
<path>/</path>
<uriEncoding>${project.build.sourceEncoding}</uriEncoding>
</configuration>
</plugin>
<!-- maven插件控制 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
步骤二:创建一个数据库,然后创建一个表,sql语句如下:
CREATE TABLE `product` (
`pid` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`pname` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`market_price` double NULL DEFAULT NULL,
`shop_price` double NULL DEFAULT NULL,
`pimage` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`pdate` date NULL DEFAULT NULL,
`is_hot` int(11) NULL DEFAULT NULL,
`pdesc` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`pflag` int(11) NULL DEFAULT 0,
`cid` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`pid`) USING BTREE,
INDEX `product_fk_0001`(`cid`) USING BTREE,
CONSTRAINT `product_fk_0001` FOREIGN KEY (`cid`) REFERENCES `category` (`cid`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
在表中插入数据
INSERT INTO `product` VALUES ('1', '适用小米note m4小米4c小米3手机屏幕总成寄修维修单独换外屏触摸', 6000, 4500, '/group1/M00/00/00/wKgLLF9aJ-aAHf1_AAAkTSy22tk972.jpg', '2020-09-06', 1, '小米 4c 标准版 全网通 白色 移动联通电信4G手机 双卡双待', 0, '1');
INSERT INTO `product` VALUES ('10', '华为 Ascend Mate7', 2699, 2599, '/group1/M00/00/00/wKgLLF9aJ-aAJ5u7AAAZED_7CRQ758.jpg', '2020-06-07', 1, '华为 Ascend Mate7 月光银 移动4G手机 双卡双待双通6英寸高清大屏,纤薄机身,智能超八核,按压式指纹识别!!选择下方“移动老用户4G飞享合约”,无需换号,还有话费每月返还!', 0, '1');
INSERT INTO `product` VALUES ('11', 'vivo X5Pro', 2399, 2298, '/group1/M00/00/00/wKgLLF9aJ-aADloqAAA2ESv0qm4810.jpg', '2015-11-02', 1, '移动联通双4G手机 3G运存版 极光白【购机送蓝牙耳机+蓝牙自拍杆】新升级3G运行内存·双2.5D弧面玻璃·眼球识别技术', 0, '1');
INSERT INTO `product` VALUES ('12', '努比亚(nubia)My 布拉格', 1899, 1799, '/group1/M00/00/00/wKgLLF9aJ-aAQMz6AAASrk2J_YQ654.jpg', '2015-11-02', 0, '努比亚(nubia)My 布拉格 银白 移动联通4G手机 双卡双待【嗨11,下单立减100】金属机身,快速充电!布拉格相机全新体验!', 0, '1');
INSERT INTO `product` VALUES ('13', '华为 麦芒4', 2599, 2499, '/group1/M00/00/00/wKgLLF9aJ-aAM2FJAAAYbBojWhU595.jpg', '2020-06-07', 1, '华为 麦芒4 晨曦金 全网通版4G手机 双卡双待金属机身 2.5D弧面屏 指纹解锁 光学防抖', 0, '1');
INSERT INTO `product` VALUES ('14', 'vivo X5M', 1899, 1799, '/group1/M00/00/00/wKgLLF9aJ-aAaSgUAAAfOWRJDAU691.jpg', '2015-11-02', 0, 'vivo X5M 移动4G手机 双卡双待 香槟金【购机送蓝牙耳机+蓝牙自拍杆】5.0英寸大屏显示·八核双卡双待·Hi-Fi移动KTV', 0, '1');
INSERT INTO `product` VALUES ('15', 'Apple iPhone 6 (A1586)', 4399, 4288, '/group1/M00/00/00/wKgLLF9aJ-aASUerAAA0al8aZfI027.jpg', '2015-11-02', 1, 'Apple iPhone 6 (A1586) 16GB 金色 移动联通电信4G手机长期省才是真的省!点击购机送费版,月月送话费,月月享优惠,畅享4G网络,就在联通4G!', 0, '1');
INSERT INTO `product` VALUES ('16', '华为 HUAWEI Mate S 臻享版', 4200, 4087, '/group1/M00/00/00/wKgLLF9aJ-aAaqcqAAA4SjHJ01c248.jpg', '2015-11-03', 0, '华为 HUAWEI Mate S 臻享版 手机 极昼金 移动联通双4G(高配)满星评价即返30元话费啦;买就送电源+清水套+创意手机支架;优雅弧屏,mate7升级版', 0, '1');
INSERT INTO `product` VALUES ('17', '索尼(SONY) E6533 Z3+', 4099, 3999, '/group1/M00/00/00/wKgLLF9aJ-aAI0fDAAA2hhTWd8g514.jpg', '2015-11-02', 0, '索尼(SONY) E6533 Z3+ 双卡双4G手机 防水防尘 涧湖绿索尼z3专业防水 2070万像素 移动联通双4G', 0, '1');
INSERT INTO `product` VALUES ('18', 'HTC One M9+', 3599, 3499, '/group1/M00/00/00/wKgLLF9aJ-aACIJfAAAZ9xDRiOE069.jpg', '2015-11-02', 0, 'HTC One M9+(M9pw) 金银汇 移动联通双4G手机5.2英寸,8核CPU,指纹识别,UltraPixel超像素前置相机+2000万/200万后置双镜头相机!降价特卖,惊喜不断!', 0, '1');
INSERT INTO `product` VALUES ('19', 'HTC Desire 826d 32G 臻珠白', 1599, 1469, '/group1/M00/00/00/wKgLLF9aJ-aAXNnoAAAWtFUC_IU136.jpg', '2015-11-02', 1, '后置1300万+UltraPixel超像素前置摄像头+【双】前置扬声器+5.5英寸【1080p】大屏!', 0, '1');
INSERT INTO `product` VALUES ('2', '中兴 AXON', 2899, 2699, '/group1/M00/00/00/wKgLLF9aJ-aAPV73AAA33ntoU7U614.jpg', '2015-11-05', 1, '中兴 AXON 天机 mini 压力屏版 B2015 华尔金 移动联通电信4G 双卡双待', 0, '1');
INSERT INTO `product` VALUES ('20', '小米 红米2A 增强版 白色', 649, 549, '/group1/M00/00/00/wKgLLF9aJ-aAEf_zAAATQnaxs94932.jpg', '2015-11-02', 0, '新增至2GB 内存+16GB容量!4G双卡双待,联芯 4 核 1.5GHz 处理器!', 0, '1');
INSERT INTO `product` VALUES ('21', '魅族 魅蓝note2 16GB 白色', 1099, 999, '/group1/M00/00/00/wKgLLF9aJ-aACmoSAAAS-njphHY862.jpg', '2015-11-02', 0, '现货速抢,抢完即止!5.5英寸1080P分辨率屏幕,64位八核1.3GHz处理器,1300万像素摄像头,双色温双闪光灯!', 0, '1');
INSERT INTO `product` VALUES ('22', '三星 Galaxy S5 (G9008W) 闪耀白', 2099, 1999, '/group1/M00/00/00/wKgLLF9aJ-aAc2DrAAAXcAexZUc530.jpg', '2015-11-02', 1, '5.1英寸全高清炫丽屏,2.5GHz四核处理器,1600万像素', 0, '1');
INSERT INTO `product` VALUES ('23', 'sonim XP7700 4G手机', 1799, 1699, '/group1/M00/00/00/wKgLLF9aJ-aABq8cAABBSS-kHK8618.jpg', '2015-11-09', 1, '三防智能手机 移动/联通双4G 安全 黑黄色 双4G美国军工IP69 30天长待机 3米防水防摔 北斗', 0, '1');
INSERT INTO `product` VALUES ('24', '努比亚(nubia)Z9精英版 金色', 3988, 3888, '/group1/M00/00/00/wKgLLF9aJ-aAU8owAAAVjTbhPVs422.jpg', '2015-11-02', 1, '移动联通电信4G手机 双卡双待真正的无边框!金色尊贵版!4GB+64GB大内存!', 0, '1');
INSERT INTO `product` VALUES ('25', 'Apple iPhone 6 Plus (A1524) 16GB 金色', 5188, 4988, '/group1/M00/00/00/wKgLLF9aJ-aAVnLvAAA1UlTGDRU439.jpg', '2015-11-02', 0, 'Apple iPhone 6 Plus (A1524) 16GB 金色 移动联通电信4G手机 硬货 硬实力', 0, '1');
INSERT INTO `product` VALUES ('26', 'Apple iPhone 6s (A1700) 64G 玫瑰金色', 6388, 6088, '/group1/M00/00/00/wKgLLF9aJ-aAQNEpAAAVCgwgLZs013.jpg', '2015-11-02', 0, 'Apple iPhone 6 Plus (A1524) 16GB 金色 移动联通电信4G手机 硬货 硬实力', 0, '1');
INSERT INTO `product` VALUES ('27', '三星 Galaxy Note5(N9200)32G版', 5588, 5388, '/group1/M00/00/00/wKgLLF9aJ-aAVBSdAAAXxiMOkYE771.jpg', '2015-11-02', 0, '旗舰机型!5.7英寸大屏,4+32G内存!不一样的SPen更优化的浮窗指令!赠无线充电板!', 0, '1');
INSERT INTO `product` VALUES ('28', '三星 Galaxy S6 Edge+(G9280)32G版 铂光金', 5999, 5888, '/group1/M00/00/00/wKgLLF9aJ-aAOAMdAAASAkjFMxE781.jpg', '2015-11-02', 0, '赠移动电源+自拍杆+三星OTG金属U盘+无线充电器+透明保护壳', 0, '1');
INSERT INTO `product` VALUES ('29', 'LG G4(H818)陶瓷白 国际版', 3018, 2978, '/group1/M00/00/00/wKgLLF9aJ-aAE2XsAAAX7G3vRhg056.jpg', '2015-11-02', 0, '李敏镐代言,F1.8大光圈1600万后置摄像头,5.5英寸2K屏,3G+32G内存,LG年度旗舰机!', 0, '1');
INSERT INTO `product` VALUES ('3', '华为荣耀6', 1599, 1499, '/group1/M00/00/00/wKgLLF9aJ-aATGxfAAA1szeugx0851.jpg', '2015-11-02', 0, '荣耀 6 (H60-L01) 3GB内存标准版 黑色 移动4G手机', 0, '1');
INSERT INTO `product` VALUES ('30', '微软(Microsoft) Lumia 640 LTE DS (RM-1113)', 1099, 999, '/group1/M00/00/00/wKgLLF9aJ-aAPSB2AAAbLyRrHbA775.jpg', '2015-11-02', 0, '微软首款双网双卡双4G手机,5.0英寸高清大屏,双网双卡双4G!', 0, '1');
INSERT INTO `product` VALUES ('31', '宏碁(acer)ATC705-N50 台式电脑', 2399, 2299, '/group1/M00/00/00/wKgLLF9aJ-aAZ-WCAABG5zeudNs503.jpg', '2015-11-02', 0, '爆款直降,满千减百,品质宏碁,特惠来袭,何必苦等11.11,早买早便宜!', 0, '2');
INSERT INTO `product` VALUES ('32', 'Apple MacBook Air MJVE2CH/A 13.3英寸', 6788, 6688, '/group1/M00/00/00/wKgLLF9aJ-aAPGYzAAAmh2lwLOM281.jpg', '2015-11-02', 0, '宽屏笔记本电脑 128GB 闪存', 0, '2');
INSERT INTO `product` VALUES ('33', '联想(ThinkPad) 轻薄系列E450C(20EH0001CD)', 4399, 4199, '/group1/M00/00/00/wKgLLF9aJ-aAdttqAAA3_nI8lJc412.jpg', '2015-11-02', 0, '联想(ThinkPad) 轻薄系列E450C(20EH0001CD)14英寸笔记本电脑(i5-4210U 4G 500G 2G独显 Win8.1)', 0, '2');
INSERT INTO `product` VALUES ('34', '联想(Lenovo)小新V3000经典版', 4599, 4499, '/group1/M00/00/00/wKgLLF9aJ-aARcwuAAArBBdSe_c841.jpg', '2015-11-02', 0, '14英寸超薄笔记本电脑(i7-5500U 4G 500G+8G SSHD 2G独显 全高清屏)黑色满1000減100,狂减!火力全开,横扫3天!', 0, '2');
INSERT INTO `product` VALUES ('35', '华硕(ASUS)经典系列R557LI', 3799, 3699, '/group1/M00/00/00/wKgLLF9aJ-aAbuA8AAA6ZGqqVP0591.jpg', '2015-11-02', 0, '15.6英寸笔记本电脑(i5-5200U 4G 7200转500G 2G独显 D刻 蓝牙 Win8.1 黑色)', 0, '2');
INSERT INTO `product` VALUES ('36', '华硕(ASUS)X450J', 4599, 4399, '/group1/M00/00/00/wKgLLF9aJ-aAN4XBAAAxOXIrWl0149.jpg', '2015-11-02', 0, '14英寸笔记本电脑 (i5-4200H 4G 1TB GT940M 2G独显 蓝牙4.0 D刻 Win8.1 黑色)', 0, '2');
INSERT INTO `product` VALUES ('37', '戴尔(DELL)灵越 飞匣3000系列', 3399, 3299, '/group1/M00/00/00/wKgLLF9aJ-aAD3R5AAA54iF2FrQ918.jpg', '2015-11-03', 0, ' Ins14C-4528B 14英寸笔记本(i5-5200U 4G 500G GT820M 2G独显 Win8)黑', 0, '2');
INSERT INTO `product` VALUES ('38', '惠普(HP)WASD 暗影精灵', 5699, 5499, '/group1/M00/00/00/wKgLLF9aJ-aAXuntAAAvWVCdSN4380.jpg', '2015-11-02', 0, '15.6英寸游戏笔记本电脑(i5-6300HQ 4G 1TB+128G SSD GTX950M 4G独显 Win10)', 0, '2');
INSERT INTO `product` VALUES ('39', 'Apple 配备 Retina 显示屏的 MacBook', 11299, 10288, '/group1/M00/00/00/wKgLLF9aJ-aADLMLAAArdCyl21o104.jpg', '2015-11-02', 0, 'Pro MF840CH/A 13.3英寸宽屏笔记本电脑 256GB 闪存', 0, '2');
INSERT INTO `product` VALUES ('4', '联想 P1', 2199, 1999, '/group1/M00/00/00/wKgLLF9aJ-aACbGDAAAsKETq9vo836.jpg', '2015-11-02', 0, '联想 P1 16G 伯爵金 移动联通4G手机充电5分钟,通话3小时!科技源于超越!品质源于沉淀!5000mAh大电池!高端商务佳配!', 0, '1');
INSERT INTO `product` VALUES ('40', '机械革命(MECHREVO)MR X6S-M', 6799, 6599, '/group1/M00/00/00/wKgLLF9aJ-aASB30AAAvaiJ78aA659.jpg', '2015-11-02', 0, '15.6英寸游戏本(I7-4710MQ 8G 64GSSD+1T GTX960M 2G独显 IPS屏 WIN7)黑色', 0, '2');
INSERT INTO `product` VALUES ('41', '神舟(HASEE) 战神K660D-i7D2', 5699, 5499, '/group1/M00/00/00/wKgLLF9aJ-aAY85ZAAAnEQgMFf0431.jpg', '2015-11-02', 0, '15.6英寸游戏本(i7-4710MQ 8G 1TB GTX960M 2G独显 1080P)黑色', 0, '2');
INSERT INTO `product` VALUES ('42', '微星(MSI)GE62 2QC-264XCN', 6199, 5999, '/group1/M00/00/00/wKgLLF9aJ-aAZPghAAAtpBz8iL0150.jpg', '2015-11-02', 0, '15.6英寸游戏笔记本电脑(i5-4210H 8G 1T GTX960MG DDR5 2G 背光键盘)黑色', 0, '2');
INSERT INTO `product` VALUES ('43', '雷神(ThundeRobot)G150S', 5699, 5499, '/group1/M00/00/00/wKgLLF9aJ-aAFH3LAAAtMW1A6cs446.jpg', '2015-11-02', 0, '15.6英寸游戏本 ( i7-4710MQ 4G 500G GTX950M 2G独显 包无亮点全高清屏) 金', 0, '2');
INSERT INTO `product` VALUES ('44', '惠普(HP)轻薄系列 HP', 3199, 3099, '/group1/M00/00/00/wKgLLF9aJ-aAStdPAAA8RW4XEc4322.jpg', '2015-11-02', 0, '15-r239TX 15.6英寸笔记本电脑(i5-5200U 4G 500G GT820M 2G独显 win8.1)金属灰', 0, '2');
INSERT INTO `product` VALUES ('45', '未来人类(Terrans Force)T5', 10999, 9899, '/group1/M00/00/00/wKgLLF9aJ-aAUjrDAAAgC232Kc0040.jpg', '2015-11-02', 0, '15.6英寸游戏本(i7-5700HQ 16G 120G固态+1TB GTX970M 3G GDDR5独显)黑', 0, '2');
INSERT INTO `product` VALUES ('46', '戴尔(DELL)Vostro 3800-R6308 台式电脑', 3299, 3199, '/group1/M00/00/00/wKgLLF9aJ-aAOZE7AAAuQzhKjMk553.jpg', '2015-11-02', 0, '(i3-4170 4G 500G DVD 三年上门服务 Win7)黑', 0, '2');
INSERT INTO `product` VALUES ('47', '联想(Lenovo)H3050 台式电脑', 5099, 4899, '/group1/M00/00/00/wKgLLF9aJ-aAYMMZAAA3fxaOTNE383.jpg', '2015-11-11', 0, '(i5-4460 4G 500G GT720 1G独显 DVD 千兆网卡 Win10)23英寸', 0, '2');
INSERT INTO `product` VALUES ('48', 'Apple iPad mini 2 ME279CH/A', 2088, 1888, '/group1/M00/00/00/wKgLLF9aJ-eARYlbAAA1QklTxCg471.jpg', '2015-11-02', 0, '(配备 Retina 显示屏 7.9英寸 16G WLAN 机型 银色)', 0, '2');
INSERT INTO `product` VALUES ('49', '小米(MI)7.9英寸平板', 1399, 1299, '/group1/M00/00/01/wKgLLF9aJ-eAdesqAAAjtSP2ALc298.jpg', '2015-11-02', 0, 'WIFI 64GB(NVIDIA Tegra K1 2.2GHz 2G 64G 2048*1536视网膜屏 800W)白色', 0, '2');
INSERT INTO `product` VALUES ('5', '摩托罗拉 moto x(x+1)', 1799, 1699, '/group1/M00/00/00/wKgLLF9aJ-aAXiaAAAAgIXvnkYw991.jpg', '2015-11-01', 0, '摩托罗拉 moto x(x+1)(XT1085) 32GB 天然竹 全网通4G手机11月11天!MOTO X震撼特惠来袭!1699元!带你玩转黑科技!天然材质,原生流畅系统!', 0, '1');
INSERT INTO `product` VALUES ('50', 'Apple iPad Air 2 MGLW2CH/A', 2399, 2299, '/group1/M00/00/01/wKgLLF9aJ-eAfez0AAA3jw2iOlQ487.jpg', '2015-11-12', 0, '(9.7英寸 16G WLAN 机型 银色)', 0, '2');
步骤三:创建一个实体类Product.java
package pojo;
import lombok.Data;
import lombok.ToString;
import java.util.Date;
/**
* @ClassName Product
* @Author chen
* @Description 这里是用Lombok插件通过注解来创建getter/setter方法和toString方法
* @Date 2020/10/16 22:32
*/
@Data
@ToString
public class Product {
// ID
private String id;
// 商品名称
private String name;
// 商品的市场价格
private Double marketPrice;
// 商品的商城价格
private Double shopPrice;
// 商品的图片URL
private String image;
// 商品上架的时间
private Date createDate;
// 是否热销 1 热销 0 不是热销
private Integer isHot;
// 商品的描述
private String desc;
// 是否上架 0 上架 1 下架
private Integer flag;
// 商品的分类ID
private String cid;
}
步骤四:新建jdbc.properties文件
jdbc.driver=com.mysql.jdbc.Driver
#数据库连接字符串(改成自己的连接)
jdbc.url=jdbc:mysql://192.168.11.44:3306/shop?characterEncoding=utf8&useSSL=false
#数据库用户名(改成自己的用户名)
jdbc.username=root
#数据库密码(改成自己的密码)
jdbc.password=123456
步骤五:写出这个实体类的映射接口,里面有我们要实现的抽象方法。
package dao;
import pojo.Product;
import java.util.List;
/**
* @InterfaceName ProductDao
* @Description
* @Author chen
* @Date 2020/10/16 0016 22:39
*/
public interface ProductDao {
List<Product> findAll();
}
步骤六:写出这个类的映射配置文件,里面有 sql语句
<?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="cque.dao.ProductDao">
<!--自定义映射-->
<resultMap id="productMap" type="product">
<id column="pid" property="id"/>
<result column="pname" property="name"/>
<result column="market_price" property="marketPrice"/>
<result column="shop_price" property="shopPrice"/>
<result column="pimage" property="image"/>
<result column="pdate" property="createDate"/>
<result column="is_hot" property="isHot"/>
<result column="pdesc" property="desc"/>
<result column="pflag" property="flag"/>
<result column="cid" property="cid"/>
</resultMap>
<select id="findAll" resultMap="productMap">
select * from product
</select>
</mapper>
步骤七:新建mybatis核心配置文件SqlMapConfig.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>
<!--别名-->
<typeAliases>
<package name="cque.pojo"/>
</typeAliases>
</configuration>
步骤八:新建spring核心配置文件applicationContext.xml,并整合Mybatis
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--扫描注解-->
<context:component-scan base-package="cque.service"/>
<!-- 加载properties配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- spring整合mybatis -->
<!-- 1 将SqlSessionFactory交给spring管理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 1.1 数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 1.2 加载Mybatis核心配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
</bean>
<!-- 2 让spring去扫描dao接口所在的包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cque.dao"/>
</bean>
</beans>
步骤九:测试类编写
package cque.service;
import cque.dao.ProductDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @ClassName Test
* @Description
* @Author chen
* @Date 2020/10/17 0017 9:29
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestProductService {
@Autowired
ProductDao productDao;
@Test
public void testFindAll(){
productDao.findAll().forEach(System.out::println);
}
}