标签:getname ids out delete str type nec builds nbsp
搭建环境
hibernate的jar包
这些jar包全部导入
1.建立hibernate.cfg.xml文件, 包所在的文件夹的project里去搜索cfg.xml复制并修改
2.配置hibernate.cfg.xml文件
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 数据库方言 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><!-- 引用jdbc包 --> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/myzy?characterEncoding=GBK</property> <!-- 数据库链接 --> <property name="show_sql">true</property> <!-- true 执行完在控制台打印SQL语句 --> <!-- 表映射加载 --> <mapping resource="com/itnba/maya/model/Fruit.hbm.xml"/> </session-factory> </hibernate-configuration>
3.每个配置项的值可以从project/etc/hibernate.properties中搜索
package com.itnba.maya.model; public class Fruit { public String getIds() { return ids; } public void setIds(String ids) { this.ids = ids; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public String getSource() { return source; } public void setSource(String source) { this.source = source; } public Integer getNumbers() { return numbers; } public void setNumbers(Integer numbers) { this.numbers = numbers; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } private String ids; private String name; private Double price; private String source; private Integer numbers; private String image; }
1.建立:实体类名.hbm.xml文件。包所在的文件夹的project里去搜索hbm.xml复制并修改
要跟实体类放一个包里
2.配置实体类名.hbm.xml文件
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping default-lazy="false" package="com.itnba.maya.model"> <!-- 导入包 --> <class name="Fruit" table="Fruit"> <!-- 表名 --> <id name="ids"> <!-- 主键 --> <generator class="assigned"/><!-- 如果主键是自增长改为class="native" --> </id> <!-- 对应的各个字段名 --> <property name="name"/> <property name="price"/> <property name="source"/> <property name="numbers"/> <property name="image"/> </class> </hibernate-mapping>
说明:这里的session不是web中的session。
web中的session代表的是客户端与web服务器之间的会话。
这里的session指的是程序与数据库之前的会话,它是由hibernate进行管理的。
添加
package com.itnba.maya.test; import java.util.List; import org.hibernate.*; import org.hibernate.cfg.*; import com.itnba.maya.model.Fruit; public class AA { public static void main(String[] args) { //读取hibernate.cfg.xml的配置,加载Hiberna的类库 Configuration config=new Configuration().configure(); //根据配置,生成session工厂 SessionFactory factory= config.buildSessionFactory(); //用工厂生成session Session session =factory.openSession(); //添加 //1.造对象 Fruit data = new Fruit(); data.setIds("s002"); data.setName("芒果"); data.setSource("博山"); data.setPrice(10.0); data.setNumbers(200); data.setImage("无"); //2.用session.save()保存到数据库了 try{ session.beginTransaction(); session.save(data); session.getTransaction().commit(); } catch(Exception ex){ session.getTransaction().rollback(); } session.close(); } }
查询
package com.itnba.maya.test; import java.util.List; import org.hibernate.*; import org.hibernate.cfg.*; import com.itnba.maya.model.Fruit; public class AA { public static void main(String[] args) { //读取hibernate.cfg.xml的配置,加载Hiberna的类库 Configuration config=new Configuration().configure(); //根据配置,生成session工厂 SessionFactory factory= config.buildSessionFactory(); //用工厂生成session Session session =factory.openSession(); //查询 //查单条数据 Fruit data=session.get(Fruit.class, "k001"); System.out.println(data.getName()+data.getPrice()+data.getNumbers()); //查询所有数据 List<Fruit> list = session.createQuery("from Fruit").getResultList(); for(Fruit a:list){ System.out.println(a.getName()+a.getPrice()+a.getNumbers()); } session.close(); } }
控制台显示
删除
package com.itnba.maya.test; import java.util.List; import org.hibernate.*; import org.hibernate.cfg.*; import com.itnba.maya.model.Fruit; public class AA { public static void main(String[] args) { //读取hibernate.cfg.xml的配置,加载Hiberna的类库 Configuration config=new Configuration().configure(); //根据配置,生成session工厂 SessionFactory factory= config.buildSessionFactory(); //用工厂生成session Session session =factory.openSession(); //删除 //先查出 Fruit data = session.load(Fruit.class,"k002");//get和load都可以查 if(data!=null){ session.beginTransaction(); //删除提交数据库 session.delete(data); session.getTransaction().commit(); } session.close(); } }
修改
package com.itnba.maya.test; import java.util.List; import org.hibernate.*; import org.hibernate.cfg.*; import com.itnba.maya.model.Fruit; public class AA { public static void main(String[] args) { //读取hibernate.cfg.xml的配置,加载Hiberna的类库 Configuration config=new Configuration().configure(); //根据配置,生成session工厂 SessionFactory factory= config.buildSessionFactory(); //用工厂生成session Session session =factory.openSession(); //修改 //1.查 Fruit data = session.load(Fruit.class,"s002"); if(data != null){ session.beginTransaction(); //2.改 data.setName("哈密瓜"); data.setPrice(11.1); data.setNumbers(99); //3.提交 session.update(data); session.getTransaction().commit(); } session.close(); } }
标签:getname ids out delete str type nec builds nbsp
原文地址:http://www.cnblogs.com/hq233/p/6497985.html