mybatis练习1
1.建表
USE how2java;
CREATE TABLE product (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(32) DEFAULT NULL,
price float(8) default 0.0,
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
USE how2java;
INSERT INTO product VALUES (001,'computer',3700.0);
2.编写对应pojo和配置文件xml
package com.how2java.pojo; public class Product { int id; String name; float price; public Product(int id, String name, float price) { super(); this.id = id; this.name = name; this.price = price; } public Product() { super(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } @Override public String toString() { return "Product [id=" + id + ", name=" + name + ", price=" + price + "]"; } }
<?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="com.how2java.pojo"> <select id="listProduct" resultType="Product"> select * from product </select> </mapper>
3.编写测试文件并测试