用Maven整合SpringMVC+Spring+Hibernate 框架,实现简单的插入数据库数据功能

用Maven整合SpringMVC+Spring+Hibernate 框架,实现简单的插入数据库数据功能

分类: 环境IDE Java Web Maven Spring SpringMVC Hibernate   1696人阅读  评论(0)  收藏  举报

一、搭建开始前的准备

1、我用的MyEclipse2014版,大家也可以用IDEA。

2、下载Tomcat(免安装解压包)、MySQL(zip包下载地址 免安装解压包,好处就是双击启动,最后我会把bat的启动发给大家)、用的Navicat for MySQL的MySQL的图形界面工具。

3、配置环境变量CATALINA_HOME(path中添加%CATALINA_HOME%\lib;%CATALINA_HOME%\bin;)、MySQL_HOME(path中添加%MySQL_HOME%\bin;)

4、启动mysql和Tomcat试试能否正常运行。

5、打开MyEclipse新建Maven项目。File>New>Other(或Ctrl+N)>Maven Project;然后我们用default Workspace就行了(注意Location的路径,区分目录名和项目名);然后再Filter中输入webapp,我们选择org.apache.maven.archetypes 下的maven-archetype-webapp;点击下一步,填入Group Id(公司名,一般为com.xxx),Artifact Id(项目名),包名(一般根据公司名和项目名生成),单机Finish就创建完一个Maven的Web项目了。注:创建出来后有时候会有错(具体是index.jsp)这是因为我们没有在pom.xml中添加servlet的原因,自己加上就OK了。

6、一般创建出来的项目,只有src/main/resources,然后我们加上其他的三项(必须是新建Source Folder!src/main/java; src/test/resources; src/test/java),有时候会提示你该文件夹已经存在,我们稍改一下名,等创建完了后再用F2改回正确的就OK了(例如src/main/java创建失败,我们把java改为javas,最后再改为java即可。)

最终创建出来的目录如图:

用Maven整合SpringMVC+Spring+Hibernate 框架,实现简单的插入数据库数据功能_第1张图片

7、万事具备只欠搭建,好吧下面才开始进入正文。

二、搭建

1、我习惯先建立一些package包名(src/main/java),例如:xxx.xxx.dao;xxx.xxx.dao.impl;xxx.xxx.model;xxx.xxx.service;xxx.xxx.service.impl;xxx.xxx.controller;具体的包应该放什么,起什么作用,大家可以百度。

2、配置资源文件,也就是所谓的src/main/resources下的文件。

(1.)config.properties

[html]  view plain copy
  1. #\u5C5E\u6027\u6587\u4EF6  
  2.   
  3. #Oracle \u7684\u914D\u7F6E  
  4. #hibernate.dialect=org.hibernate.dialect.OracleDialect  
  5. #driverClassName=oracle.jdbc.driver.OracleDriver  
  6. #validationQuery=SELECT 1 FROM DUAL  
  7. #jdbc_url=jdbc:oracle:thin:@localhost:1521:orcl  
  8. #jdbc_username=  
  9. #jdbc_password=  
  10.   
  11. #SQLServer \u7684\u914D\u7F6E  
  12. #hibernate.dialect=org.hibernate.dialect.SQLServerDialect  
  13. #driverClassName=net.sourceforge.jtds.jdbc.Driver  
  14. #validationQuery=SELECT 1  
  15. #jdbc_url=jdbc:jtds:sqlserver://127.0.0.1:1433/sy  
  16. #jdbc_username=  
  17. #jdbc_password=  
  18.   
  19. #Derby \u7684\u914D\u7F6E  
  20. #hibernate.dialect=org.hibernate.dialect.DerbyDialect  
  21. #driverClassName=org.apache.derby.jdbc.EmbeddedDriver  
  22. #validationQuery=SELECT 1  
  23. #jdbc_url=jdbc:derby:sy;create=true  
  24. #jdbc_username=  
  25. #jdbc_password=  
  26.   
  27. #MySQL \u7684\u914D\u7F6E  
  28. hibernate.dialect=org.hibernate.dialect.MySQLDialect  
  29. driverClassName=com.mysql.jdbc.Driver  
  30. validationQuery=SELECT 1  
  31. jdbc_url=jdbc:mysql://localhost:3306/sshf?useUnicode=true&characterEncoding=utf-8  
  32. jdbc_username=root  
  33. jdbc_password=  
  34.   
  35. hibernate.hbm2ddl.auto=update  
  36. hibernate.show_sql=true  
  37. hibernate.format_sql=false  
  38.   
  39. sessionInfoName=sessionInfo  
  40.   
  41. uploadFieldName=filedata  
  42. uploadFileMaxSize=20971520  
  43. uploadFileExts=txt,rar,zip,doc,docx,xls,xlsx,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid  
  44. uploadDirectory=attached  

注意: ?useUnicode=true&characterEncoding=utf-8 这句话,是UTF8的编码。注释的部分是其他数据库的编写形式。

(2.)log4j.properties

[html]  view plain copy
  1. log4j.rootLogger=INFO,A1,R  
  2.   
  3. log4j.appender.A1=org.apache.log4j.ConsoleAppender  
  4. log4j.appender.A1.Target=System.out  
  5. log4j.appender.A1.layout=org.apache.log4j.PatternLayout  
  6. log4j.appender.A1.layout.ConversionPattern=[%c]%m%n  
  7.   
  8. log4j.appender.R=org.apache.log4j.RollingFileAppender   
  9. log4j.appender.R.File=sshf.log  
  10. log4j.appender.R.MaxFileSize=10MB  
  11. log4j.appender.R.Threshold=ALL  
  12. log4j.appender.R.layout=org.apache.log4j.PatternLayout  
  13. log4j.appender.R.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n  

注意: sshf.log 就是输出日志文件的文件名

(3.)spring.xml

[html]  view plain copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <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" xsi:schemaLocation="  
  3. http://www.springframework.org/schema/beans   
  4. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  5. http://www.springframework.org/schema/context   
  6. http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  7. ">  
  8.   
  9.       
  10.     <context:property-placeholder location="classpath:config.properties" />  
  11.   
  12.       
  13.     <context:component-scan base-package="com.jesus.sshframework.dao,com.jesus.sshframework.service" />  
  14.   
  15. beans>  
没什么可说的,要说的也在注释里了。

(4.)spring-mvc.xml

[html]  view plain copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  6.       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  7.       http://www.springframework.org/schema/context    
  8.       http://www.springframework.org/schema/context/spring-context-3.1.xsd    
  9.       http://www.springframework.org/schema/mvc    
  10.       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  11.       
  12.     <context:component-scan base-package="com.jesus.sshframework.controller" />  
  13.       
  14.     <mvc:annotation-driven />  
  15.   
  16.     <bean id="viewResolver"  
  17.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  18.         <property name="prefix" value="/">property>  
  19.         <property name="suffix" value=".jsp">property>  
  20.     bean>  
  21. beans>    

(5.)spring-hibernate.xml(我喜欢这样写,这样能知道具体用的什么持久层框架,如果用myBatis,则spring-mybatis.xml就可以了)

[html]  view plain copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="  
  3. http://www.springframework.org/schema/beans   
  4. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  5. http://www.springframework.org/schema/tx   
  6. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  7. http://www.springframework.org/schema/aop   
  8. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  9. ">  
  10.   
  11.       
  12.       
  13.       
  14.       
  15.       
  16.     <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
  17.         <property name="url" value="${jdbc_url}" />  
  18.         <property name="username" value="${jdbc_username}" />  
  19.         <property name="password" value="${jdbc_password}" />  
  20.   
  21.           
  22.         <property name="initialSize" value="0" />  
  23.           
  24.         <property name="maxActive" value="20" />  
  25.           
  26.         <property name="maxIdle" value="20" />  
  27.           
  28.         <property name="minIdle" value="0" />  
  29.           
  30.         <property name="maxWait" value="60000" />  
  31.   
  32.           
  33.   
  34.         <property name="validationQuery" value="${validationQuery}" />  
  35.         <property name="testOnBorrow" value="false" />  
  36.         <property name="testOnReturn" value="false" />  
  37.         <property name="testWhileIdle" value="true" />  
  38.   
  39.           
  40.         <property name="timeBetweenEvictionRunsMillis" value="60000" />  
  41.           
  42.         <property name="minEvictableIdleTimeMillis" value="25200000" />  
  43.   
  44.           
  45.         <property name="removeAbandoned" value="true" />  
  46.           
  47.         <property name="removeAbandonedTimeout" value="1800" />  
  48.           
  49.         <property name="logAbandoned" value="true" />  
  50.   
  51.           
  52.           
  53.         <property name="filters" value="mergeStat" />  
  54.     bean>  
  55.   
  56.       
  57.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  58.         <property name="dataSource" ref="dataSource" />  
  59.         <property name="hibernateProperties">  
  60.             <props>  
  61.                 <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}prop>  
  62.                 <prop key="hibernate.dialect">${hibernate.dialect}prop>  
  63.                 <prop key="hibernate.show_sql">${hibernate.show_sql}prop>  
  64.                 <prop key="hibernate.format_sql">${hibernate.format_sql}prop>  
  65.             props>  
  66.         property>  
  67.   
  68.           
  69.         <property name="packagesToScan">  
  70.             <list>  
  71.                 <value>com.jesus.sshframework.modelvalue>  
  72.             list>  
  73.         property>  
  74.     bean>  
  75.   
  76.       
  77.     <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
  78.         <property name="sessionFactory" ref="sessionFactory">property>  
  79.     bean>  
  80.   
  81.       
  82.       
  83.   
  84.       
  85.     <tx:advice id="transactionAdvice" transaction-manager="transactionManager">  
  86.         <tx:attributes>  
  87.             <tx:method name="add*" />  
  88.             <tx:method name="save*" />  
  89.             <tx:method name="update*" />  
  90.             <tx:method name="modify*" />  
  91.             <tx:method name="edit*" />  
  92.             <tx:method name="delete*" />  
  93.             <tx:method name="remove*" />  
  94.             <tx:method name="repair" />  
  95.             <tx:method name="deleteAndRepair" />  
  96.   
  97.             <tx:method name="get*" propagation="SUPPORTS" />  
  98.             <tx:method name="find*" propagation="SUPPORTS" />  
  99.             <tx:method name="load*" propagation="SUPPORTS" />  
  100.             <tx:method name="search*" propagation="SUPPORTS" />  
  101.             <tx:method name="datagrid*" propagation="SUPPORTS" />  
  102.   
  103.             <tx:method name="*" propagation="SUPPORTS" />  
  104.         tx:attributes>  
  105.     tx:advice>  
  106.     <aop:config>  
  107.         <aop:pointcut id="transactionPointcut" expression="execution(* com.jesus.sshframework.service..*Impl.*(..))" />  
  108.         <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />  
  109.     aop:config>  
  110.   
  111.   
  112. beans>  

注:我用的是阿里巴巴 温少写的Druid的,具体介绍
3、贴一下pom.xml

[html]  view plain copy
  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.0modelVersion>  
  4.       
  5.     <groupId>com.jesusgroupId>  
  6.     <artifactId>SSHframeworkartifactId>  
  7.     <packaging>warpackaging>  
  8.     <name>SSHframeworkname>  
  9.     <url>http://maven.apache.orgurl>  
  10.     <description>description>   
  11.     <inceptionYear>inceptionYear>  
  12.     <licenses>licenses>  
  13.   
  14.   
  15.       
  16.     <repositories>  
  17.           
  18.         <repository>  
  19.             <id>mavenid>  
  20.             <name>Maven Repository Switchboardname>  
  21.             <layout>defaultlayout>  
  22.             <url>http://repo1.maven.org/maven2url>  
  23.             <snapshots>  
  24.                 <enabled>falseenabled>  
  25.             snapshots>  
  26.         repository>  
  27.           
  28.         <repository>  
  29.             <id>alibaba-opensourceid>  
  30.             <name>alibaba-opensourcename>  
  31.             <url>http://code.alibabatech.com/mvn/releases/url>  
  32.             <layout>defaultlayout>  
  33.         repository>  
  34.           
  35.         <repository>  
  36.             <id>alibaba-opensource-snapshotid>  
  37.             <name>alibaba-opensource-snapshotname>  
  38.             <url>http://code.alibabatech.com/mvn/snapshots/url>  
  39.             <layout>defaultlayout>  
  40.         repository>  
  41.     repositories>  
  42.   
  43.     <properties>  
  44.         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>  
  45.     properties>  
  46.   
  47.       
  48.     <build>  
  49.         <finalName>SSHframeworkfinalName>  
  50.         <plugins>  
  51.             <plugin>  
  52.                 <artifactId>maven-war-pluginartifactId>  
  53.             plugin>  
  54.             <plugin>  
  55.                 <artifactId>maven-compiler-pluginartifactId>  
  56.                 <configuration>  
  57.                     <source>1.6source>  
  58.                     <target>1.6target>  
  59.                 configuration>  
  60.             plugin>  
  61.         plugins>  
  62.     build>  
  63.   
  64.     <dependencies>  
  65.           
  66.         <dependency>  
  67.             <groupId>junitgroupId>  
  68.             <artifactId>junitartifactId>  
  69.             <version>4.11version>  
  70.             <scope>testscope>  
  71.         dependency>  
  72.           
  73.           
  74.         <dependency>  
  75.             <groupId>javax.servletgroupId>  
  76.             <artifactId>servlet-apiartifactId>  
  77.             <version>3.0-alpha-1version>  
  78.             <scope>providedscope>  
  79.         dependency>  
  80.           
  81.           
  82.         <dependency>  
  83.             <groupId>log4jgroupId>  
  84.             <artifactId>log4jartifactId>  
  85.             <version>1.2.17version>  
  86.         dependency>  
  87.           
  88.           
  89.         <dependency>  
  90.             <groupId>javax.servletgroupId>  
  91.             <artifactId>jstlartifactId>  
  92.             <version>1.2version>  
  93.         dependency>  
  94.         <dependency>  
  95.             <groupId>javax.servlet.jspgroupId>  
  96.             <artifactId>jsp-apiartifactId>  
  97.             <version>2.2.1-b03version>  
  98.             <scope>providedscope>  
  99.         dependency>  
  100.           
  101.           
  102.         <dependency>  
  103.             <groupId>commons-fileuploadgroupId>  
  104.             <artifactId>commons-fileuploadartifactId>  
  105.             <version>1.2.2version>  
  106.         dependency>  
  107.           
  108.           
  109.         <dependency>  
  110.             <groupId>com.alibabagroupId>  
  111.             <artifactId>fastjsonartifactId>  
  112.             <version>1.1.26version>  
  113.         dependency>  
  114.           
  115.           
  116.         <dependency>  
  117.             <groupId>org.codehaus.jacksongroupId>  
  118.             <artifactId>jackson-mapper-aslartifactId>  
  119.             <version>1.9.11version>  
  120.         dependency>  
  121.           
  122.           
  123.         <dependency>  
  124.             <groupId>mysqlgroupId>  
  125.             <artifactId>mysql-connector-javaartifactId>  
  126.             <version>5.1.21version>  
  127.         dependency>  
  128.   
  129.           
  130.         <dependency>  
  131.             <groupId>com.alibabagroupId>  
  132.             <artifactId>druidartifactId>  
  133.             <version>0.2.9version>  
  134.         dependency>  
  135.           
  136.           
  137.         <dependency>  
  138.             <groupId>org.aspectjgroupId>  
  139.             <artifactId>aspectjweaverartifactId>  
  140.             <version>1.7.1version>  
  141.         dependency>  
  142.           
  143.           
  144.         <dependency>  
  145.             <groupId>org.hibernategroupId>  
  146.             <artifactId>hibernate-coreartifactId>  
  147.             <version>4.2.5.Finalversion>  
  148.         dependency>       
  149.         <dependency>  
  150.             <groupId>org.hibernategroupId>  
  151.             <artifactId>hibernate-entitymanagerartifactId>  
  152.             <version>4.2.5.Finalversion>  
  153.         dependency>  
  154.         <dependency>  
  155.             <groupId>org.hibernategroupId>  
  156.             <artifactId>hibernate-ehcacheartifactId>  
  157.             <version>4.2.5.Finalversion>  
  158.         dependency>  
  159.         <dependency>  
  160.             <groupId>org.hibernate.javax.persistencegroupId>  
  161.             <artifactId>hibernate-jpa-2.0-apiartifactId>  
  162.             <version>1.0.1.Finalversion>  
  163.         dependency>  
  164.           
  165.           
  166.         <dependency>  
  167.             <groupId>org.springframeworkgroupId>  
  168.             <artifactId>spring-webmvcartifactId>  
  169.             <version>3.2.0.RELEASEversion>  
  170.         dependency>  
  171.           
  172.           
  173.         <dependency>  
  174.             <groupId>org.springframeworkgroupId>  
  175.             <artifactId>spring-coreartifactId>  
  176.             <version>3.2.0.RELEASEversion>  
  177.         dependency>  
  178.         <dependency>  
  179.             <groupId>org.springframeworkgroupId>  
  180.             <artifactId>spring-contextartifactId>  
  181.             <version>3.2.0.RELEASEversion>  
  182.         dependency>  
  183.         <dependency>  
  184.             <groupId>org.springframeworkgroupId>  
  185.             <artifactId>spring-jdbcartifactId>  
  186.             <version>3.2.0.RELEASEversion>  
  187.         dependency>       
  188.         <dependency>  
  189.             <groupId>org.springframeworkgroupId>  
  190.             <artifactId>spring-beansartifactId>  
  191.             <version>3.2.0.RELEASEversion>  
  192.         dependency>  
  193.         <dependency>  
  194.             <groupId>org.springframeworkgroupId>  
  195.             <artifactId>spring-webartifactId>  
  196.             <version>3.2.0.RELEASEversion>  
  197.         dependency>  
  198.         <dependency>  
  199.             <groupId>org.springframeworkgroupId>  
  200.             <artifactId>spring-expressionartifactId>  
  201.             <version>3.2.0.RELEASEversion>  
  202.         dependency>  
  203.         <dependency>  
  204.             <groupId>org.springframeworkgroupId>  
  205.             <artifactId>spring-ormartifactId>  
  206.             <version>3.2.0.RELEASEversion>  
  207.         dependency>  
  208.         <dependency>  
  209.             <groupId>org.springframeworkgroupId>  
  210.             <artifactId>spring-testartifactId>  
  211.             <version>3.2.0.RELEASEversion>  
  212.         dependency>  
  213.           
  214.     dependencies>  
  215.       
  216. project>  
注:会使用的注意下版本更新就是了,有的可能

你可能感兴趣的:(用Maven整合SpringMVC+Spring+Hibernate 框架,实现简单的插入数据库数据功能)