网站开发售后服务,咸阳网站建设专业公司哪家好,wordpress开发分类筛选,河间做网站 申梦网络自己亲测的东西才是最有效果的#xff0c;下面贴出整个编写的过程。 1 hibernate环境搭建#xff0c;这个博客非常给力#xff1a;http://www.111cn.net/wy/js-ajax/93142.htm 需要用到的jar包#xff1a;
2 使用myeclipse自动生成model/model配置文件/hibernate.xml配…自己亲测的东西才是最有效果的下面贴出整个编写的过程。 1 hibernate环境搭建这个博客非常给力http://www.111cn.net/wy/js-ajax/93142.htm 需要用到的jar包
2 使用myeclipse自动生成model/model配置文件/hibernate.xml配置文件详细步骤点开下面链接http://jingyan.baidu.com/article/27fa7326e9ef8b46f8271f2a.html 讲解一下大概步骤 1在DB Browser界面生成与数据库的链接 2MyEclipse Java Enterprise界面增加hibernate 功能 3生成实体类、实体类配置文件、Dao文件、hibernate.xml配置文件 4修改Dao文件 具体贴出hibernate.xml配置文件
?xml version1.0 encodingUTF-8?
!DOCTYPE hibernate-configuration PUBLIC-//Hibernate/Hibernate Configuration DTD 3.0//ENhttp://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd
!-- Generated by MyEclipse Hibernate Tools. --
hibernate-configurationsession-factoryproperty namedialect!--方言--org.hibernate.dialect.MySQLDialect/propertyproperty nameconnection.urljdbc:mysql://localhost:3306/hibernate/propertyproperty nameconnection.usernameroot/propertyproperty nameconnection.password1q2w3e4r5t/propertyproperty nameconnection.driver_classcom.mysql.jdbc.Driver/propertyproperty namemyeclipse.connection.profilehibernate/propertyproperty nameshow_sqltrue/propertyproperty nameformat_sqltrue/property!-- 如果spring没有配置事务这里可以设置自动提交 --property namehibernate.connection.autocommittrue/property!-- pojo映射 --mapping resourcecom/zx/model/Customer.hbm.xml //session-factory/hibernate-configuration
model类当然这里就需要在MySql中创建一个Customer表
public class Customer implements java.io.Serializable {// Constructorsprivate Integer cid;private String username;private String password;//省略set、get
}
model配置
?xml version1.0 encodingutf-8?
!DOCTYPE hibernate-mapping PUBLIC -//Hibernate/Hibernate Mapping DTD 3.0//EN
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd
!-- Mapping file autogenerated by MyEclipse Persistence Tools
--
hibernate-mappingclass namecom.zx.model.Customer tablecustomer cataloghibernateid namecid typejava.lang.Integercolumn nameCID /generator classnative //idproperty nameusername typejava.lang.Stringcolumn nameUSERNAME length12 not-nulltrue //propertyproperty namepassword typejava.lang.Stringcolumn namePASSWORD length12 //property/class
/hibernate-mapping
dao层
public class CustomerDAO{private static final Log log LogFactory.getLog(CustomerDAO.class);public static void main(String[] args) {Customer customer new Customer();customer.setUsername(张三);customer.setPassword(123123);CustomerDAO customerDAO new CustomerDAO();customerDAO.save(customer);}public void save(Customer transientInstance) {log.debug(saving Customer instance);try {getSession().save(transientInstance);log.debug(save successful);} catch (RuntimeException re) {log.error(save failed, re);throw re;}}
}
通过上面的代码就可以实现存储了相信删除、更改都不难了。 在以上环境搭建和测试过程中我遇到的问题 1 存储时sql语句打印出来了却没有存储到数据库中很有可能是事物并没有提交需要在hibernate.xml中添加
!-- 如果spring没有配置事务这里可以设置自动提交 --
property namehibernate.connection.autocommittrue/property