教程目标:
在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等特性以后才逐渐完成的,我在这里就把完成以后的贴在这里了
- <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/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>MybatisSpringStrut2</groupId>
- <artifactId>MybatisSpringStrut2</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <name>Mybatis+Spring+Strut2集成示例</name>
- <developers>
- <developer>
- <name>Sam</name>
- <email>sanlai_lee@foxmail.com</email>
- </developer>
- </developers>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <!-- 下面这些定义的属性在mybatis-generator.xml文件中引用 -->
- <classPathEntry.location>${basedir}/WebRoot/WEB-INF/lib/mysql-connector-java-5.1.18.jar</classPathEntry.location>
- <jdbc.driverClass>com.mysql.jdbc.Driver</jdbc.driverClass>
- <jdbc.connectionURL>jdbc:mysql://localhost:3306/test?generateSimpleParameterMetadata=true</jdbc.connectionURL>
- <jdbc.userId>root</jdbc.userId>
- <jdbc.password>1120</jdbc.password>
- <model.target.dir>${basedir}/src/java</model.target.dir>
- <mappers.target.dir>${basedir}/src/config</mappers.target.dir>
- <client.target.dir>${basedir}/src/java</client.target.dir>
- </properties>
- <!-- 依赖包 -->
- <dependencies>
- <dependency>
- <groupId>org.glassfish</groupId>
- <artifactId>bean-validator</artifactId>
- <version>3.0-JBoss-4.0.2</version>
- </dependency>
- <dependency>
- <groupId>org.glassfish</groupId>
- <artifactId>javax.annotation</artifactId>
- <version>3.0.1</version>
- </dependency>
- <dependency>
- <groupId>org.glassfish</groupId>
- <artifactId>javax.ejb</artifactId>
- <version>3.0.1</version>
- </dependency>
- <dependency>
- <groupId>org.glassfish</groupId>
- <artifactId>javax.enterprise.deploy</artifactId>
- <version>3.0.1</version>
- </dependency>
- <dependency>
- <groupId>org.glassfish</groupId>
- <artifactId>javax.jms</artifactId>
- <version>3.0.1</version>
- </dependency>
- <dependency>
- <groupId>org.glassfish</groupId>
- <artifactId>javax.management.j2ee</artifactId>
- <version>3.0.1</version>
- </dependency>
- <dependency>
- <groupId>org.eclipse.persistence</groupId>
- <artifactId>javax.persistence</artifactId>
- <version>2.0.0</version>
- </dependency>
- <dependency>
- <groupId>org.glassfish</groupId>
- <artifactId>javax.resource</artifactId>
- <version>3.0.1</version>
- </dependency>
- <dependency>
- <groupId>org.glassfish</groupId>
- <artifactId>javax.security.auth.message</artifactId>
- <version>3.0.1</version>
- </dependency>
- <dependency>
- <groupId>org.glassfish</groupId>
- <artifactId>javax.security.jacc</artifactId>
- <version>3.0.1</version>
- </dependency>
- <dependency>
- <groupId>org.glassfish</groupId>
- <artifactId>javax.servlet</artifactId>
- <version>3.0.1</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.glassfish</groupId>
- <artifactId>javax.servlet.jsp</artifactId>
- <version>3.0.1</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.glassfish</groupId>
- <artifactId>javax.servlet.jsp.jstl</artifactId>
- <version>3.0.1</version>
- </dependency>
- <dependency>
- <groupId>org.glassfish</groupId>
- <artifactId>javax.transaction</artifactId>
- <version>3.0.1</version>
- </dependency>
- <dependency>
- <groupId>javax.xml.bind</groupId>
- <artifactId>jaxb-api-osgi</artifactId>
- <version>2.2.1</version>
- </dependency>
- <dependency>
- <groupId>javax.ws.rs</groupId>
- <artifactId>jsr311-api</artifactId>
- <version>1.1.1</version>
- </dependency>
- <dependency>
- <groupId>javax.mail</groupId>
- <artifactId>mail</artifactId>
- <version>1.4.3</version>
- </dependency>
- <dependency>
- <groupId>javax.xml</groupId>
- <artifactId>webservices-api-osgi</artifactId>
- <version>2.0.1</version>
- </dependency>
- <dependency>
- <groupId>org.jboss.weld</groupId>
- <artifactId>weld-osgi-bundle</artifactId>
- <version>1.0.1-SP3</version>
- </dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.15</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.18</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.0.6</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>1.0.2</version>
- </dependency>
- <dependency>
- <groupId>xerces</groupId>
- <artifactId>xmlParserAPIs</artifactId>
- <version>2.6.2</version>
- </dependency>
- <dependency>
- <groupId>dom4j</groupId>
- <artifactId>dom4j</artifactId>
- <version>1.6.1</version>
- </dependency>
- <dependency>
- <groupId>com.sun.mail</groupId>
- <artifactId>pop3</artifactId>
- <version>1.4.4</version>
- </dependency>
- <dependency>
- <groupId>com.sun.mail</groupId>
- <artifactId>smtp</artifactId>
- <version>1.4.4</version>
- </dependency>
- <dependency>
- <groupId>cglib</groupId>
- <artifactId>cglib</artifactId>
- <version>2.2.2</version>
- <type>jar</type>
- </dependency>
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-core</artifactId>
- <version>2.2.3.1</version>
- </dependency>
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-json-plugin</artifactId>
- <version>2.2.3.1</version>
- </dependency>
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-config-browser-plugin</artifactId>
- <version>2.2.3.1</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.5</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet.jsp</groupId>
- <artifactId>jsp-api</artifactId>
- <version>2.1</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-spring-plugin</artifactId>
- <version>2.2.3.1</version>
- </dependency>
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.1.1</version>
- </dependency>
- <dependency>
- <groupId>commons-dbcp</groupId>
- <artifactId>commons-dbcp</artifactId>
- <version>1.4</version>
- </dependency>
- <dependency>
- <groupId>c3p0</groupId>
- <artifactId>c3p0</artifactId>
- <version>0.9.1.2</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>3.0.6.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>3.0.6.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>3.0.6.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <version>3.0.6.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>3.0.6.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>3.0.6.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-asm</artifactId>
- <version>3.0.6.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>3.0.6.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-expression</artifactId>
- <version>3.0.6.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>commons-pool</groupId>
- <artifactId>commons-pool</artifactId>
- <version>1.5.6</version>
- </dependency>
- </dependencies>
- <build>
- <!-- 源文件夹位置定义 -->
- <sourceDirectory>${basedir}/src/java,${basedir}/src/config</sourceDirectory>
- <!-- 输出文件夹位置定义 -->
- <outputDirectory>${basedir}/WebRoot/WEB-INF/classes</outputDirectory>
- <!-- 资源文件位置定义 -->
- <resources>
- <resource>
- <targetPath>${project.build.outputDirectory}</targetPath>
- <directory>${project.build.sourceDirectory}</directory>
- <excludes>
- <exclude>**/*.java</exclude>
- </excludes>
- </resource>
- <resource>
- <directory>${project.build.sourceDirectory}</directory>
- <includes>
- <include>**/*.xml,**/*.properties</include>
- </includes>
- </resource>
- </resources>
- <!-- war包的最终名字格式定义 -->
- <finalName>${project.artifactId}-${project.version}</finalName>
- <!-- 插件定义 -->
- <plugins>
- <!-- 打包插件 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.1.1</version>
- <configuration>
- <webappDirectory>${basedir}/WebRoot</webappDirectory>
- <warSourceDirectory>${basedir}/WebRoot</warSourceDirectory>
- <outputDirectory>${basedir}/target/output</outputDirectory>
- </configuration>
- </plugin>
- <!-- 编译插件 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.3.2</version>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- </configuration>
- </plugin>
- <!-- mybatis的代码生成插件 -->
- <plugin>
- <groupId>org.mybatis.generator</groupId>
- <artifactId>mybatis-generator-maven-plugin</artifactId>
- <version>1.3.1</version>
- <configuration>
- <configurationFile>${basedir}/src/config/mybatis-generator.xml</configurationFile>
- <verbose>true</verbose>
- <overwrite>true</overwrite>
- </configuration>
- <executions>
- <execution>
- <id>Generate MyBatis Artifacts</id>
- <goals>
- <goal>generate</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </project>
3、编辑web.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="3.0"
- 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_3_0.xsd">
- <display-name>MybatisSpringStrut2</display-name>
- <!-- struts2过滤器配置 -->
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- 集成spring -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- spring的配置文件位置 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:spring/applicationContext*.xml</param-value>
- </context-param>
- <!-- 欢迎页面配置 -->
- <welcome-file-list>
- <welcome-file>site/index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
4、struts2的配置文件:
strutsxml:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="3.0"
- 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_3_0.xsd">
- <display-name>MybatisSpringStrut2</display-name>
- <!-- struts2过滤器配置 -->
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- 集成spring -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- spring的配置文件位置 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:spring/applicationContext*.xml</param-value>
- </context-param>
- <!-- 欢迎页面配置 -->
- <welcome-file-list>
- <welcome-file>site/index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
struts/struts-admin.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>
- <!--
- result type:
- <result-types>
- <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
- <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
- <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
- <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
- <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
- <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
- <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
- <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
- <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
- <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
- </result-types>
- -->
- <package name="admin" namespace="/admin" extends="my-truts2">
- <action name="login">
- <result>/admin/login.jsp</result>
- </action>
- <action name="main">
- <result>/admin/main.jsp</result>
- </action>
- <!-- 测试拦截器 -->
- <action name="testInterceptor" class="adminAction" method="testInterceptor">
- <interceptor-ref name="adminLoginInterceptorStack"/>
- <result>/admin/main.jsp</result>
- </action>
- <!-- 测试数据访问功能CRUD -->
- <action name="testCrud" class="adminAction" method="testCrud">
- <result>/admin/main.jsp</result>
- </action>
- </package>
- </struts>
struts/struts-index.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>
- <!--
- result type:
- <result-types>
- <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
- <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
- <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
- <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
- <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
- <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
- <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
- <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
- <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
- <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
- </result-types>
- -->
- <package name="index" namespace="/" extends="my-truts2">
- <action name="index">
- <result>/site/index.jsp</result>
- </action>
- <action name="language">
- <result type="redirectAction">index</result>
- </action>
- </package>
- </struts>
5、spring的配置文件,位于src/config/spring:
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"
- 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">
- <!-- 数据源连接信息配置文件 -->
- <context:property-placeholder location="classpath*:jdbc.properties" />
- <!-- 开发环境数据源 -->
- <bean id="dataSource_dev" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="${jdbc.driverClassName.mysql_dev}" />
- <property name="url" value="${jdbc.url.mysql_dev}" />
- <property name="username" value="${jdbc.username.mysql_dev}" />
- <property name="password" value="${jdbc.password.mysql_dev}" />
- </bean>
- <!-- 生产环境数据源 -->
- <bean id="dataSource_product" class="com.mchange.v2.c3p0.ComboPooledDataSource"
- destroy-method="close">
- <property name="driverClass" value="${jdbc.driverClassName.mysql}" />
- <property name="jdbcUrl" value="${jdbc.url.mysql}" />
- <property name="user" value="${jdbc.username.mysql}" />
- <property name="password" value="${jdbc.password.mysql}" />
- <property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
- <property name="acquireIncrement" value="${c3p0.acquireIncrement}" />
- <property name="minPoolSize" value="${c3p0.minPoolSize}" />
- <property name="maxIdleTime" value="${c3p0.maxIdleTime}" />
- </bean>
- <!-- 配置sqlSessionFactory -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource_dev" />
- <!-- 自动到mappers包下面去搜索mybatis的映射文件 -->
- <property name="mapperLocations" value="classpath*:mappers/**/*.xml" />
- </bean>
- <!-- 使用Spring的事务管理器 -->
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource_dev" />
- </bean>
- </beans>
applicationContextDao.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"
- 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">
- <!-- 单个的注册 MapperFactoryBean -->
- <!--
- <bean id="managerVOMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
- <property name="mapperInterface" value="com.xylcolor.dao.ManagerVOMapper" />
- <property name="sqlSessionFactory" ref="sqlSessionFactory" />
- </bean>
- -->
- <!-- 利用MyBatis-Spring的自动装配机制:
- 自动从com.xylcolor.dao包中寻找接口装配成MapperFactoryBean
- 有了下面这个配置,那么就不需要像上面那样一个一个的去注册映射器Bean了,大大的减少了工作量
- -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.xylcolor.dao" />
- </bean>
- </beans>
applicationContextService.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"
- 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">
- <bean id="newsService" class="com.xylcolor.service.impl.NewsServiceImpl">
- <!--
- 这里的ref=newsVOMapper,就是mybatis-spring自动装配的,我们在applicationContextDao.xml
- 文件中并没有显示的注册newsVOMapper这个Bean;
- 当然这个名字为什么就是newsVOMapper呢,默认的会根据Dao接口名称按照驼峰命名习惯来生成这个名称,
- 如果你想拥有自己的名称风格,那么你可以在Dao接口上通过注解@Component或@Named来更改名称,
- 例如:
- @Component(value="newsVODao")
- public interface NewsVOMapper {……}
- 或
- @Named(value="newsVODao")
- public interface NewsVOMapper {……}
- 这样以后,这里ref就应该是ref="newsVODao"
- -->
- <property name="newsVOMapper" ref="newsVOMapper" />
- </bean>
- </beans>
applicationContextActions.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"
- 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">
- <bean id="adminAction" class="com.xylcolor.actions.AdminAction">
- <property name="newsService" ref="newsService"/>
- </bean>
- </beans>
6、mybatis的配置文件:mybatis-config.xml,这个文件在我们现在这个环境中其实可以不用,除非你一定要定义一些
typeAliases或者settings;项目中实际要用到的映射文件会由插件生成,由mybatis-spring根据你给的路径自动寻找装配:
mybatis-config.xml:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <settings>
- <setting name="cacheEnabled" value="true"/>
- <setting name="lazyLoadingEnabled" value="true"/>
- <setting name="aggressiveLazyLoading" value="true"/>
- </settings>
- <!-- 别名配置 -->
- <typeAliases>
- <typeAlias alias="NewsVO" type="com.xylcolor.vo.model.NewsVO"/>
- </typeAliases>
- </configuration>
7、代码生成插件的配置文件:mybatis-generator.xml
- <?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">
- <!-- mybatis-generator插件生成代码的配置文件 -->
- <generatorConfiguration>
- <classPathEntry location="${classPathEntry.location}" />
- <context id="MySQLTables" targetRuntime="MyBatis3">
- <jdbcConnection driverClass="${jdbc.driverClass}"
- connectionURL="${jdbc.connectionURL}"
- userId="${jdbc.userId}"
- password="${jdbc.password}">
- </jdbcConnection>
- <!-- java类型处理器,默认的就这么一个处理器可以用,可以自己写,具体的请参考官方文档http://code.google.com/p/mybatis/wiki/Generator -->
- <javaTypeResolver>
- <property name="forceBigDecimals" value="false" />
- </javaTypeResolver>
- <!-- 生成 Model,POJO -->
- <javaModelGenerator targetPackage="com.xylcolor.vo.model" targetProject="${model.target.dir}">
- <property name="enableSubPackages" value="true" />
- <property name="trimStrings" value="true" />
- </javaModelGenerator>
- <!-- 生成映射文件*.xml -->
- <sqlMapGenerator targetPackage="mappers" targetProject="${mappers.target.dir}">
- <property name="enableSubPackages" value="true" />
- </sqlMapGenerator>
- <!-- 生成映射器类 -->
- <javaClientGenerator type="XMLMAPPER" targetPackage="com.xylcolor.dao" targetProject="${client.target.dir}">
- <property name="enableSubPackages" value="true" />
- </javaClientGenerator>
- <!-- 指定要生成的表名和对应形成的POJO类名 -->
- <table schema=""
- tableName="tb_news"
- domainObjectName="NewsVO"/>
- </context>
- </generatorConfiguration>
完成上面那些配置文件以后,基本上配置上就完成了,下面的工作就是java代码的完成了,具体的示例代码见附件,必要的地方我都做了注释。
看看实际运行情况:
1.国际化:
2:利用插件生成的代码,完成数据的增删改查:
- /**
- * 测试数据的增删改查
- * @return
- * @throws JSONException
- */
- public String testCrud() throws JSONException{
- String b = "<br />";
- String response = "测试数据的增删改查:<br />";
- //查
- NewsVOExample example = new NewsVOExample();
- example.or().andIdIsNotNull();
- List<NewsVO> list = newsService.selectByExample(example);
- response += "原始数据:<br />";
- for (NewsVO newsVO : list) {
- response += JSONUtil.serialize(newsVO)+b;
- }
- //增
- NewsVO record = new NewsVO();
- record.setTitle("这是我新增的一个新闻");
- record.setContent("2012其实真的快到了");
- record.setSource("火星资讯");
- record.setDate(new Date());
- int result = newsService.insert(record);
- response += result==1?"插入新数据成功":"插入数据失败"+b;
- list = newsService.selectByExample(example);
- response += "插入数据后:"+b;
- for (NewsVO newsVO : list) {
- response += JSONUtil.serialize(newsVO)+b;
- }
- //改
- record = newsService.selectByExample(example).get(0);
- response += "修改前的数据:"+b+JSONUtil.serialize(record);
- example = new NewsVOExample();
- example.or().andTitleEqualTo(record.getTitle());
- int originId = record.getId();
- record.setTitle("这是我修改后的标题");
- result = newsService.updateByExample(record, example);
- response += result==1?"修改成功":"修改失败"+b;
- example.or().andTitleEqualTo(record.getTitle());
- record = newsService.selectByPrimaryKey(originId);
- response +="修改后的数据:"+b+JSONUtil.serialize(record)+b;
- //删
- example.or().andTitleEqualTo("这是我新增的一个新闻");
- newsService.deleteByExample(example);
- example.or().andIdIsNotNull();
- list = newsService.selectByExample(example);
- response += "删除数据后:"+b;
- for (NewsVO newsVO : list) {
- response += JSONUtil.serialize(newsVO)+b;
- }
- write(response);
- return SUCCESS;
- }