mybatis练习1
Posted roof
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.编写测试文件并测试
以上是关于mybatis练习1的主要内容,如果未能解决你的问题,请参考以下文章
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段