当前位置: 首页 > news >正文

宁波规划建设局网站用dw制作网站模板下载地址

宁波规划建设局网站,用dw制作网站模板下载地址,广州市建筑集团有限公司官网,长沙景点排行榜CRUD是指在做计算处理时的增加(Create)、读取(Retrieve)#xff08;重新得到数据#xff09;、更新(Update)和删除(Delete)几个单词的首字母简写. 下面列举实例来讲解这几个操作#xff1a; 实体类#xff1a; package com.oumyye.model;public class Student {private lon…CRUD是指在做计算处理时的增加(Create)、读取(Retrieve)重新得到数据、更新(Update)和删除(Delete)几个单词的首字母简写. 下面列举实例来讲解这几个操作 实体类 package com.oumyye.model;public class Student {private long id;private String name;private Class c;public long getId() {return id;}public void setId(long id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public Class getC() {return c;}public void setC(Class c) {this.c c;}Overridepublic String toString() {return Student [id id , name name ];}} package com.oumyye.model;import java.util.HashSet; import java.util.Set;public class Class {private long id;private String name;private SetStudent studentsnew HashSetStudent();public long getId() {return id;}public void setId(long id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public SetStudent getStudents() {return students;}public void setStudents(SetStudent students) {this.students students;}} 映射文件 Student.hbm.xml ?xml version1.0? !DOCTYPE hibernate-mapping PUBLIC -//Hibernate/Hibernate Mapping DTD 3.0//ENhttp://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd hibernate-mapping packagecom.oumyye.modelclass nameStudent tablet_studentid columnstuId nameidgenerator classnative//idproperty columnstuName generatednever lazyfalse namename/many-to-one cascadesave-update classcom.oumyye.model.ClasscolumnclassId namec//class /hibernate-mapping Class.hbm.xml ?xml version1.0? !DOCTYPE hibernate-mapping PUBLIC -//Hibernate/Hibernate Mapping DTD 3.0//ENhttp://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd hibernate-mapping packagecom.oumyye.modelclass nameClass tablet_classid columnclassId nameidgenerator classnative//idproperty columnclassName generatednever lazyfalse namename/set cascadedelete inversetrue namestudents sortunsortedkey columnclassId/one-to-many classcom.oumyye.model.Student//set/class /hibernate-mapping 工具类可以有myeclipse生成 package com.oumyye.util;import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.AnnotationConfiguration;/*** Configures and provides access to Hibernate sessions, tied to the* current thread of execution. Follows the Thread Local Session* pattern, see {link http://hibernate.org/42.html }.*/ public class HibernateSessionFactory {/** * Location of hibernate.cfg.xml file.* Location should be on the classpath as Hibernate uses * #resourceAsStream style lookup for its configuration file. * The default classpath location of the hibernate config file is * in the default package. Use #setConfigFile() to update * the location of the configuration file for the current session. */private static final ThreadLocalSession threadLocal new ThreadLocalSession();private static org.hibernate.SessionFactory sessionFactory;private static Configuration configuration new AnnotationConfiguration(); private static String CONFIG_FILE_LOCATION /hibernate.cfg.xml;private static String configFile CONFIG_FILE_LOCATION;static {try {configuration.configure(configFile);sessionFactory configuration.buildSessionFactory();} catch (Exception e) {System.err.println(%%%% Error Creating SessionFactory %%%%);e.printStackTrace();}}private HibernateSessionFactory() {}/*** Returns the ThreadLocal Session instance. Lazy initialize* the codeSessionFactory/code if needed.** return Session* throws HibernateException*/public static Session getSession() throws HibernateException {Session session (Session) threadLocal.get();if (session null || !session.isOpen()) {if (sessionFactory null) {rebuildSessionFactory();}session (sessionFactory ! null) ? sessionFactory.openSession(): null;threadLocal.set(session);}return session;}/*** Rebuild hibernate session factory**/public static void rebuildSessionFactory() {try {configuration.configure(configFile);sessionFactory configuration.buildSessionFactory();} catch (Exception e) {System.err.println(%%%% Error Creating SessionFactory %%%%);e.printStackTrace();}}/*** Close the single hibernate session instance.** throws HibernateException*/public static void closeSession() throws HibernateException {Session session (Session) threadLocal.get();threadLocal.set(null);if (session ! null) {session.close();}}/*** return session factory**/public static org.hibernate.SessionFactory getSessionFactory() {return sessionFactory;}/*** return session factory** session factory will be rebuilded in the next call*/public static void setConfigFile(String configFile) {HibernateSessionFactory.configFile configFile;sessionFactory null;}/*** return hibernate configuration**/public static Configuration getConfiguration() {return configuration;}} 配置文件hibernate.cfg.xml ?xml version1.0 encodingutf-8? !DOCTYPE hibernate-configuration PUBLIC-//Hibernate/Hibernate Configuration DTD 3.0//ENhttp://www.hibernate.org/dtd/hibernate-configuration-3.0.dtdhibernate-configurationsession-factory!--数据库连接设置 --property nameconnection.driver_classcom.mysql.jdbc.Driver/propertyproperty nameconnection.urljdbc:mysql://localhost:3306/mytest/propertyproperty nameconnection.usernameroot/propertyproperty nameconnection.passwordroot/property!-- 方言 --property namedialectorg.hibernate.dialect.MySQL5Dialect/property!-- 控制台显示SQL --property nameshow_sqltrue/property!-- 自动更新表结构 --property namehbm2ddl.autoupdate/propertymapping resourcecom/oumyye/model/Class.hbm.xml /mapping resourcecom/oumyye/model/Student.hbm.xml //session-factory/hibernate-configuration 测试类 package com.oumyye.service;import java.util.Iterator; import java.util.Set;import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.After; import org.junit.Before; import org.junit.Test;import com.oumyye.model.Class; import com.oumyye.model.Student; import com.oumyye.util.HibernateSessionFactory;public class StudentTest {private SessionFactory sessionFactoryHibernateSessionFactory.getSessionFactory();private Session session;Beforepublic void setUp() throws Exception {sessionsessionFactory.openSession(); // 生成一个sessionsession.beginTransaction(); // 开启事务}Afterpublic void tearDown() throws Exception {session.getTransaction().commit(); // 提交事务session.close(); // 关闭session}Testpublic void testSaveClassAndStudent() {Class cnew Class();c.setName(08计本);Student s1new Student();s1.setName(张三);s1.setC(c);Student s2new Student();s2.setName(李四);s2.setC(c);session.save(s1);session.save(s2);}Testpublic void testLoadClass(){// Class c(Class)session.load(Class.class, Long.valueOf(2));Class c(Class)session.load(Class.class, Long.valueOf(1));System.out.println(c.getStudents());}Testpublic void testGetClass(){// Class c(Class)session.get(Class.class, Long.valueOf(2));Class c(Class)session.get(Class.class, Long.valueOf(1));System.out.println(c.getStudents());}Testpublic void testUpdateClass(){Session session1sessionFactory.openSession();session1.beginTransaction();Class c(Class)session1.get(Class.class, Long.valueOf(1));session1.getTransaction().commit(); // 提交事务session1.close();Session session2sessionFactory.openSession();session2.beginTransaction();c.setName(08计算机本科2);session2.update(c);session2.getTransaction().commit(); // 提交事务session2.close();}!--更新--Testpublic void testSaveOrUpdateClass(){Session session1sessionFactory.openSession();session1.beginTransaction();Class c(Class)session1.get(Class.class, Long.valueOf(1));session1.getTransaction().commit(); // 提交事务session1.close();Session session2sessionFactory.openSession();session2.beginTransaction();c.setName(08计算机本科3);Class c2new Class();c2.setName(09计算机本科3);session2.saveOrUpdate(c);session2.saveOrUpdate(c2);session2.getTransaction().commit(); // 提交事务session2.close();}Testpublic void testMergeClass(){Session session1sessionFactory.openSession();session1.beginTransaction();Class c(Class)session1.get(Class.class, Long.valueOf(1));session1.getTransaction().commit(); // 提交事务session1.close();Session session2sessionFactory.openSession();session2.beginTransaction();Class c2(Class)session2.get(Class.class, Long.valueOf(1));c.setName(08计算机本科4);session2.merge(c);session2.getTransaction().commit(); // 提交事务session2.close();}!--删除--Testpublic void testDeleteStudent(){Student student(Student)session.load(Student.class, Long.valueOf(1));session.delete(student);} } package com.oumyye.service;import java.util.Iterator; import java.util.Set;import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.After; import org.junit.Before; import org.junit.Test;import com.oumyye.model.Class; import com.oumyye.model.Student; import com.oumyye.util.HibernateSessionFactory;public class StudentTest {private SessionFactory sessionFactoryHibernateSessionFactory.getSessionFactory();private Session session;Beforepublic void setUp() throws Exception {sessionsessionFactory.openSession(); // 生成一个sessionsession.beginTransaction(); // 开启事务}Afterpublic void tearDown() throws Exception {session.getTransaction().commit(); // 提交事务session.close(); // 关闭session}Testpublic void testSaveClassAndStudent() {Class cnew Class();c.setName(08计本);Student s1new Student();s1.setName(张三);s1.setC(c);Student s2new Student();s2.setName(李四);s2.setC(c);session.save(s1);session.save(s2);}Testpublic void testLoadClass(){// Class c(Class)session.load(Class.class, Long.valueOf(2));Class c(Class)session.load(Class.class, Long.valueOf(1));System.out.println(c.getStudents());}Testpublic void testGetClass(){// Class c(Class)session.get(Class.class, Long.valueOf(2));Class c(Class)session.get(Class.class, Long.valueOf(1));System.out.println(c.getStudents());}Testpublic void testUpdateClass(){Session session1sessionFactory.openSession();session1.beginTransaction();Class c(Class)session1.get(Class.class, Long.valueOf(1));session1.getTransaction().commit(); // 提交事务session1.close();Session session2sessionFactory.openSession();session2.beginTransaction();c.setName(08计算机本科2);session2.update(c);session2.getTransaction().commit(); // 提交事务session2.close();}!--更新--Testpublic void testSaveOrUpdateClass(){Session session1sessionFactory.openSession();session1.beginTransaction();Class c(Class)session1.get(Class.class, Long.valueOf(1));session1.getTransaction().commit(); // 提交事务session1.close();Session session2sessionFactory.openSession();session2.beginTransaction();c.setName(08计算机本科3);Class c2new Class();c2.setName(09计算机本科3);session2.saveOrUpdate(c);session2.saveOrUpdate(c2);session2.getTransaction().commit(); // 提交事务session2.close();}Testpublic void testMergeClass(){Session session1sessionFactory.openSession();session1.beginTransaction();Class c(Class)session1.get(Class.class, Long.valueOf(1));session1.getTransaction().commit(); // 提交事务session1.close();Session session2sessionFactory.openSession();session2.beginTransaction();Class c2(Class)session2.get(Class.class, Long.valueOf(1));c.setName(08计算机本科4);session2.merge(c);session2.getTransaction().commit(); // 提交事务session2.close();}!--删除--Testpublic void testDeleteStudent(){Student student(Student)session.load(Student.class, Long.valueOf(1));session.delete(student);} } Session的入门常用方法 Query query session.createQuery(hql)利用hql查询语句查询Criteria critera session.createCriteria(Class clazz);(3)Transaction tx session.beginTransaction();     //开始事务tx.commit()提交事务session.close();//关闭Session此后被session管理的持久化对象变为脱管状态session.save(Object obj);    //添加session.update(Object obj);     //更新session.delete(Object obj);    //删除Object obj session.get(Class clazz,Serialiazble id);    //根据主键查找记录并返回Object obj session.load(Class clazz,Serializable id);    //和get方法效果一样但是是懒加载即在不使用他之前他不会返回对象转载于:https://www.cnblogs.com/zhujiabin/p/4529851.html
http://www.sadfv.cn/news/452706/

相关文章:

  • 网站推广营销博客关键词优化
  • 建设银行银监会官方网站泾川县建设局网站
  • 网站建设与设计开题报告学生如何建设网站
  • 重庆品牌网站建设怎么样自己可以设计装修的软件
  • 网站设计实训心得江西手机版建站系统开发
  • 丰都县网站杭州市建设工程造价信息网
  • 保洁网站模板金华seo扣费
  • 做网站需要填什么看动漫什么网站好
  • 江门专用网站建设wordpress the7 主题
  • 东莞高端建站公司WordPress数据库备份还原
  • 论坛网站建设流程培训网页设计吗
  • 网站开发技术及应用怎样在网站上做办公家具
  • 网站设计师证书新注册公司网站建设
  • 建设工程质量协会网站设计签名免费名字
  • 河南省住房和建设厅安监站网站网站添加搜索
  • 创新的武进网站建设高端网址
  • 网站建设服务费属于哪个大类成都网站建设、
  • html前端网站开发PPT网站采集来源
  • 在线做logo厦门seo排名优化方式
  • 网站站长要会什么用做网站怎么设置背景
  • 深圳网站搜索引擎优化免费手机h5模板网站模板
  • 湖南网站设计公司常州网站建设策划
  • 手机端网站建设的费用清单网站建设销售经理职责
  • 免费外贸接单网站网站做自己的超链接
  • 网站制作论文题目重庆电子商务网站
  • 遵义网站建设制作公司河南网站推广那家好
  • 一个做音乐的网站山东的互联网公司都有什么
  • 电子商务网站建设收获营销公司
  • 鞍山建设集团网站苏州工业园区网站
  • 河北网站建设案例个人网页主页