- 浏览: 441274 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
litainiu:
点亮了,感谢
Maven+Struts2+MyBatis+Spring搭建教程 -
y413059495:
对了 部署完跑起来了 完美!
Maven+Struts2+MyBatis+Spring搭建教程 -
springaop_springmvc:
可以参考最新的文档:如何在eclipse jee中检出项目并转 ...
Maven+Struts2+MyBatis+Spring搭建教程 -
郑智睿:
把这句去掉,你就挂了<!DOCTYPE conf ...
Maven+Struts2+MyBatis+Spring搭建教程 -
zlfzml:
不行,没什么用
jquery uploadify插件多文件上传
教程目标:
在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; }
- MybatisSpringStrut2.zip (832.2 KB)
- 下载次数: 1659
评论
"http://mybatis.org/dtd/mybatis-3-config.dtd">
你说啥,我没看懂!
发表评论
-
Spring报cvc-complex-type.2.3: Element 'bean' cannot have character [children]
2012-09-26 14:11 38210今天在STS下面启动项目的时候,Spring报如下错: ... -
JTextArea自动滚动到最低端
2012-07-19 13:41 1176…… jScrollPane = new JScrol ... -
网站代码完成,正式上线使用,纪念一下
2012-03-03 20:58 1470朋友他爹的公司,公司成立不久,网站我一个人业余时间胡乱写的,完 ... -
IP地址查询接口收集
2011-12-28 21:03 3178新浪 IP 地址查询接口:http://counter.sin ... -
C3P0 配置详细说明
2011-12-19 18:41 1254<c3p0-config><default- ... -
Struts2 常量配置
2011-12-17 16:50 1599### 指定加载struts2配置文件管理器,默认为org.a ... -
Flex Builder 4.5 正式版eclipse插件利用BlazeDS于java交互实例
2011-11-09 22:24 3301Flash Builder 4.5的插件好像跟3在细节上有点不 ... -
Java 发送邮件
2011-06-24 16:36 1474/** * 邮件工具 */ package t ... -
JVM调优总结
2011-05-25 17:30 1099堆大小设置JVM 中最大堆大小有三方面限制:相关操作 ... -
java调用shell
2011-05-09 12:34 1876转自:http://www.cnblogs.com/Sea ... -
如何使用Log4j?
2011-01-04 16:32 10271、 Log4j是什么? Log4j ... -
根据表信息生成JavaVO
2010-11-02 10:54 3135例子采用MySQL数据库 代码: package c ... -
html表格导出Excel
2010-09-19 23:37 4184<table id="export_table ... -
BBS论坛系统
2010-09-07 23:45 2012今天上班的时候,有位陌生朋友跟我聊天,问我可有BBS论坛的源码 ... -
struts2.0基础配置-附加实例
2010-08-05 11:26 1487struts的作用不多说了,它充当了MVC中的C的角色 ... -
java socket 多线程实例
2010-07-26 09:06 6229题: 做一个简单的网络控制软件,分为Server端和Cli ... -
jquery uploadify插件多文件上传
2010-06-01 13:07 128964效果图: 1、jqu ... -
kindeditor的图片上传 struts2实现
2010-05-31 11:18 9084效果: 1、kindeditor下载:http://ww ... -
BufferedReader 和BufferedWriter 实例
2010-05-23 12:57 3477/** * 文件处理工具类 * @author sam ... -
利用Ext.ux.UploadDialog异步实现多文件上传
2010-05-15 10:35 113881、先看看效果: 接下来详细说说怎么用它:java ...
相关推荐
公司框架是SSH,hibernate用的越来越少,做了几年后,也懒得用了,springjdbc玩到现在,maven,mybatis没用到一直都没去接触,感慨...也当学习下,于是花了两天时间搭建了eclipse+mavaen+struts2+spring+mybatis基础环境
本项目"maven+struts2+spring+mybatis+springMVC"就是一个典型的Java Web应用开发组合,利用了这些框架的优势进行集成,实现了数据持久化、业务逻辑处理、视图展示以及依赖管理等多个层面的功能。 首先,我们来详细...
通过"Maven+Struts2+Spring+Mybatis",开发者可以快速地搭建出稳定、可扩展的Web应用。每个组件都有其独特的优势,共同构成了一个强大的开发环境,帮助开发者专注于业务逻辑,提高生产力。这个框架组合的广泛应用...
【基于注解的Spring+Struts2+Mybatis+Maven项目详解】 在现代Web开发中,Spring、Struts2、Mybatis和Maven是四个非常重要的组件,它们各自负责不同的职责,共同构建了一个高效、灵活且易于维护的Java Web应用。这个...
总的来说,"struts2+spring+mybatis+easyui"的实现是一个标准的Java Web项目结构,它利用Maven进行构建管理,通过整合四个组件,实现了后端的业务逻辑处理、数据访问和前端的用户界面展示。这种架构在实际开发中具有...
标题 "idea14+maven+struts+spring+mybatis框架" 描述的是一个使用IntelliJ IDEA 14集成开发环境,结合Maven构建工具,以及SSH(Struts、Spring、Hibernate)经典企业级开发框架,并融入MyBatis持久层框架的项目。...
Struts2、Maven、Spring和MyBatis是Java Web开发中的四大框架,它们的整合应用可以构建出高效、模块化的应用程序。这个实例是关于如何将这些技术融合在一起,实现一个用户注册的功能。 首先,Struts2是一个基于MVC...
Struts2+spring+Mybatis+mysql+架包相对完善的一个项目架子,log4j也向对完善。 学习Mybatis和搭架子的朋友学习下,希望能提出宝贵意见。 ----明月
【标题】"myeclipse+maven管理struts2+spring+mybatis" 是一个整合了四大框架的示例项目,适合初学者进行学习和实践。这个项目利用MyEclipse作为集成开发环境,Maven作为构建工具,Struts2作为表现层框架,Spring...
**SSI(Struts + Spring + Maven + MyBatis)整合详解** 在现代Java Web开发中,集成多种框架来实现高效、灵活的应用程序架构是常见的做法。`SSI`整合,即Struts、Spring、Maven和MyBatis的组合,旨在提供一个强大...
Struts2、MyBatis和Spring是Java Web开发中常用的三大框架,它们分别负责MVC模式中的Action层、数据持久层和应用上下文管理。这篇文档将深入探讨如何将这三个框架整合在一起,以及如何结合数据库进行实际应用。 ...
"maven+spring3+struts2+hibernate4+mybatis3整合"是一个常见的Java Web开发组合,它们各自扮演着不同的角色,协同工作以提供全面的功能。 首先,Maven是一个项目管理工具,它负责构建、依赖管理和项目信息管理。...
### Spring+Hibernate+Struts2+MyBatis 整合笔记 #### 一、SSM框架简介及整合意义 SSM框架是指Spring、SpringMVC和MyBatis三个开源框架的整合,通常用于快速开发Java Web应用。本篇笔记主要介绍如何将Spring、...
这个项目是一个基于Struts2+Spring+MyBatis的大学生兼职管理系统,分为管理员和企业用户和普通用户三种角色。 管理员权限包括: 修改登录密码 信息类别管理 用人单位管理 普通用户管理 系统公告管理 新闻资讯管理 ...
1. 引入依赖:在项目中添加Struts2、MyBatis和Spring的相应库,通常通过Maven或Gradle的依赖管理来实现。 2. 配置Struts2:创建struts.xml文件,定义Action类和对应的处理方法。例如: ```xml *" class=...
在"Struts2+Spring+Mybatis(Maven)整合"的实践中,首先需要在pom.xml中添加这些框架的依赖,然后配置Spring的ApplicationContext.xml,定义Bean的实例化和依赖关系。接着,创建Struts2的配置文件struts.xml,定义...
总的来说,"maven-ssm-web-demo"项目展示了如何将Spring、Struts2、MyBatis和EasyUI这些组件整合在一起,构建一个完整的Java Web应用。这个框架组合不仅提供了强大的功能,还便于开发、维护和扩展,是Java开发人员...