首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Spring+MyBatis+Struts2调整

2012-08-24 
Spring+MyBatis+Struts2整合框架环境:Spring 3.0.5 MyBatis 3.0.4(ibatis已正式更名为MyBatis) Struts2MyB

Spring+MyBatis+Struts2整合
框架环境:
Spring 3.0.5 MyBatis 3.0.4(ibatis已正式更名为MyBatis) Struts2
MyBatis代码生成:mybatis-generator-core-1.3.1.jar
我是按照三个框架的整合步骤来做的。Spring+MyBatis+Struts2整合与传统的SSH框架不同的区别就在于把Spring和Hibernate的整合换成Spring+MyBatis。
代码层级的变动就是DAO层级以及实现的变更。
资料手册:MyBatis 3 User Guide Simplified Chinese.pdf(对照配置文件说明)
第一步:
新建Web工程,加入jar包。
第二部:
配置Web.xml 加入Struts2 和Spring Listener

<?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>Web Application Tomcat 6 Servlet 3.0 JSP 2.2</display-name><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  </filter-class><init-param><param-name>config</param-name><param-value>struts-default.xml,configs/struts/struts.xml</param-value></init-param></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>*.action</url-pattern></filter-mapping><filter-mapping><filter-name>struts2</filter-name><url-pattern>*.jsp</url-pattern></filter-mapping><!-- 用于初始化Spring容器的Listener --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><!-- 配置spring配置文件的路径 --><param-name>contextConfigLocation</param-name><param-value>classpath:configs/spring/*-context.xml</param-value></context-param><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>


第三部:配置Spring配置文件
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!--配置数据源--><bean id="dataSource" destroy-method="close">       <!-- 指定连接数据库的驱动 -->       <property name="driverClass" value="com.mysql.jdbc.Driver"/>       <!-- 指定连接数据库的URL -->       <property name="jdbcUrl" value="jdbc:mysql://192.168.1.121/test?autoReconnect=true&amp;useUnicode=true&amp;createDatabaseIfNotExist=true&amp;characterEncoding=utf-8"/>       <!-- 指定连接数据库的用户名 -->       <property name="user" value="root"/>       <!-- 指定连接数据库的密码 -->       <property name="password" value="root"/>       <!-- 指定连接数据库连接池的最大连接数 -->       <property name="maxPoolSize" value="20"/>       <!-- 指定连接数据库连接池的最小连接数 -->       <property name="minPoolSize" value="1"/>       <!-- 指定连接数据库连接池的初始化连接数 -->       <property name="initialPoolSize" value="1"/>       <!-- 指定连接数据库连接池的连接的最大空闲时间 -->       <property name="maxIdleTime" value="20"/>    </bean> <!-- TransactionManager --><bean id="transactionManager"/></property></bean><!--MyBatis的会话工厂 注意配置文件路径--><bean id="sqlSessionFactory" value="classpath:configs/mybatis-config.xml"></property><property name="dataSource" ref="dataSource"></property></bean><!--Mapper由Spring统一管理--><bean id="StudentMapper" ref="sqlSessionFactory"></property><property name="mapperInterface" value="dao.StudentMapper"></property></bean><bean id="T1Mapper" ref="sqlSessionFactory"></property><property name="mapperInterface" value="mapper.T1Mapper"></property></bean><bean id="StudentService" ref="StudentMapper"></property></bean><!-- 事务拦截器 代理--> <bean id="baseTransactionProxy"/></property><!--    下面定义事务传播属性--> <property name="transactionAttributes"><props><prop key="find*">PROPAGATION_REQUIRED,readOnly</prop><prop key="add*">PROPAGATION_REQUIRED</prop><prop key="modify*">PROPAGATION_REQUIRED</prop><prop key="remove*">PROPAGATION_REQUIRED</prop><prop key="do*">PROPAGATION_REQUIRED</prop></props></property></bean> <!-- 事务实例动态代理 --> <bean id="autoProxyCreator"name="code"><?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><properties resource="configs/database/jdbc.properties"></properties><settings><setting name="lazyLoadingEnabled" value="false" /></settings><!--配置与数据库的链接 参数定义在jdbc.properties中 跟Spring整合 这段可以省略--><environments default="development"><environment id="development"><transactionManager type="JDBC" /><dataSource type="POOLED"><property name="driver" value="${driverClassName}" /><property name="url" value="${url}" /><property name="username" value="${username}" /><property name="password" value="${password}" /></dataSource></environment></environments> <mappers><mapper resource="model/T1-sqlMapper.xml"/><mapper resource="mapper/StudentMapper.xml"/></mappers></configuration>

第五步:学习如何用MyBatis代码生成器
新建mybatis-generator.xml放在src目录下
<?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:\lib\mysql-connector-java-5.0.8-bin.jar" />    <context id="DB2Tables" targetRuntime="MyBatis3">        <commentGenerator>            <property name="suppressDate" value="true" />        </commentGenerator>        <jdbcConnection driveruserId="root" password="root">        </jdbcConnection>        <javaTypeResolver>            <property name="forceBigDecimals" value="false" />        </javaTypeResolver>        <javaModelGenerator targetPackage="model"            targetProject="D:\workspace\SSI\src">            <property name="enableSubPackages" value="true" />            <property name="trimStrings" value="true" />        </javaModelGenerator>        <sqlMapGenerator targetPackage="mapper"            targetProject="D:\workspace\SSI\src">            <property name="enableSubPackages" value="true" />        </sqlMapGenerator>        <javaClientGenerator type="XMLMAPPER"            targetPackage="dao" targetProject="D:\workspace\SSI\src">            <property name="enableSubPackages" value="true" />        </javaClientGenerator>        <table tableName="push_info" domainObjectName="PushInfo">        </table> <table tableName="stu" domainObjectName="Student">        </table>    </context></generatorConfiguration>



通过java类的方式直接执行MyBatis代码生成

public class MyBatisGeneratorTest {public static void main(String[] args) throws Exception {List<String> warnings = new ArrayList<String>();boolean overwrite = true;File configFile = new File("src\\configs\\mybatis-generator.xml");System.out.println(configFile.getAbsolutePath());ConfigurationParser cp = new ConfigurationParser(warnings);Configuration config = cp.parseConfiguration(configFile);DefaultShellCallback callback = new DefaultShellCallback(overwrite);MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,callback, warnings);myBatisGenerator.generate(null);}}

1 楼 qianyeyouyi 2011-07-15   有源码木有的 2 楼 lcl_1210 2011-08-13   能提供完整的源码不

热点排行