绝逼新手小白,so 请大神指点!

如果真的错的太多,错的太离谱,错的误导了其他小伙伴,还望大神请勿喷,大神请担待,大神请高抬贵嘴......谢谢。

好了,正题

刚接触ssh,今天在搞使用.hbm.xml文件 和 注解方式 来自动生成数据表

其中只是整了spring、hibernate,struts部分没有整。也就是说我只是测试了能够自动生成数据表(自动生成为"标准",自认为是对的......)

下面是配置和代码:

使用工具:myeclipse 2014 ,其中web project项目是使用工具自动生成(具体步骤网上小弟也是百度的[一大堆]),

额,绝逼新手,有错请提,勿喷,谢谢。

下面粘代码(俩个项目 目录):

web.xml文件时myeclipse自动生成(俩个一样,就只粘一个了):

 <?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     id="WebApp_ID" version="3.0">
     <display-name>myReply</display-name>
     <welcome-file-list>
         <welcome-file>index.jsp</welcome-file>
     </welcome-file-list>
     <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
     <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:applicationContext.xml</param-value>
     </context-param>
 </web-app>

db.properties (俩个一样,只粘一个了)

 jdbc.user=root
 jdbc.password=123456
 jdbc.driverClass=com.mysql.jdbc.Driver
 jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8

 #3306/test,test是在mysql中自己建的database,具体自己建

 jdbc.initPoolSize=5
 jdbc.maxPoolSize=10

Test.java和Test.hbm.xml就不粘了,额,就是俩个属性和getter/setter方法,.hbm.xml可以在hibernate jar包中搜个模版再改下;

Store.java(注解方式)

 package com.entities;

 import java.io.Serializable;

 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.Table;

 @Entity
 @Table(name="my_store")
 public class Store implements Serializable {

     @Id
     @GeneratedValue(strategy=GenerationType.IDENTITY)
     private int id;

     @Column(name="sname",length=28)
     private String name;

     @Column(name="data")
     private String data;

     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 String getData() {
         return data;
     }

     public void setData(String data) {
         this.data = data;
     }

     public Store() {
         super();
     }

     public Store(int id, String name, String data) {
         super();
         this.id = id;
         this.name = name;
         this.data = data;
     }

 }

hibernate.cfg.xml文件基本一样(只粘一个了):

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>
    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>

        <mapping class="com.entities.Store" />
        <!-- <mapping class="com.entities.Test" /> 我是ssh项目中的,俩个项目就这句话不同-->
    </session-factory>

</hibernate-configuration>

最最重要的就是applicationContext.xml文件(吭扭了半天,还不知道对不对,晕晕乎乎的将项目启动后[项目启动,数据表自动生成],数据表就生成了,感觉好神奇...)

下面是ssh的applicationContext.xml文件(因未写struts部分,所以不完整,这个就是能自动生成数据表!请别喷,我还没写,咳咳,具体还在摸索...)

喔,粘的时候想到个事情,说一下:applicationContext.xml文件中第二行开始的那些(xml namespace)是我网上找的[3.0/4.0自己看导的jar包]......这不是写的时候没提示 我也很捉急麽(发现加上xmlns:..../context 貌似就有提示了,具体小弟也在摸索)。

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

     <context:annotation-config />
     <context:component-scan base-package="com" />

     <!-- 导入资源文件 -->
     <context:property-placeholder location="classpath:db.properties"/>

     <!-- 配置 C3P0 数据源 -->
     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
         <property name="user" value="${jdbc.user}"></property>
         <property name="password" value="${jdbc.password}"></property>
         <property name="driverClass" value="${jdbc.driverClass}"></property>
         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

         <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
         <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
     </bean>

     <!-- 配置 SessionFactory 请注意此处,请注意此处,请注意此处,重要事情说三遍-->
     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
         <property name="dataSource" ref="dataSource"></property>
         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
         <property name="mappingLocations" value="classpath:com/entities/*.hbm.xml"></property>
     </bean>

     <!-- 配置 Spring 的声明式事务 -->
     <!-- 1. 配置 hibernate 的事务管理器 -->
     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory"></property>
     </bean>
 <!--
     2. 配置事务属性
     <tx:advice id="txAdvice" transaction-manager="transactionManager">
         <tx:attributes>
             <tx:method name="get*" read-only="true"/>
             <tx:method name="lastNameIsValid" read-only="true"/>
             <tx:method name="*"/>
         </tx:attributes>
     </tx:advice>

     3. 配置事务切入点, 再把事务属性和事务切入点关联起来
     <aop:config>
         <aop:pointcut expression="execution(* com.service.*.*(..))" id="txPointcut"/>
         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
     </aop:config>
  -->
 </beans>

myReply的applicationContext.xml文件

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/aop
             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
             http://www.springframework.org/schema/tx
             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.0.xsd">

     <context:annotation-config></context:annotation-config>
     <context:component-scan base-package="com"></context:component-scan>

     <!-- 导入资源文件 -->
     <context:property-placeholder location="classpath:db.properties"/>

     <!-- 配置C3P0数据源 -->
     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
         <property name="user" value="${jdbc.user}"></property>
         <property name="password" value="${jdbc.password}"></property>
         <property name="driverClass" value="${jdbc.driver}"></property>
         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

         <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
         <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
     </bean>
     <!-- 请注意此处,请注意此处,请注意此处,重要事情说三遍 -->
     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
         <property name="dataSource" ref="dataSource"></property>
         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
         <!-- <property name="mappingJarLocations" value="classpath:com/entities/*.hbm.xml"></property> -->

         <property name="packagesToScan" value="com"></property>
     </bean>

     <!-- 配置spring的声明式事务 -->
     <!-- 1.配置 Hibernate 的事务管理器 -->
     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory" />
     </bean>

     <tx:annotation-driven transaction-manager="transactionManager" />
 </beans>

咳咳,今天也就搞了这么多,纯粹新手小白啊,具体里面有没有错这个问题我也是表示很尴尬的......

启动项目执行后,数据表是自动生成了...咳咳,我是以这个"标准" 自认为 好像大概可能 是没问题的吧......

这俩天再搞搞前端部分,咳咳,新手还不知道要多少天,看课本看的头晕,还迷糊...

不喜勿喷,小弟也是费了一天的脑细胞才总结出来的,还不知道错不错,就请大神们多担待,

纯粹适合刚接触的新手、和我一样的小白...

hibernate中.hbm.xml和注解方式自动生成数据表的简单实例(由新手小白编写,仅适用新手小白)的更多相关文章

  1. Hibernate映射文件配置(hbm.xml和注解方式)

    一:通过*.hbm.xml配置实体的实现方式 mappingResources用于指定少量的hibernate配置文件像这样 Xml代码  <property name="mappin ...

  2. generator自动生成数据表

    1.先写好自己要创建的字段等: 然后将将上面的在plsql中运行,创建数据表.

  3. Hibernate —— Entity.hbm.xml

    一.简述 1.对象关系映射文件,用于映射实体类和关系数据库数据表之间的一个 xml 文件. 2.通过 Entity.hbm.xml 映射文件,Hibernate 可以理解持久化类和数据表之间的对应关系 ...

  4. hibernate中多对多的注解配置

    hibernate多对多的注解配置中的自动生成中间表的配置: @Entity@Table(name="test_student")public class Students { @ ...

  5. XML(php中获取xml文件的方式/ajax获取xml格式的响应数据的方式)

    1.XML 格式规范: ① 必须有一个根元素 ② 不可有空格.不可以数字或.开头.大小写敏感 ③ 不可交叉嵌套 ④ 属性双引号(浏览器自动修正成双引号了) ⑤ 特殊符号要使用实体 ⑥ 注释和HTML一 ...

  6. Hibernate中@Embedded和@Embeddable注解

    在使用实体类生成对应的数据库表时,很多的时候都会遇到这种情况:在一个实体类中引用另外的实体类,一般遇上这种情况,我们使用@OneToOne.@OneToMany.@ManyToOne.@ManyToM ...

  7. 使用Java注解开发自动生成SQL

    使用注解开发的好处就是减少配置文件的使用.在实际过程中,随着项目越来越复杂,功能越来越多,会产生非常多的配置文件.但是,当配置文件过多,实际维护过程中产生的问题就不容易定位,这样就会徒劳的增加工作量. ...

  8. Eclipse中设置在创建新类时自动生成注释

    方法一:Eclipse中设置在创建新类时自动生成注释 windows-->preference Java-->Code Style-->Code Templates code--&g ...

  9. Mybatis总结之如何自动生成数据库表结构

    一般情况下,用Mybatis的时候是先设计表结构再进行实体类以及映射文件编写的,特别是用代码生成器的时候. 但有时候不想用代码生成器,也不想定义表结构,那怎么办? 这个时候就会想到Hibernate, ...

随机推荐

  1. DOM之节点|属性

    1.查询文档的一个或多个元素有如下方法 a. 用指定的id属性:(若一个文档中有两个相同的id,只会选择第一个;在低于IE8的IE中,getElementById()对匹配元素的ID不区分大小写,而且 ...

  2. eclipse Content Assist 无法使用,不能自动补全的解决办法

    今天用eclipse写JAVA代码,写着写着突然,eclipse 的自动补全功能失效了,没办法自动补全.折腾半天,终于解决了. 在window->Preferences->Java-> ...

  3. IOS 开发之文件管理

    一.iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立.封闭.安全的空间,叫做沙盒.它一般存放着程序包文件(可执行文件).图片.音频.视频.plist文件.sqlite数据库 ...

  4. 每次从vss获取文件都是只读

    在 Visual Studio 2008 中,使用 VSS 作为源码管理器,把文件签入后,文件会自动变为只读状态.在公司习惯是在某个开发人员的机器上获取最新的代码后编译,编译完之后服务器上再从 VSS ...

  5. iOS数据持久化 -- Core Data-备用

    Core Data是一个功能强大的层,位于SQLite数据库之上,它避免了SQL的复杂性,能让我们以更自然的方式与数据库进行交互.Core Data将数据库行转换为OC对象(托管对象)来实现,这样无需 ...

  6. Effective Java实作hashCode() - 就是爱Java

    hashCode()这个方法,也是定义在Object class中,这个是所有class的base class,因此所有的class也都继承这个方法,预设是传回这个对象储存的内存地址编号,因为Mix覆 ...

  7. RMAN学习笔记

    RMAN:如果RMAN连接一个远程数据库,格式:RMAN>rman target sys/jxsrpv@test 1.列出备份信息,所有的备份信息 RMAN>list backup of ...

  8. bitmap 内存溢出OOM的解决办法分享

    昨天遇到这个问题就是从一个输入流里调用BitmapFactory.decodeStream(this.getContentResolver().openInputStream(uri))得到一个bit ...

  9. mybatis常用操作

    一.增 1.1 单条 <insert id="addUser" parameterType="com.xxx.model.UserInfo" useGen ...

  10. 如何使用WCF调试器WcfTestClient.exe

    如果启用服务出现如下异常 请启用下面该服务 C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE 文件->添加服务  地 ...