之前的项目中用到了maven,而且是web项目(但不是标准的ssh架构),趁着假期,重用南哥的pom文件,自己也试试用maven构建web项目,搭建完整的ssh开发框架。
工具准备:jdk,eclipse,maven
Eclipse 插件:Hibernate Tools
(以上工具软件请请自行Google下载安装)
当然,构建过程需要畅通的网络,以确保maven能顺利下载依赖包。
第一步,在eclipse新建Maven项目,
New->Maven Project;
两个next之后选择maven-archetype-webapp 架构类型;
Next,然后设置好Group Id,Artifact Id,等信息,之后点击Finish。
至此,web项目已经搭建好。
在src/main目录下建立java和test代码文件夹,
下面进行maven依赖配置和ssh配置
下面只需要配置项目根目录下的pom.xml文件,配置ssh框架的各种依赖jar包,并设定Java版本,Maven默认是1.5
pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>edu.ncut.decloud.test</groupId> 5 <artifactId>mvntest</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>mvntest Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <properties> 11 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 12 <cxf.version>2.7.4</cxf.version> 13 <org.springframework.version>3.2.2.RELEASE</org.springframework.version> 14 <servlet-api.version>2.5</servlet-api.version> 15 <jsp-api.version>2.2</jsp-api.version> 16 <commons-logging.version>1.1.2</commons-logging.version> 17 <jsr311-api.version>1.1.1</jsr311-api.version> 18 <json.version>20090211</json.version> 19 <log4j.version>1.2.17</log4j.version> 20 <jstl.version>1.2</jstl.version> 21 <mysql.version>5.1.30</mysql.version> 22 <struts2.version>2.3.16.3</struts2.version> 23 <hibernate.version>4.2.2.Final</hibernate.version> 24 <hibernate-commons.version>3.3.0.ga</hibernate-commons.version> 25 <hibernate-annotations.version>3.5.6-Final</hibernate-annotations.version> 26 <slf4j.version>1.7.5</slf4j.version> 27 <c3p0.version>0.9.1.2</c3p0.version> 28 <commons-lang.version>2.6</commons-lang.version> 29 </properties> 30 <dependencies> 31 <dependency> 32 <groupId>junit</groupId> 33 <artifactId>junit</artifactId> 34 <version>4.11</version> 35 <scope>test</scope> 36 </dependency> 37 <dependency> 38 <groupId>org.springframework</groupId> 39 <artifactId>spring-core</artifactId> 40 <version>${org.springframework.version}</version> 41 </dependency> 42 <!-- Expression Language(depends on spring-core) Define this if you use 43 Spring Expression APIs(org.springframework.expression.*) --> 44 45 <dependency> 46 <groupId>org.springframework</groupId> 47 <artifactId>spring-expression</artifactId> 48 <version>${org.springframework.version}</version> 49 </dependency> 50 51 <!-- Aspect Oriented Programing(AOP) Framework (depends on spring-core,spring-beans) 52 Define this if you use Spring AOP APIs(org.springframework.aop.*) --> 53 54 <dependency> 55 <groupId>org.springframework</groupId> 56 <artifactId>spring-aop</artifactId> 57 <version>${org.springframework.version}</version> 58 </dependency> 59 60 <!-- Application Context (depends on spring-core, spring-expression, spring-aop, 61 spring-beans) This is the central artifact for Spring's Dependency Injection 62 Container and is generally always defined --> 63 64 <dependency> 65 <groupId>org.springframework</groupId> 66 <artifactId>spring-context</artifactId> 67 <version>${org.springframework.version}</version> 68 </dependency> 69 70 <!-- Various Application Context utilities, including EhCache, JavaMail, 71 Quartz, and Freemarker integration Define this if you need any of these integrations --> 72 73 <dependency> 74 <groupId>org.springframework</groupId> 75 <artifactId>spring-context-support</artifactId> 76 <version>${org.springframework.version}</version> 77 </dependency> 78 79 <!-- Transaction Management Abstraction(depends on spring-core,spring-beans,spring-aop,spring-context) 80 Define this if you use Spring Transactions or DAO Exception Hierarchy (org.springframework.transaction.* 81 /org.springframework.dao.*) --> 82 83 <dependency> 84 <groupId>org.springframework</groupId> 85 <artifactId>spring-tx</artifactId> 86 <version>${org.springframework.version}</version> 87 </dependency> 88 89 <!-- JDBC Data Access Library (depends on spring-core, spring-beans, spring-context, 90 spring-tx) Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*) --> 91 92 <dependency> 93 <groupId>org.springframework</groupId> 94 <artifactId>spring-jdbc</artifactId> 95 <version>${org.springframework.version}</version> 96 </dependency> 97 98 <!-- Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA, 99 and iBatis. (depends on spring-core, spring-beans, spring-context, spring-tx) 100 Define this if you need ORM (org.springframework.orm.*) --> 101 <dependency> 102 <groupId>org.springframework</groupId> 103 <artifactId>spring-orm</artifactId> 104 <version>${org.springframework.version}</version> 105 </dependency> 106 107 <!-- Object-to-XML Mapping (OXM) abstraction and integration with JAXB, 108 JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-beans, 109 spring-context) Define this if you need OXM (org.springframework.oxm.*) --> 110 111 <dependency> 112 <groupId>org.springframework</groupId> 113 <artifactId>spring-oxm</artifactId> 114 <version>${org.springframework.version}</version> 115 </dependency> 116 117 <!-- Web application development utilities applicable to both Servlet and 118 Portlet Environments (depends on spring-core, spring-beans, spring-context) 119 Define this if you use Spring MVC, or wish to use Struts, JSF, or another 120 web framework with Spring (org.springframework.web.*) --> 121 122 <dependency> 123 <groupId>org.springframework</groupId> 124 <artifactId>spring-web</artifactId> 125 <version>${org.springframework.version}</version> 126 </dependency> 127 128 <!-- Spring MVC for Servlet Environments (depends on spring-core, spring-beans, 129 spring-context, spring-web) Define this if you use Spring MVC with a Servlet 130 Container such as Apache Tomcat (org.springframework.web.servlet.*) --> 131 132 <dependency> 133 <groupId>org.springframework</groupId> 134 <artifactId>spring-webmvc</artifactId> 135 <version>${org.springframework.version}</version> 136 </dependency> 137 138 <!-- Spring MVC for Portlet Environments (depends on spring-core, spring-beans, 139 spring-context, spring-web) Define this if you use Spring MVC with a Portlet 140 Container (org.springframework.web.portlet.*) --> 141 142 <dependency> 143 <groupId>org.springframework</groupId> 144 <artifactId>spring-webmvc-portlet</artifactId> 145 <version>${org.springframework.version}</version> 146 </dependency> 147 148 <!-- Support for testing Spring applications with tools such as JUnit and 149 TestNG This artifact is generally always defined with a 'test' scope for 150 the integration testing framework and unit testing stubs --> 151 152 <dependency> 153 <groupId>org.springframework</groupId> 154 <artifactId>spring-test</artifactId> 155 <version>${org.springframework.version}</version> 156 <scope>test</scope> 157 </dependency> 158 159 160 <dependency> 161 <groupId>org.springframework</groupId> 162 <artifactId>spring-aspects</artifactId> 163 <version>${org.springframework.version}</version> 164 </dependency> 165 166 <dependency> 167 <groupId>org.springframework</groupId> 168 <artifactId>spring-beans</artifactId> 169 <version>${org.springframework.version}</version> 170 </dependency> 171 <!-- Spring3.2.2配置结束 --> 172 173 <!-- 可能与tomcat 的 servlet冲突 --> 174 <dependency> 175 <groupId>javax.servlet</groupId> 176 <artifactId>servlet-api</artifactId> 177 <version>${servlet-api.version}</version> 178 <scope>provided</scope> 179 </dependency> 180 <dependency> 181 <groupId>javax.servlet.jsp</groupId> 182 <artifactId>jsp-api</artifactId> 183 <version>${jsp-api.version}</version> 184 <scope>provided</scope> 185 </dependency> 186 187 <dependency> 188 <groupId>org.slf4j</groupId> 189 <artifactId>slf4j-api</artifactId> 190 <version>${slf4j.version}</version> 191 </dependency> 192 <dependency> 193 <groupId>org.slf4j</groupId> 194 <artifactId>slf4j-log4j12</artifactId> 195 <version>${slf4j.version}</version> 196 </dependency> 197 198 <dependency> 199 <groupId>org.slf4j</groupId> 200 <artifactId>slf4j-nop</artifactId> 201 <version>${slf4j.version}</version> 202 </dependency> 203 204 <dependency> 205 <groupId>log4j</groupId> 206 <artifactId>log4j</artifactId> 207 <version>${log4j.version}</version> 208 </dependency> 209 <dependency> 210 <groupId>jstl</groupId> 211 <artifactId>jstl</artifactId> 212 <version>${jstl.version}</version> 213 </dependency> 214 <dependency> 215 <groupId>net.sf.json-lib</groupId> 216 <artifactId>json-lib</artifactId> 217 <version>2.4</version> 218 <classifier>jdk15</classifier> 219 </dependency> 220 221 <!-- mysql --> 222 <dependency> 223 <groupId>mysql</groupId> 224 <artifactId>mysql-connector-java</artifactId> 225 <version>${mysql.version}</version> 226 </dependency> 227 228 <!-- hibernate --> 229 <dependency> 230 <groupId>c3p0</groupId> 231 <artifactId>c3p0</artifactId> 232 <version>${c3p0.version}</version> 233 </dependency> 234 <dependency> 235 <groupId>org.hibernate</groupId> 236 <artifactId>hibernate-core</artifactId> 237 <version>${hibernate.version}</version> 238 </dependency> 239 <dependency> 240 <groupId>org.hibernate</groupId> 241 <artifactId>hibernate-validator</artifactId> 242 <version>5.0.1.Final</version> 243 </dependency> 244 <!-- hibernate --> 245 246 <dependency> 247 <groupId>javax.validation</groupId> 248 <artifactId>validation-api</artifactId> 249 <version>1.1.0.Final</version> 250 </dependency> 251 <dependency> 252 <groupId>edu.ncut.decloud</groupId> 253 <artifactId>traffic-common</artifactId> 254 <version>1.0.2</version> 255 </dependency> 256 <!--struts --> 257 <dependency> 258 <groupId>org.apache.struts</groupId> 259 <artifactId>struts2-core</artifactId> 260 <version>${struts2.version}</version> 261 </dependency> 262 <dependency> 263 <groupId>org.apache.struts</groupId> 264 <artifactId>struts2-convention-plugin</artifactId> 265 <version>${struts2.version}</version> 266 </dependency> 267 <dependency> 268 <groupId>org.apache.struts</groupId> 269 <artifactId>struts2-junit-plugin</artifactId> 270 <version>${struts2.version}</version> 271 <scope>test</scope> 272 </dependency> 273 <dependency> 274 <groupId>org.apache.struts</groupId> 275 <artifactId>struts2-config-browser-plugin</artifactId> 276 <version>${struts2.version}</version> 277 </dependency> 278 <dependency> 279 <groupId>org.apache.struts</groupId> 280 <artifactId>struts2-embeddedjsp-plugin</artifactId> 281 <version>${struts2.version}</version> 282 </dependency> 283 <dependency> 284 <groupId>org.apache.struts</groupId> 285 <artifactId>struts2-spring-plugin</artifactId> 286 <version>${struts2.version}</version> 287 </dependency> 288 <dependency> 289 <groupId>org.apache.struts</groupId> 290 <artifactId>struts2-json-plugin</artifactId> 291 <version>${struts2.version}</version> 292 </dependency> 293 <!--struts --> 294 295 296 297 </dependencies> 298 <build> 299 <finalName>mvntest</finalName> 300 <resources> 301 <resource> 302 <directory>src/main/java</directory> 303 <!-- 此目录下的所有xml文件作为资源打包 --> 304 <includes> 305 <include>**/*.xml</include> 306 </includes> 307 </resource> 308 <resource> 309 <directory>src/main/resources</directory> 310 <!-- 此目录下的所有xml文件作为资源打包 --> 311 <includes> 312 <include>**/*.xml</include> 313 <include>**/*.properties</include> 314 </includes> 315 </resource> 316 </resources> 317 318 <plugins> 319 <plugin> 320 <groupId>org.apache.maven.plugins</groupId> 321 <artifactId>maven-compiler-plugin</artifactId> 322 <configuration> 323 <source>1.7</source> 324 <target>1.7</target> 325 <encoding>UTF-8</encoding> 326 </configuration> 327 </plugin> 328 329 <plugin> 330 <artifactId>maven-war-plugin</artifactId> 331 <version>2.3</version> 332 <configuration> 333 <webXml>src/main/webapp/WEB-INF/web.xml</webXml> 334 </configuration> 335 </plugin> 336 337 <plugin> 338 <groupId>org.apache.maven.plugins</groupId> 339 <artifactId>maven-surefire-plugin</artifactId> 340 <version>2.6</version> 341 <configuration> 342 <skipTests>true</skipTests> 343 </configuration> 344 </plugin> 345 </plugins> 346 </build> 347 </project>
更改pom.xml之后,在项目右键,Maven -> Update Project,配置已更新,ssh各种依赖包已导入。
下面是各种配置文件,web.xml在webapp目录下的WEB-INF下,spring配置文件spring-config.xml我把它放在了web.xml同级的spring配置文件夹下,可自行更改,只需在web.xml中指定即可。
在src/main/resources/下放置hibernate 配置文件,struts 2 配置文件,log4j等一系列配置
web.xml
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Archetype Created Web Application</display-name> <!-- <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> --> <!-- 上下文配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/conf/spring/spring-config.xml</param-value> </context-param> <listener> <description>springListerner</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置struts2 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <!-- <init-param> <param-name>config</param-name> <param-value>struts.xml</param-value> </init-param> --> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
spring-config.xml,其中包含了对hibernate的配置
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!-- 配置扫描注解 --> <context:component-scan base-package="edu.ncut.decloud.mvnssh"> <!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> --> </context:component-scan> <!-- 自动注解映射的支持,下面是一种简写形式,完全可以手动配置替代这种简写形式,它会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter,是spring MVC为@Controllers分发请求所必须的 --> <mvc:annotation-driven> </mvc:annotation-driven> <!-- 加载propertis文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <!-- 配置数据库连接spring --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${connection.driver_class}"/> <property name="url" value="${connection.url}"/> <property name="username" value="${connection.username}"/> <property name="password" value="${connection.password}"/> </bean> <!-- 配置hibernate相关信息 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" > <property name="dataSource" ref="dataSource"/> <!-- <property name="packagesToScan"> <list> <value>edu.ncut.decloud.mvnssh.domain</value> </list> </property> --> <property name="mappingDirectoryLocations"> <list> <value>classpath:edu/ncut/decloud/mvnssh/domain</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.query.substitutions">${hibernate.query.substitutions}</prop> <prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop> <prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop> <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop> <prop key="hibernate.bytecode.use_reflection_optimizer">${hibernate.bytecode.use_reflection_optimizer}</prop> <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop> <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop> <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop> <prop key="net.sf.ehcache.configurationResourceName">${net.sf.ehcache.configurationResourceName}</prop> <prop key="hibernate.cache.use_structured_entries">${hibernate.cache.use_structured_entries}</prop> <!-- 解决no session found <prop key="hibernate.current_session_context_class">thread</prop> --> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> </props> </property> </bean> <!-- 开启AOP监听 只对当前配置文件有效 --> <!-- <aop:aspectj-autoproxy expose-proxy="true"/> --> <aop:aspectj-autoproxy /> <!-- 开启注解事务 只对当前配置文件有效 --> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="merge*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="put*" propagation="REQUIRED" /> <tx:method name="use*" propagation="REQUIRED"/> <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到--> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="count*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="list*" propagation="REQUIRED" read-only="true" /> <tx:method name="query*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <!-- 只对DAO层实施事务 --> <aop:config expose-proxy="true"> <aop:pointcut id="transactionPointcut" expression="execution(* edu.ncut.decloud..dao..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/> </aop:config> </beans>
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <!-- caution: in release version, this value must be set false --> <constant name="struts.devMode" value="true"></constant> <!-- 将Action的创建交给spring来管理 --> <constant name="struts.objectFactory" value="spring" /> <package name="mvnssh" extends="json-default"> <!-- 配置拦截器 --> <interceptors> <interceptor-stack name="appDefaultStack"> <interceptor-ref name="defaultStack"> <param name="exception.logEnabled">true</param> <param name="exception.logLevel">ERROR</param> </interceptor-ref> </interceptor-stack> <!-- <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor" /> --> </interceptors> <default-interceptor-ref name="appDefaultStack" /> <global-results> <result name="error">/error.jsp</result> </global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="error" /> </global-exception-mappings> <action name="personFinder" class="edu.ncut.decloud.mvnssh.action.PersonFinder" method="execute"> <result name="success">/personinfo.jsp</result> </action> <action name="allPersonsFinder" class="edu.ncut.decloud.mvnssh.action.AllPersonsFinder" method="execute"> <result name="success">/personinfo.jsp</result> </action> <action name="*PersonUpdate" class="edu.ncut.decloud.mvnssh.action.PersonUpdater" method="{1}"> <result name="success">/personupdated.jsp</result> <result name="input">/inputpersonupdate.jsp</result> </action> <action name="personDelete" class="edu.ncut.decloud.mvnssh.action.PersonDeleter" method="execute"> <result name="success">/persondeleted.jsp</result> </action> <action name="*PersonSave" class="edu.ncut.decloud.mvnssh.action.PersonSaver" method="{1}"> <result name="input">/inputpersonsave.jsp</result> <result name="success">/personsaved.jsp</result> </action> </package> </struts>
jdbc.properties hibernate参数配置
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.hbm2ddl.auto=none
hibernate.show_sql=true
hibernate.query.substitutions=true 1, false 0
hibernate.default_batch_fetch_size=16
hibernate.max_fetch_depth=2
hibernate.bytecode.use_reflection_optimizer=true
hibernate.cache.use_second_level_cache=false
hibernate.cache.use_query_cache=false
#hibernate.cache.region.factory_class=org.hibernate.cache.EhCacheRegionFactory
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
net.sf.ehcache.configurationResourceName=/ehcache_hibernate.xml
hibernate.cache.use_structured_entries=true
hibernate.generate_statistics=true
connection.driver_class=com.mysql.jdbc.Driver
connection.url=jdbc:mysql://localhost:3306/mvnssh
connection.username=root
connection.password=
proxool.maximum.connection.count=40
proxool.minimum.connection.count=5
proxool.statistics=1m,15m,1h,1d
proxool.simultaneous.build.throttle=30
至此,基于Maven的ssh 架构web开发环境就搭建完成了。
一些收获,做过一些项目后,重用已有的架构和相应的组件会减少造轮子的过程,省时省力-.-
下面附上通过Hibernate Tools来自动生成关系数据库里对应的实体类和相应的映射文件(映射文件的位置可在spring-config.xml中配置)
首先打开Hibernate Tools视图,新建配置
Hibernate configured connection 点击New,新建数据库连接,首先选择数据库类型
next,配置数据库连接信息
点击右上角,添加数据库驱动jar包。
至此,数据库连接配置完毕。
下面为项目生成实体类和相应的配置文件,
在Hibernate Tools视图下运行Hibernate Code 生成配置
配置基本信息
可以在reveng.xml配置映射那些表,不设置则默认全部映射。
然后在Exporters里配置生成哪些文件
点击 Run就可以在指定的输出包里看到相应的实体类和对应的Hibernate映射文件。
That's all. Good Luck. - - !