4、引入servlet包
添加点测试代码,执行【tomcat】测试一下
创建【com.item.servlet】包,并创建【GetInfoServlet】文件。
package com.item.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/GetInfo") public class GetInfoServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("访问测试"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
访问接口:
为了操作方便,将默认浏览器改为谷歌。
5、配置mybatis-config.xml
基础配置,这里用到的表是【product】表,sql语句看下方。
<?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> <settings> <setting name="logImpl" value="LOG4J"/> </settings> <typeAliases> <package name="com.item.model"/> </typeAliases> <environments default="dev"> <environment id="dev"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mytest?characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="12345678"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/item/mapper/ProductMapper.xml"></mapper> </mappers> </configuration>
配置ProductMapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC ".//mybaits.org/DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.item.mapper.ProductMapper"> <select id="GetInfo" resultType="Product"> select * from product </select> <delete id="DeleteById" parameterType="java.lang.Integer"> delete from product where id="${id}" </delete> </mapper>
数据库名称【mytest】,编码类型【utf8】
DROP TABLE IF EXISTS `product`; CREATE TABLE `product` ( `id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `createDate` datetime(0) NOT NULL, `modifyDate` datetime(0) NOT NULL, `productName` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `productTitle` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `productPrice` decimal(10, 2) NOT NULL, `productCount` int(8) NOT NULL, `productType` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `productColor` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `productWeight` double NULL DEFAULT NULL, `productStatus` int(1) NOT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact; INSERT INTO `product` VALUES ('b383581fd20211ec84b500e070bfdb54', '2022-05-12 22:49:18', '2022-05-12 22:49:18', '外星人M15', '高端外星人', 13499.00, 299, '外星人', 'black', 3300, 1); INSERT INTO `product` VALUES ('b3839547d20211ec84b500e070bfdb54', '2022-05-12 22:49:18', '2022-05-12 22:49:18', 'ThinkBook', '联想', 4599.00, 159, '联想', 'gray', 2250, 1); INSERT INTO `product` VALUES ('b383d49dd20211ec84b500e070bfdb54', '2022-05-12 22:49:18', '2022-05-12 22:49:18', '戴尔G15', '戴尔', 7499.00, 179, '戴尔', 'gray', 2270, 1); INSERT INTO `product` VALUES ('b384180cd20211ec84b500e070bfdb54', '2022-05-12 22:49:18', '2022-05-12 22:49:18', 'RedmiBook Pro15', '小米', 4499.00, 699, '小米', 'black', 2500, 1); INSERT INTO `product` VALUES ('b38457bed20211ec84b500e070bfdb54', '2022-05-12 22:49:18', '2022-05-12 22:49:18', '华硕a豆', '华硕', 3699.00, 799, '华硕', 'pink', 2100, 1); INSERT INTO `product` VALUES ('f6715eb2d20111ec84b500e070bfdb54', '2022-05-12 22:44:01', '2022-05-12 22:44:01', '拯救者Y7700P', '2022新品拯救者', 7399.00, 199, '联想', 'gray', 2200, 1);
6、配置ProductMapper.xml文件
7、创建【com.item.model.Product.java】文件
写入一下编码,【Getter】【Setter】【toString()】
package com.item.model; import java.math.BigDecimal; import java.util.Date; public class Product { private String id; private Date createDate; private Date modifyDate; private String productName; private String productTitle; private BigDecimal productPrice; private int productCount; private String productType; private String productColor; private double productWeight; private int productStatus; @Override public String toString() { return "Product{" + "id='" + id + '\'' + ", createDate=" + createDate + ", modifyDate=" + modifyDate + ", productName='" + productName + '\'' + ", productTitle='" + productTitle + '\'' + ", productPrice=" + productPrice + ", productCount=" + productCount + ", productType='" + productType + '\'' + ", productColor='" + productColor + '\'' + ", productWeight=" + productWeight + ", productStatus=" + productStatus + '}'; } public String getId() { return id; } public void setId(String id) { this.id = id; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public Date getModifyDate() { return modifyDate; } public void setModifyDate(Date modifyDate) { this.modifyDate = modifyDate; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getProductTitle() { return productTitle; } public void setProductTitle(String productTitle) { this.productTitle = productTitle; } public BigDecimal getProductPrice() { return productPrice; } public void setProductPrice(BigDecimal productPrice) { this.productPrice = productPrice; } public int getProductCount() { return productCount; } public void setProductCount(int productCount) { this.productCount = productCount; } public String getProductType() { return productType; } public void setProductType(String productType) { this.productType = productType; } public String getProductColor() { return productColor; } public void setProductColor(String productColor) { this.productColor = productColor; } public double getProductWeight() { return productWeight; } public void setProductWeight(double productWeight) { this.productWeight = productWeight; } public int getProductStatus() { return productStatus; } public void setProductStatus(int productStatus) { this.productStatus = productStatus; } }