hibernate中.hbm.xml和注解方式自动生成数据表的简单实例(由新手小白编写,仅适用新手小白)
绝逼新手小白,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和注解方式自动生成数据表的简单实例(由新手小白编写,仅适用新手小白)的更多相关文章
- Hibernate映射文件配置(hbm.xml和注解方式)
一:通过*.hbm.xml配置实体的实现方式 mappingResources用于指定少量的hibernate配置文件像这样 Xml代码 <property name="mappin ...
- generator自动生成数据表
1.先写好自己要创建的字段等: 然后将将上面的在plsql中运行,创建数据表.
- Hibernate —— Entity.hbm.xml
一.简述 1.对象关系映射文件,用于映射实体类和关系数据库数据表之间的一个 xml 文件. 2.通过 Entity.hbm.xml 映射文件,Hibernate 可以理解持久化类和数据表之间的对应关系 ...
- hibernate中多对多的注解配置
hibernate多对多的注解配置中的自动生成中间表的配置: @Entity@Table(name="test_student")public class Students { @ ...
- XML(php中获取xml文件的方式/ajax获取xml格式的响应数据的方式)
1.XML 格式规范: ① 必须有一个根元素 ② 不可有空格.不可以数字或.开头.大小写敏感 ③ 不可交叉嵌套 ④ 属性双引号(浏览器自动修正成双引号了) ⑤ 特殊符号要使用实体 ⑥ 注释和HTML一 ...
- Hibernate中@Embedded和@Embeddable注解
在使用实体类生成对应的数据库表时,很多的时候都会遇到这种情况:在一个实体类中引用另外的实体类,一般遇上这种情况,我们使用@OneToOne.@OneToMany.@ManyToOne.@ManyToM ...
- 使用Java注解开发自动生成SQL
使用注解开发的好处就是减少配置文件的使用.在实际过程中,随着项目越来越复杂,功能越来越多,会产生非常多的配置文件.但是,当配置文件过多,实际维护过程中产生的问题就不容易定位,这样就会徒劳的增加工作量. ...
- Eclipse中设置在创建新类时自动生成注释
方法一:Eclipse中设置在创建新类时自动生成注释 windows-->preference Java-->Code Style-->Code Templates code--&g ...
- Mybatis总结之如何自动生成数据库表结构
一般情况下,用Mybatis的时候是先设计表结构再进行实体类以及映射文件编写的,特别是用代码生成器的时候. 但有时候不想用代码生成器,也不想定义表结构,那怎么办? 这个时候就会想到Hibernate, ...
随机推荐
- freemarker中遍历list<map<String,String>>
<#list var as map><tr> <#list map?keys as itemKey> //关键点 <#if itemKey=" ...
- HTML5视频
<video>标签用于定义视频. 案例1: <!DOCTYPE html><html><head lang="en"> <me ...
- LUA 捕获模式 URL编码的例子解析
function escape(s) s=string.gsub(s,"([&=+%c])",function(c) return string.format(" ...
- Hdu1092
#include <stdio.h> int main() { ; while(scanf("%d",&n)){ ) {;} else{ int i,a; ;i ...
- Linux(Centos)下安装MySQL
转载:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html 一.mysql简介 说到数据库,我们大多想到的是关 ...
- (摘) MDI登陆问题
MDI编程中需要验证用户身份,那么登陆窗口就需要在验证密码后进行相关的隐藏处理. (1)隐藏登陆窗口(登陆窗体作为启动) 登陆按钮事件:this.Hide();//隐藏登陆窗口MDI_Name M = ...
- Java BufferedWriter与BufferedReader操作文本文件
/** * 采用字符流读取写入文本文件 */ public class FileUtil { /** * 写文件 * @param fileName * @param content */ publi ...
- KEIL C51 Call Tree
KEIL中函数的调用在其帮助文档中有一个详细的解释,引用如下: The Call Tree The best way to demonstrate how the call tree is gener ...
- el简略说明与11个隐含对象
El的特点: el语法: El11个隐含对象:
- pyqt字符串分离开,放入列表中
string1 = ''''' the stirng Has many line In THE fIle ''' list_of_string = string1.split() print list ...