spring,springMVC,mybatis,maven框架整合

来源:互联网 发布:电影片头制作软件 编辑:程序博客网 时间:2024/05/21 10:34
一,软件使用的是STS和Mysql,sts已经集成了maven插件
二,使用maven的generator工具,生成相关表mapper.java,mapper.xml,表.java
      1.generator的目录,如下图:
  
       最主要是generator.xml文件,src目录可以手工创建
       2.generator.xml的内容是(链接的是mysql数据库),根据自身的情况修改文件里面的内容
         
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
 <!-- 数据库驱动包位置 -->
 <!-- <classPathEntry location="D:\software\lib\mysql-connector-java-5.1.21.jar" /> -->
 <classPathEntry location="D:\Program Files\mysql-connector-java-5.1.34-bin.jar" />
 <context id="DB2Tables" targetRuntime="MyBatis3">
  <commentGenerator>
   <property name="suppressAllComments" value="true" />
  </commentGenerator>
  <!-- 数据库链接URL、用户名、密码 -->
  <!-- <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/sy" userId="sypro" password="sypro"> -->
  <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/sys" userId="root" password="root">
  </jdbcConnection>
  <javaTypeResolver>
   <property name="forceBigDecimals" value="false" />
  </javaTypeResolver>
  <!-- 生成模型的包名和位置 -->
  <javaModelGenerator targetPackage="sy.model" targetProject="D:\Program Files\generator\src">
   <property name="enableSubPackages" value="true" />
   <property name="trimStrings" value="true" />
  </javaModelGenerator>
  <!-- 生成的映射文件包名和位置 -->
  <sqlMapGenerator targetPackage="sy.mapping" targetProject="D:\Program Files\generator\src">
   <property name="enableSubPackages" value="true" />
  </sqlMapGenerator>
  <!-- 生成DAO的包名和位置 -->
  <javaClientGenerator type="XMLMAPPER" targetPackage="sy.dao" targetProject="D:\Program Files\generator\src">
   <property name="enableSubPackages" value="true" />
  </javaClientGenerator>
  <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
  <table tableName="sys_config" domainObjectName="Sysconfig" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
 </context>
</generatorConfiguration>

  3.在generator.xml当前目录,按住shift+右键,调出doc命令窗口
   4.在命令窗口中输入:java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
   5.就会再src目录下生成,关于表的一些文件,如图
三,在sts中搭建 SSM框架
      1.打开sts工具,新建maven项目,如图

   项目新建成功以后,应该出现下面包
 
   2.将通过generator工具生成的文件,放到src/main/java目录下,如图(dao,mapping,model是generator工具生成的,其他是手工建立的)
  
    3.引入框架需要的包,打开pom.xml文件,http://search.maven.org/(包下载网址),
       如果需要springMVC的包,在网址中输入spring-webmvc
回车,出现上面结果
点击一个进去
将标红的文本内容,放到pom.xml中
就会在下图位置,生成需要的包,其他的类似
pom.xml内容如下,仅供参考

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>mybatiDemo</groupId>
 <artifactId>mybatiDemo</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>mybatiDemo Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <dependencies>
  <!-- 使用spring测试 --> 
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>3.2.0.RELEASE</version>
  </dependency>
  <!-- springmvc包 -->  
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>3.2.0.RELEASE</version>
  </dependency>
  <!-- spring包,spring-core是spring的核心包 -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>3.2.0.RELEASE</version>
  </dependency>
  <!-- mybatis包,mybatis-core是mybatis的核心包 -->
  <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.1.1</version>
  </dependency>  
  <!-- mybatis和spring整合包 -->
  <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.1.1</version>
  </dependency>
  <!-- mybatis是操作数据库的,操作数据库的驱动包 -->
  <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.21</version>
  </dependency> 
  <!-- junit是测试包,测试spring和mybatis整合,项目发布时需要删除 -->
  <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
  </dependency>
  <!-- 数据源 -->
  <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>0.2.9</version>
  </dependency> 
  <!-- 测试mybatis和spring时,报错需要的包 -->
  <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.7.1</version>
  </dependency>

  <!-- 文件上传使用的包 --> 
  <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.2.2</version>
  </dependency>
  <!-- servlet -->
  <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>3.0-alpha-1</version>
  </dependency>
  <!-- 打印日志 --> 
  <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
  </dependency>
 </dependencies>
 <build>
 </build>
</project>
 4.引入spring,springMVC,mybatis需要的xml文件
将spring.xml,spring-mvc.xml,spring-mybatis.xml,config.properties文件放到resource目录下,如图
spring.xml是spring框架需要的文件,内容如下

<?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" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
 <!-- 引入属性文件,classpath路径是指src/main/resource -->
 <context:property-placeholder location="classpath:config.properties"/>
    <!-- 自动扫描(自动注解)sy.service是一个包名,下面的作用是不用写很多个bean -->
    <context:component-scan base-package="sy.service"></context:component-scan>
 </beans>

spring-mvc.xml是springMVC框架需要的文件,内容如下

<?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:p="http://www.springframework.org/schema/p"    
  xmlns:context="http://www.springframework.org/schema/context"    
  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">  

  <!--自动扫描Controller包下的所有类,使其认为spring mvc的控制器-->
 
  <context:component-scan base-package = "sy.controller"></context:component-scan>
  <!-- 文件上传 -->
  <!--   配置视图解析器将视图逻辑名解析为/*.jsp-->
  <!-- /表示文件放在了root下 -->
  <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />
   
   <bean id="multipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" >
     
    <property name = "defaultEncoding" >
     <value>UFT-8</value>
    </property>
    <!-- 上传文件大小为31M 31*1024*1024 -->
    <property name = "maxUploadSize" >
     <value>32505856</value>
    </property>
    <property name = "maxInMemorySize" >
     <value>4096</value>
    </property>
   </bean>
</beans>

spring-mybatis.xml是spring和mybatis结合需要的文件,内容如下

<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
    <!-- 配置数据源 -->
    <!-- 如果tomcate配置了数据源,可以直接使用JNDI的方式 -->
    <!-- value使用变量,和config.properties中变量有关 -->
    <bean name = "datasource" class = "com.alibaba.druid.pool.DruidDataSource" init-method = "init" destroy-method = "close">
     <property name ="url" value = "${jdbc_url}"></property>
     <property name="username" value="${jdbc_username}" />
   <property name="password" value="${jdbc_password}" />
    </bean>       
     <!--
    <bean name = "datasource" class = "com.alibaba.druid.pool.DruidDataSource" init-method = "init" destroy-method = "close">
     <property name ="url" value = "jdbc:mysql://localhost:8000/bookShopping"></property>
     <property name="username" value="root" />
   <property name="password" value="13072399672" />
    </bean>
    -->
    <!-- mybatis文件,很重要 -->
    <!--配置sqlSessionFactory 并读取mybatis的一些配置-->
    <bean name = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean">
    <property name = "dataSource" ref = "datasource"></property>
    <!-- 自动扫描entity目录,省掉Configuration.xml的手工配置 -->
    <property name="mapperLocations" value="classpath:sy/mapping/*.xml"/>
    </bean>
   
    <!-- 自动扫描 将Mapper接口生成代理注入到Spring-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="sy.dao" />
  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
 </bean>
   
    <!--配置事物-->
    <bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name = "dataSource" ref = "datasource"></property>
    </bean>
   
    <!--
 <tx:annotation-driven transaction-manager = "transactionManager"/>
    -->
 
    <!-- 事物的具体内容 -->
 <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="add*" propagation="REQUIRED" />
   <tx:method name="append*" propagation="REQUIRED" />
   <tx:method name="insert*" propagation="REQUIRED" />
   <tx:method name="save*" propagation="REQUIRED" />
   <tx:method name="update*" propagation="REQUIRED" />
   <tx:method name="modify*" propagation="REQUIRED" />
   <tx:method name="edit*" propagation="REQUIRED" />
   <tx:method name="delete*" propagation="REQUIRED" />
   <tx:method name="remove*" propagation="REQUIRED" />
   <tx:method name="repair" propagation="REQUIRED" />
   <tx:method name="delAndRepair" propagation="REQUIRED" />
   
   
   <tx:method name="get*" propagation="SUPPORTS" />
   <tx:method name="find*" propagation="SUPPORTS" />
   <tx:method name="load*" propagation="SUPPORTS" />
   <tx:method name="search*" propagation="SUPPORTS" />
   <tx:method name="datagrid*" propagation="SUPPORTS" />
   
   
   <tx:method name="*" propagation="SUPPORTS" />
  </tx:attributes>
 </tx:advice>

 <!--定义一个切面,在定义的切面上加入事物 -->
    <!-- 
    <aop:config>
  <aop:pointcut id="transactionPointcut" expression="execution(* com.longhang.service..*Impl.*(..))" />
  <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
 </aop:config>
 -->
</beans>


config.properties的内容如下

jdbc_url=jdbc:mysql://localhost:3306/sys?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10&autoReconnectForPools=true&zeroDateTimeBehavior=convertToNull&connectTimeout=3000
jdbc_username=root
jdbc_password=root
validationQuery=select 1

这样SSM框架就搭建完成了!

0 0