Maven+Struts2+MyBatis+Spring搭建教程

教程目标

在MyEclipse中搭建Struts2+MyBatis+Spring环境,用Maven管理项目,利用mybatis-gernerator插件生成部分代码

附带目标:

struts2的自定义拦截器和国际化,mybatis-spring自动装配Dao,mybatis-gernerator的配置

 

先贴下搭建成功后项目的结构图,方便理解:

 


1、新建web project,add maven spport:


2、编辑已经生成的pom.xml:pom文件不是一口气写完的,而是随着添加struts、mybatis和spring等特性以后才逐渐完成的,我在这里就把完成以后的贴在这里了

 

 

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/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>MybatisSpringStrut2</groupId>  
  5.     <artifactId>MybatisSpringStrut2</artifactId>  
  6.     <version>0.0.1-SNAPSHOT</version>  
  7.     <packaging>war</packaging>  
  8.     <name>Mybatis+Spring+Strut2集成示例</name>  
  9.     <developers>  
  10.         <developer>  
  11.             <name>Sam</name>  
  12.             <email>sanlai_lee@foxmail.com</email>  
  13.         </developer>  
  14.     </developers>  
  15.     <properties>  
  16.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  17.         <!-- 下面这些定义的属性在mybatis-generator.xml文件中引用  -->  
  18.         <classPathEntry.location>${basedir}/WebRoot/WEB-INF/lib/mysql-connector-java-5.1.18.jar</classPathEntry.location>  
  19.         <jdbc.driverClass>com.mysql.jdbc.Driver</jdbc.driverClass>  
  20.         <jdbc.connectionURL>jdbc:mysql://localhost:3306/test?generateSimpleParameterMetadata=true</jdbc.connectionURL>  
  21.         <jdbc.userId>root</jdbc.userId>  
  22.         <jdbc.password>1120</jdbc.password>  
  23.         <model.target.dir>${basedir}/src/java</model.target.dir>  
  24.         <mappers.target.dir>${basedir}/src/config</mappers.target.dir>  
  25.         <client.target.dir>${basedir}/src/java</client.target.dir>  
  26.     </properties>  
  27.         <!-- 依赖包  -->  
  28.     <dependencies>  
  29.         <dependency>  
  30.             <groupId>org.glassfish</groupId>  
  31.             <artifactId>bean-validator</artifactId>  
  32.             <version>3.0-JBoss-4.0.2</version>  
  33.         </dependency>  
  34.         <dependency>  
  35.             <groupId>org.glassfish</groupId>  
  36.             <artifactId>javax.annotation</artifactId>  
  37.             <version>3.0.1</version>  
  38.         </dependency>  
  39.         <dependency>  
  40.             <groupId>org.glassfish</groupId>  
  41.             <artifactId>javax.ejb</artifactId>  
  42.             <version>3.0.1</version>  
  43.         </dependency>  
  44.         <dependency>  
  45.             <groupId>org.glassfish</groupId>  
  46.             <artifactId>javax.enterprise.deploy</artifactId>  
  47.             <version>3.0.1</version>  
  48.         </dependency>  
  49.         <dependency>  
  50.             <groupId>org.glassfish</groupId>  
  51.             <artifactId>javax.jms</artifactId>  
  52.             <version>3.0.1</version>  
  53.         </dependency>  
  54.         <dependency>  
  55.             <groupId>org.glassfish</groupId>  
  56.             <artifactId>javax.management.j2ee</artifactId>  
  57.             <version>3.0.1</version>  
  58.         </dependency>  
  59.         <dependency>  
  60.             <groupId>org.eclipse.persistence</groupId>  
  61.             <artifactId>javax.persistence</artifactId>  
  62.             <version>2.0.0</version>  
  63.         </dependency>  
  64.         <dependency>  
  65.             <groupId>org.glassfish</groupId>  
  66.             <artifactId>javax.resource</artifactId>  
  67.             <version>3.0.1</version>  
  68.         </dependency>  
  69.         <dependency>  
  70.             <groupId>org.glassfish</groupId>  
  71.             <artifactId>javax.security.auth.message</artifactId>  
  72.             <version>3.0.1</version>  
  73.         </dependency>  
  74.         <dependency>  
  75.             <groupId>org.glassfish</groupId>  
  76.             <artifactId>javax.security.jacc</artifactId>  
  77.             <version>3.0.1</version>  
  78.         </dependency>  
  79.         <dependency>  
  80.             <groupId>org.glassfish</groupId>  
  81.             <artifactId>javax.servlet</artifactId>  
  82.             <version>3.0.1</version>  
  83.             <scope>provided</scope>  
  84.         </dependency>  
  85.         <dependency>  
  86.             <groupId>org.glassfish</groupId>  
  87.             <artifactId>javax.servlet.jsp</artifactId>  
  88.             <version>3.0.1</version>  
  89.             <scope>provided</scope>  
  90.         </dependency>  
  91.         <dependency>  
  92.             <groupId>org.glassfish</groupId>  
  93.             <artifactId>javax.servlet.jsp.jstl</artifactId>  
  94.             <version>3.0.1</version>  
  95.         </dependency>  
  96.         <dependency>  
  97.             <groupId>org.glassfish</groupId>  
  98.             <artifactId>javax.transaction</artifactId>  
  99.             <version>3.0.1</version>  
  100.         </dependency>  
  101.         <dependency>  
  102.             <groupId>javax.xml.bind</groupId>  
  103.             <artifactId>jaxb-api-osgi</artifactId>  
  104.             <version>2.2.1</version>  
  105.         </dependency>  
  106.         <dependency>  
  107.             <groupId>javax.ws.rs</groupId>  
  108.             <artifactId>jsr311-api</artifactId>  
  109.             <version>1.1.1</version>  
  110.         </dependency>  
  111.         <dependency>  
  112.             <groupId>javax.mail</groupId>  
  113.             <artifactId>mail</artifactId>  
  114.             <version>1.4.3</version>  
  115.         </dependency>  
  116.         <dependency>  
  117.             <groupId>javax.xml</groupId>  
  118.             <artifactId>webservices-api-osgi</artifactId>  
  119.             <version>2.0.1</version>  
  120.         </dependency>  
  121.         <dependency>  
  122.             <groupId>org.jboss.weld</groupId>  
  123.             <artifactId>weld-osgi-bundle</artifactId>  
  124.             <version>1.0.1-SP3</version>  
  125.         </dependency>  
  126.         <dependency>  
  127.             <groupId>log4j</groupId>  
  128.             <artifactId>log4j</artifactId>  
  129.             <version>1.2.15</version>  
  130.         </dependency>  
  131.         <dependency>  
  132.             <groupId>mysql</groupId>  
  133.             <artifactId>mysql-connector-java</artifactId>  
  134.             <version>5.1.18</version>  
  135.         </dependency>  
  136.         <dependency>  
  137.             <groupId>org.mybatis</groupId>  
  138.             <artifactId>mybatis</artifactId>  
  139.             <version>3.0.6</version>  
  140.         </dependency>  
  141.         <dependency>  
  142.             <groupId>org.mybatis</groupId>  
  143.             <artifactId>mybatis-spring</artifactId>  
  144.             <version>1.0.2</version>  
  145.         </dependency>  
  146.         <dependency>  
  147.             <groupId>xerces</groupId>  
  148.             <artifactId>xmlParserAPIs</artifactId>  
  149.             <version>2.6.2</version>  
  150.         </dependency>  
  151.         <dependency>  
  152.             <groupId>dom4j</groupId>  
  153.             <artifactId>dom4j</artifactId>  
  154.             <version>1.6.1</version>  
  155.         </dependency>  
  156.         <dependency>  
  157.             <groupId>com.sun.mail</groupId>  
  158.             <artifactId>pop3</artifactId>  
  159.             <version>1.4.4</version>  
  160.         </dependency>  
  161.         <dependency>  
  162.             <groupId>com.sun.mail</groupId>  
  163.             <artifactId>smtp</artifactId>  
  164.             <version>1.4.4</version>  
  165.         </dependency>  
  166.         <dependency>  
  167.             <groupId>cglib</groupId>  
  168.             <artifactId>cglib</artifactId>  
  169.             <version>2.2.2</version>  
  170.             <type>jar</type>  
  171.         </dependency>  
  172.         <dependency>  
  173.             <groupId>org.apache.struts</groupId>  
  174.             <artifactId>struts2-core</artifactId>  
  175.             <version>2.2.3.1</version>  
  176.         </dependency>  
  177.         <dependency>  
  178.             <groupId>org.apache.struts</groupId>  
  179.             <artifactId>struts2-json-plugin</artifactId>  
  180.             <version>2.2.3.1</version>  
  181.         </dependency>  
  182.         <dependency>  
  183.             <groupId>org.apache.struts</groupId>  
  184.             <artifactId>struts2-config-browser-plugin</artifactId>  
  185.             <version>2.2.3.1</version>  
  186.         </dependency>  
  187.         <dependency>  
  188.             <groupId>javax.servlet</groupId>  
  189.             <artifactId>servlet-api</artifactId>  
  190.             <version>2.5</version>  
  191.             <scope>provided</scope>  
  192.         </dependency>  
  193.         <dependency>  
  194.             <groupId>javax.servlet.jsp</groupId>  
  195.             <artifactId>jsp-api</artifactId>  
  196.             <version>2.1</version>  
  197.             <scope>provided</scope>  
  198.         </dependency>  
  199.         <dependency>  
  200.             <groupId>org.apache.struts</groupId>  
  201.             <artifactId>struts2-spring-plugin</artifactId>  
  202.             <version>2.2.3.1</version>  
  203.         </dependency>  
  204.         <dependency>  
  205.             <groupId>commons-logging</groupId>  
  206.             <artifactId>commons-logging</artifactId>  
  207.             <version>1.1.1</version>  
  208.         </dependency>  
  209.         <dependency>  
  210.             <groupId>commons-dbcp</groupId>  
  211.             <artifactId>commons-dbcp</artifactId>  
  212.             <version>1.4</version>  
  213.         </dependency>  
  214.         <dependency>  
  215.             <groupId>c3p0</groupId>  
  216.             <artifactId>c3p0</artifactId>  
  217.             <version>0.9.1.2</version>  
  218.         </dependency>  
  219.         <dependency>  
  220.             <groupId>org.springframework</groupId>  
  221.             <artifactId>spring-core</artifactId>  
  222.             <version>3.0.6.RELEASE</version>  
  223.         </dependency>  
  224.         <dependency>  
  225.             <groupId>org.springframework</groupId>  
  226.             <artifactId>spring-web</artifactId>  
  227.             <version>3.0.6.RELEASE</version>  
  228.         </dependency>  
  229.         <dependency>  
  230.             <groupId>org.springframework</groupId>  
  231.             <artifactId>spring-jdbc</artifactId>  
  232.             <version>3.0.6.RELEASE</version>  
  233.         </dependency>  
  234.         <dependency>  
  235.             <groupId>org.springframework</groupId>  
  236.             <artifactId>spring-beans</artifactId>  
  237.             <version>3.0.6.RELEASE</version>  
  238.         </dependency>  
  239.         <dependency>  
  240.             <groupId>org.springframework</groupId>  
  241.             <artifactId>spring-context</artifactId>  
  242.             <version>3.0.6.RELEASE</version>  
  243.         </dependency>  
  244.         <dependency>  
  245.             <groupId>org.springframework</groupId>  
  246.             <artifactId>spring-tx</artifactId>  
  247.             <version>3.0.6.RELEASE</version>  
  248.         </dependency>  
  249.         <dependency>  
  250.             <groupId>org.springframework</groupId>  
  251.             <artifactId>spring-asm</artifactId>  
  252.             <version>3.0.6.RELEASE</version>  
  253.         </dependency>  
  254.         <dependency>  
  255.             <groupId>org.springframework</groupId>  
  256.             <artifactId>spring-aop</artifactId>  
  257.             <version>3.0.6.RELEASE</version>  
  258.         </dependency>  
  259.         <dependency>  
  260.             <groupId>org.springframework</groupId>  
  261.             <artifactId>spring-expression</artifactId>  
  262.             <version>3.0.6.RELEASE</version>  
  263.         </dependency>  
  264.         <dependency>  
  265.             <groupId>commons-pool</groupId>  
  266.             <artifactId>commons-pool</artifactId>  
  267.             <version>1.5.6</version>  
  268.         </dependency>  
  269.     </dependencies>  
  270.     <build>  
  271.         <!-- 源文件夹位置定义 -->  
  272.         <sourceDirectory>${basedir}/src/java,${basedir}/src/config</sourceDirectory>  
  273.         <!-- 输出文件夹位置定义 -->  
  274.         <outputDirectory>${basedir}/WebRoot/WEB-INF/classes</outputDirectory>  
  275.         <!-- 资源文件位置定义 -->  
  276.         <resources>  
  277.             <resource>  
  278.                 <targetPath>${project.build.outputDirectory}</targetPath>  
  279.                 <directory>${project.build.sourceDirectory}</directory>  
  280.                 <excludes>  
  281.                     <exclude>**/*.java</exclude>  
  282.                 </excludes>  
  283.             </resource>  
  284.             <resource>  
  285.                 <directory>${project.build.sourceDirectory}</directory>  
  286.                 <includes>  
  287.                     <include>**/*.xml,**/*.properties</include>  
  288.                 </includes>  
  289.             </resource>  
  290.         </resources>  
  291.         <!-- war包的最终名字格式定义 -->  
  292.         <finalName>${project.artifactId}-${project.version}</finalName>  
  293.         <!-- 插件定义 -->  
  294.         <plugins>  
  295.             <!-- 打包插件 -->  
  296.             <plugin>  
  297.                 <groupId>org.apache.maven.plugins</groupId>  
  298.                 <artifactId>maven-war-plugin</artifactId>  
  299.                 <version>2.1.1</version>  
  300.                 <configuration>  
  301.                     <webappDirectory>${basedir}/WebRoot</webappDirectory>  
  302.                     <warSourceDirectory>${basedir}/WebRoot</warSourceDirectory>  
  303.                     <outputDirectory>${basedir}/target/output</outputDirectory>  
  304.                 </configuration>  
  305.             </plugin>  
  306.             <!-- 编译插件 -->  
  307.             <plugin>  
  308.                 <groupId>org.apache.maven.plugins</groupId>  
  309.                 <artifactId>maven-compiler-plugin</artifactId>  
  310.                 <version>2.3.2</version>  
  311.                 <configuration>  
  312.                     <source>1.6</source>  
  313.                     <target>1.6</target>  
  314.                 </configuration>  
  315.             </plugin>  
  316.             <!-- mybatis的代码生成插件 -->  
  317.             <plugin>  
  318.                 <groupId>org.mybatis.generator</groupId>  
  319.                 <artifactId>mybatis-generator-maven-plugin</artifactId>  
  320.                 <version>1.3.1</version>  
  321.                 <configuration>  
  322.                     <configurationFile>${basedir}/src/config/mybatis-generator.xml</configurationFile>  
  323.                     <verbose>true</verbose>  
  324.                     <overwrite>true</overwrite>  
  325.                 </configuration>  
  326.                 <executions>  
  327.                     <execution>  
  328.                         <id>Generate MyBatis Artifacts</id>  
  329.                         <goals>  
  330.                             <goal>generate</goal>  
  331.                         </goals>  
  332.                     </execution>  
  333.                 </executions>  
  334.             </plugin>  
  335.         </plugins>  
  336.     </build>  
  337. </project>  
 

 

 

3、编辑web.xml:

 

 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="3.0"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
  7.   <display-name>MybatisSpringStrut2</display-name>    
  8.   <!-- struts2过滤器配置 -->  
  9.     <filter>  
  10.         <filter-name>struts2</filter-name>  
  11.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  12.     </filter>  
  13.   
  14.     <filter-mapping>  
  15.         <filter-name>struts2</filter-name>  
  16.         <url-pattern>/*</url-pattern>  
  17.     </filter-mapping>  
  18.   
  19.     <!-- 集成spring -->  
  20.     <listener>  
  21.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  22.     </listener>  
  23.       
  24.     <!-- spring的配置文件位置 -->  
  25.     <context-param>  
  26.         <param-name>contextConfigLocation</param-name>  
  27.         <param-value>classpath*:spring/applicationContext*.xml</param-value>  
  28.     </context-param>  
  29.       
  30.     <!-- 欢迎页面配置 -->  
  31.     <welcome-file-list>  
  32.         <welcome-file>site/index.jsp</welcome-file>  
  33.     </welcome-file-list>  
  34.   
  35. </web-app>  
 

 

4、struts2的配置文件:

 

strutsxml:

 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="3.0"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
  7.   <display-name>MybatisSpringStrut2</display-name>    
  8.   <!-- struts2过滤器配置 -->  
  9.     <filter>  
  10.         <filter-name>struts2</filter-name>  
  11.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  12.     </filter>  
  13.   
  14.     <filter-mapping>  
  15.         <filter-name>struts2</filter-name>  
  16.         <url-pattern>/*</url-pattern>  
  17.     </filter-mapping>  
  18.   
  19.     <!-- 集成spring -->  
  20.     <listener>  
  21.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  22.     </listener>  
  23.       
  24.     <!-- spring的配置文件位置 -->  
  25.     <context-param>  
  26.         <param-name>contextConfigLocation</param-name>  
  27.         <param-value>classpath*:spring/applicationContext*.xml</param-value>  
  28.     </context-param>  
  29.       
  30.     <!-- 欢迎页面配置 -->  
  31.     <welcome-file-list>  
  32.         <welcome-file>site/index.jsp</welcome-file>  
  33.     </welcome-file-list>  
  34.   
  35. </web-app>  

 

 

struts/struts-admin.xml:

 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.   
  8.     <!--   
  9.         result type:  
  10.         <result-types>  
  11.             <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>  
  12.             <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>  
  13.             <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>  
  14.             <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>  
  15.             <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>  
  16.             <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>  
  17.             <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>  
  18.             <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>  
  19.             <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>  
  20.             <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />  
  21.         </result-types>  
  22.      -->  
  23.   
  24.     <package name="admin" namespace="/admin" extends="my-truts2">  
  25.           
  26.         <action name="login">  
  27.             <result>/admin/login.jsp</result>  
  28.         </action>  
  29.   
  30.         <action name="main">  
  31.             <result>/admin/main.jsp</result>  
  32.         </action>  
  33.           
  34.         <!-- 测试拦截器 -->  
  35.         <action name="testInterceptor" class="adminAction" method="testInterceptor">  
  36.             <interceptor-ref name="adminLoginInterceptorStack"/>  
  37.             <result>/admin/main.jsp</result>  
  38.         </action>  
  39.           
  40.         <!-- 测试数据访问功能CRUD -->  
  41.         <action name="testCrud" class="adminAction" method="testCrud">  
  42.             <result>/admin/main.jsp</result>  
  43.         </action>  
  44.   
  45.     </package>  
  46. </struts>  

 

 

struts/struts-index.xml:

 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.   
  8.     <!--   
  9.         result type:  
  10.         <result-types>  
  11.             <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>  
  12.             <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>  
  13.             <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>  
  14.             <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>  
  15.             <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>  
  16.             <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>  
  17.             <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>  
  18.             <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>  
  19.             <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>  
  20.             <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />  
  21.         </result-types>  
  22.      -->  
  23.   
  24.     <package name="index" namespace="/" extends="my-truts2">  
  25.   
  26.         <action name="index">  
  27.             <result>/site/index.jsp</result>  
  28.         </action>  
  29.           
  30.         <action name="language">  
  31.             <result type="redirectAction">index</result>  
  32.         </action>  
  33.   
  34.     </package>  
  35. </struts>  
 

 

 

5、spring的配置文件,位于src/config/spring:

 

applicationContext.xml:

 

 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xsi:schemaLocation="  
  5.         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.0.xsd">  
  9.   
  10.     <!-- 数据源连接信息配置文件 -->  
  11.     <context:property-placeholder location="classpath*:jdbc.properties" />  
  12.       
  13.     <!-- 开发环境数据源 -->  
  14.     <bean id="dataSource_dev" class="org.apache.commons.dbcp.BasicDataSource"  
  15.         destroy-method="close">  
  16.         <property name="driverClassName" value="${jdbc.driverClassName.mysql_dev}" />  
  17.         <property name="url" value="${jdbc.url.mysql_dev}" />  
  18.         <property name="username" value="${jdbc.username.mysql_dev}" />  
  19.         <property name="password" value="${jdbc.password.mysql_dev}" />  
  20.     </bean>  
  21.   
  22.     <!-- 生产环境数据源 -->  
  23.     <bean id="dataSource_product" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
  24.         destroy-method="close">  
  25.         <property name="driverClass" value="${jdbc.driverClassName.mysql}" />  
  26.         <property name="jdbcUrl" value="${jdbc.url.mysql}" />  
  27.         <property name="user" value="${jdbc.username.mysql}" />  
  28.         <property name="password" value="${jdbc.password.mysql}" />  
  29.         <property name="maxPoolSize" value="${c3p0.maxPoolSize}" />  
  30.         <property name="acquireIncrement" value="${c3p0.acquireIncrement}" />  
  31.         <property name="minPoolSize" value="${c3p0.minPoolSize}" />  
  32.         <property name="maxIdleTime" value="${c3p0.maxIdleTime}" />  
  33.     </bean>  
  34.   
  35.     <!-- 配置sqlSessionFactory -->  
  36.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  37.         <property name="dataSource" ref="dataSource_dev" />  
  38.         <!-- 自动到mappers包下面去搜索mybatis的映射文件 -->  
  39.         <property name="mapperLocations" value="classpath*:mappers/**/*.xml" />  
  40.     </bean>  
  41.   
  42.     <!-- 使用Spring的事务管理器 -->  
  43.     <bean id="transactionManager"  
  44.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  45.         <property name="dataSource" ref="dataSource_dev" />  
  46.     </bean>  
  47.   
  48. </beans>  
 

applicationContextDao.xml:

 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xsi:schemaLocation="  
  5.         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.0.xsd">  
  9.   
  10.     <!-- 单个的注册 MapperFactoryBean -->  
  11.     <!--   
  12.      <bean id="managerVOMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
  13.         <property name="mapperInterface" value="com.xylcolor.dao.ManagerVOMapper" />  
  14.         <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
  15.     </bean>  
  16.     -->  
  17.   
  18.     <!-- 利用MyBatis-Spring的自动装配机制:  
  19.         自动从com.xylcolor.dao包中寻找接口装配成MapperFactoryBean   
  20.         有了下面这个配置,那么就不需要像上面那样一个一个的去注册映射器Bean了,大大的减少了工作量  
  21.     -->  
  22.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  23.         <property name="basePackage" value="com.xylcolor.dao" />  
  24.     </bean>  
  25.   
  26. </beans>  
 

applicationContextService.xml:

 

 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="  
  6.         http://www.springframework.org/schema/beans  
  7.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.         http://www.springframework.org/schema/context  
  9.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  10.           
  11.     <bean id="newsService" class="com.xylcolor.service.impl.NewsServiceImpl">  
  12.         <!--   
  13.             这里的ref=newsVOMapper,就是mybatis-spring自动装配的,我们在applicationContextDao.xml  
  14.             文件中并没有显示的注册newsVOMapper这个Bean;  
  15.             当然这个名字为什么就是newsVOMapper呢,默认的会根据Dao接口名称按照驼峰命名习惯来生成这个名称,  
  16.             如果你想拥有自己的名称风格,那么你可以在Dao接口上通过注解@Component或@Named来更改名称,  
  17.             例如:  
  18.             @Component(value="newsVODao")  
  19.             public interface NewsVOMapper {……}  
  20.             或  
  21.             @Named(value="newsVODao")  
  22.             public interface NewsVOMapper {……}  
  23.               
  24.             这样以后,这里ref就应该是ref="newsVODao"   
  25.         -->  
  26.         <property name="newsVOMapper" ref="newsVOMapper" />  
  27.     </bean>  
  28.   
  29. </beans>  

 

 

applicationContextActions.xml:

 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="  
  6.         http://www.springframework.org/schema/beans  
  7.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.         http://www.springframework.org/schema/context  
  9.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  10.   
  11.     <bean id="adminAction" class="com.xylcolor.actions.AdminAction">  
  12.         <property name="newsService" ref="newsService"/>  
  13.     </bean>  
  14. </beans>  

 

 

6、mybatis的配置文件:mybatis-config.xml,这个文件在我们现在这个环境中其实可以不用,除非你一定要定义一些

typeAliases或者settings;项目中实际要用到的映射文件会由插件生成,由mybatis-spring根据你给的路径自动寻找装配:

 

 

mybatis-config.xml:

 

 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
  3. "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  4.   
  5. <configuration>  
  6.     <settings>  
  7.         <setting name="cacheEnabled" value="true"/>  
  8.         <setting name="lazyLoadingEnabled" value="true"/>  
  9.         <setting name="aggressiveLazyLoading" value="true"/>  
  10.     </settings>  
  11.       
  12.     <!-- 别名配置 -->  
  13.     <typeAliases>  
  14.         <typeAlias alias="NewsVO" type="com.xylcolor.vo.model.NewsVO"/>  
  15.     </typeAliases>  
  16.       
  17. </configuration>  

 

 

7、代码生成插件的配置文件:mybatis-generator.xml

 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE generatorConfiguration  
  3.       PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  
  4.       "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">  
  5.   
  6. <!-- mybatis-generator插件生成代码的配置文件 -->  
  7. <generatorConfiguration>  
  8.     <classPathEntry location="${classPathEntry.location}" />  
  9.       
  10.     <context id="MySQLTables" targetRuntime="MyBatis3">  
  11.       
  12.         <jdbcConnection driverClass="${jdbc.driverClass}"  
  13.             connectionURL="${jdbc.connectionURL}"  
  14.             userId="${jdbc.userId}"  
  15.             password="${jdbc.password}">  
  16.         </jdbcConnection>  
  17.   
  18.         <!-- java类型处理器,默认的就这么一个处理器可以用,可以自己写,具体的请参考官方文档http://code.google.com/p/mybatis/wiki/Generator -->  
  19.         <javaTypeResolver>  
  20.             <property name="forceBigDecimals" value="false" />  
  21.         </javaTypeResolver>  
  22.   
  23.         <!-- 生成 Model,POJO -->  
  24.         <javaModelGenerator targetPackage="com.xylcolor.vo.model" targetProject="${model.target.dir}">  
  25.             <property name="enableSubPackages" value="true" />  
  26.             <property name="trimStrings" value="true" />  
  27.         </javaModelGenerator>  
  28.   
  29.         <!-- 生成映射文件*.xml -->  
  30.         <sqlMapGenerator targetPackage="mappers" targetProject="${mappers.target.dir}">  
  31.             <property name="enableSubPackages" value="true" />  
  32.         </sqlMapGenerator>  
  33.           
  34.         <!-- 生成映射器类 -->  
  35.         <javaClientGenerator type="XMLMAPPER" targetPackage="com.xylcolor.dao" targetProject="${client.target.dir}">  
  36.             <property name="enableSubPackages" value="true" />  
  37.         </javaClientGenerator>  
  38.   
  39.         <!-- 指定要生成的表名和对应形成的POJO类名  -->  
  40.         <table schema=""   
  41.             tableName="tb_news"   
  42.             domainObjectName="NewsVO"/>  
  43.               
  44.     </context>  
  45. </generatorConfiguration>  

 

完成上面那些配置文件以后,基本上配置上就完成了,下面的工作就是java代码的完成了,具体的示例代码见附件,必要的地方我都做了注释。

 

看看实际运行情况:

1.国际化:



 

2:利用插件生成的代码,完成数据的增删改查:

 

Java代码   收藏代码
  1. /** 
  2.      * 测试数据的增删改查 
  3.      * @return 
  4.      * @throws JSONException 
  5.      */  
  6.     public String testCrud() throws JSONException{  
  7.         String b = "<br />";  
  8.         String response = "测试数据的增删改查:<br />";  
  9.           
  10.         //查  
  11.         NewsVOExample example = new NewsVOExample();  
  12.         example.or().andIdIsNotNull();  
  13.         List<NewsVO> list = newsService.selectByExample(example);  
  14.         response += "原始数据:<br />";  
  15.         for (NewsVO newsVO : list) {  
  16.             response += JSONUtil.serialize(newsVO)+b;  
  17.         }  
  18.         //增  
  19.         NewsVO record = new NewsVO();  
  20.         record.setTitle("这是我新增的一个新闻");  
  21.         record.setContent("2012其实真的快到了");  
  22.         record.setSource("火星资讯");  
  23.         record.setDate(new Date());  
  24.         int result = newsService.insert(record);  
  25.         response += result==1?"插入新数据成功":"插入数据失败"+b;  
  26.         list = newsService.selectByExample(example);  
  27.         response += "插入数据后:"+b;  
  28.         for (NewsVO newsVO : list) {  
  29.             response += JSONUtil.serialize(newsVO)+b;  
  30.         }  
  31.         //改  
  32.         record = newsService.selectByExample(example).get(0);  
  33.         response += "修改前的数据:"+b+JSONUtil.serialize(record);  
  34.         example = new NewsVOExample();  
  35.         example.or().andTitleEqualTo(record.getTitle());  
  36.         int originId = record.getId();  
  37.         record.setTitle("这是我修改后的标题");  
  38.         result = newsService.updateByExample(record, example);  
  39.         response += result==1?"修改成功":"修改失败"+b;  
  40.         example.or().andTitleEqualTo(record.getTitle());  
  41.         record = newsService.selectByPrimaryKey(originId);  
  42.         response +="修改后的数据:"+b+JSONUtil.serialize(record)+b;  
  43.           
  44.         //删  
  45.         example.or().andTitleEqualTo("这是我新增的一个新闻");  
  46.         newsService.deleteByExample(example);  
  47.         example.or().andIdIsNotNull();  
  48.         list = newsService.selectByExample(example);  
  49.         response += "删除数据后:"+b;  
  50.         for (NewsVO newsVO : list) {  
  51.             response += JSONUtil.serialize(newsVO)+b;  
  52.         }  
  53.         write(response);  
  54.         return SUCCESS;  
  55.     }  
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值