iBATIS和Spring在WEB开发中的应用(三)

    技术2022-05-11  14

    11. Spring Bean 定义文件 applicationContext.xml 这个文件应该放在 WEB 包的 WEB-INF/   <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!--

    注一     < bean id = "daoConfig "

           class = " com.wysm.netstar.persistence.DaoConfig " >        < constructor-arg >            < value > com/wysm/netstar/persistence/dao.xml </ value >        </ constructor-arg >     </ bean > --> <bean id="daoManager" class="com.wysm.netstar.persistence.DaoConfig" factory-method = "getDaoManager"/>      <bean id="sequenceDao" class="com.wysm.netstar.persistence.iface.SequenceDao" factory-bean = "daoManager" factory-method="getDao">         <constructor-arg>             <value>com.wysm.netstar.persistence.iface.SequenceDao</value>         </constructor-arg> </bean>     <bean id="userDao" class="com.wysm.netstar.persistence.iface.UserDao" factory-bean = "daoManager" factory-method="getDao">         <constructor-arg>             <value>com.wysm.netstar.persistence.iface.UserDao</value>         </constructor-arg>     </bean>     <bean id="userInfoDao" class="com.wysm.netstar.persistence.iface.UserInfoDao" factory-bean = "daoManager" factory-method="getDao">         <constructor-arg>             <value>com.wysm.netstar.persistence.iface.UserInfoDao</value>         </constructor-arg>     </bean>    <bean name="userService" class="com.wysm.netstar.service.ibatis.UserServiceImpl" singleton="false">         <property name="daoMgr">             <ref bean="daoManager"/>         </property>         <property name="sequenceDao">             <ref bean="sequenceDao"/>         </property>         <property name="userDao">             <ref bean="userDao"/>         </property>         <property name="userInfoDao">             <ref bean="userInfoDao"/>         </property>     </bean>    </beans>     1 2. JAVA 环境下测试以上内容 com.wysm.netstar.test.SpringTestCase.java   package com.wysm.netstar.test; import junit.framework.TestCase;   import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext;   import com.wysm.netstar.domain.User; import com.wysm.netstar.persistence.iface.SequenceDao; import com.wysm.netstar.service.UserService; public class SpringTestCase extends TestCase {     private UserService userService;     protected void setUp() throws Exception {         super.setUp(); //         ApplicationContext ctx=new FileSystemXmlApplicationContext( "D:/JBProject/NetStarv0.1/netstar/WEB-INF/applicationContext.xml");         ApplicationContext ctx = new FileSystemXmlApplicationContext("E:/workspace/siSample/WebRoot/WEB-INF/applicationContext.xml");         userService =(UserService)ctx.getBean("userService");     }     protected void tearDown() throws Exception {         super.tearDown();     }     public void testSave(){        //自己写测试代码            User user = new User(); //          SequenceDao sq = null ; //          user.setUserId(sq.getSequenceUserId());            user.setUserAlias("fhway");            user.setUserTypeId("1");            user.setPassword("fhway");            user.setRealName("fhway");            user.setEmail("fhway@msn.com");            userService.addNewUser(user);       } }   13 iBATIS 自动话代码生成工具 Abator for iBATIS Abator is an Eclipse plugin that generates artifacts for the iBATIS Sql Mapper (including SqlMap XML files, Java domain classes, and Java DAO classes). Abator will not overwrite any custom changes you make to these artifacts, so it can be run iteratively as the domain and data models mature. 主页: http://ibatis.apache.org/abator.html 更新: http://ibatis.apache.org/tools/abator Abator for Eclipse Update Site This is the Abator for Eclipse update site. If you're not sure what Abator is, then see this page http://ibatis.apache.org/abator.html If you've already installed a prior version of Abator for Eclipse, then simply run the Eclipse Install/Update tool and any new version will be found automatically. If you've not already installed Abator, then you can use the built in Eclipse install support by following these steps:   1.       Take the "Help>Software Updates>Find and Install" Menu Option 2.       Select the "Search for new features to install" radio button, press "Next" 3.       Press the "New Remote Site" button 4.       Enter the following information:    Name:  Abator for Eclipse Update Site     URL:  http://ibatis.apache.org/tools/abator 5.       Press OK 6.       Check the box next to "Abator for Eclipse Update Site" 7.       Follow the remainder of the install wizard 13.1 abator下载:http://ibatis.apache.org/abator.html 13.2 将abator安装到eclipse中 13.3:此时可以新建一种文件类型:Abator for iBATIS Configuration File,建立一个 13.4:在 jdbcConnection 中设置要mapping的数据库的jdbc连接  classPathEntry 是你的jdbc driver类路径 13.5:javaModelGenerator,sqlMapGenerator,daoGenerator 分别设置 java dataObject、sql mapping文件和 DAO 接口、实现类的生成位置:targetPackage 目标包,targetProject:eclipse项目 13.6:daoGenerator 中可以设置属性  type: ibatis 或 spring 指定生成的dao实现类是使用com.ibatis.dao.client.template.SqlMapDaoTemplate还是org.springframework.orm.ibatis.support.SqlMapClientDaoSupport 13.7: table 中 tableName 指定要处理的表名可以有多个table 13.8:table中可以包含子元素 generatedKey: 使Insert方法可以返回值--由指定的column mapping 13.9:generatedKey中的sqlStatement属性可以是获取sequence的SQL,也可以是获取自增值的SQL  比如:Oracle的 select theSequence.nextVal from dual       SQLServer的 SELECT @@IDENTITY as  column_name 13.10:保存文件,选中文件,右键菜单选择Generate iBATIS Artifacts! ok... 13.11 : abatorConfig.xml   <? xml version = "1.0" encoding = "UTF-8" ?> <! DOCTYPE abatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Abator for iBATIS Configuration 1.0//EN"   "http://ibatis.apache.org/dtd/abator-config_1_0.dtd" >   < abatorConfiguration >   < abatorContext >     <!-- TODO: Add Database Connection Information -->     < jdbcConnection driverClass = "???"         connectionURL = "???"         userId = "???"         password = "???" >       < classPathEntry location = "???" />     </ jdbcConnection >       < javaModelGenerator targetPackage = "???" targetProject = "???" />     < sqlMapGenerator targetPackage = "???" targetProject = "???" />     < daoGenerator type = "IBATIS" targetPackage = "???" targetProject = "???" />       < table schema = "???" tableName = "???" >       < columnOverride column = "???" property = "???" />     </ table >     </ abatorContext > </ abatorConfiguration >   13.12 :abatorConfig.xml配置文件如下:   <? xml version = "1.0" encoding = "UTF-8" ?> <! DOCTYPE abatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Abator for iBATIS Configuration 1.0//EN"   "http://ibatis.apache.org/dtd/abator-config_1_0.dtd" >   < abatorConfiguration >   < abatorContext >     <!-- TODO: Add Database Connection Information -->     < jdbcConnection driverClass = "oracle.jdbc.driver.OracleDriver"         connectionURL = "jdbc:oracle:thin:@192.168.0.254:1521:tsora"         userId = "ztry"         password = "ztry" >       < classPathEntry location = "D:/workspace_soa/sclhPersonMessage/WebContent/WEB-INF/lib/classes12_10g1.jar" />     </ jdbcConnection >       < javaModelGenerator targetPackage = "com.sclh.model" targetProject = "test" />     < sqlMapGenerator targetPackage = "com.sclh.map" targetProject = "test" />     < daoGenerator type = "IBATIS" targetPackage = "com.sclh.dao" targetProject = "test" />       < table schema = "ztry" tableName = "ztry" >     </ table >     </ abatorContext > </ abatorConfiguration > 14.注意: 1. 关于SQLMap 参考 Clinton Begin 著 刘涛 (toleu@21cn.com) 的《iBATIS-SqlMaps-2_cn.pdf   2 JdbcConnection driverClass classPathEntry 的驱动一定要指定绝对路径(对 oracle 支持得比较好)   3.iBATIS对于大字段的支持:   4 .关于Sequence 的取得可以利用立下的Sample        他利用 selectKey属性取得id的值,于是省去了DAO的操作,但是在转换DB时就得稍作变化。 ORACLE : <insert id="insertProduct-ORACLE"    parameterClass="com.domain.Product">  < selectKey resultClass="int" keyProperty="id" >     SELECT STOCKIDSEQUENCE.NEXTVAL  AS  ID      FROM  DUAL  </selectKey>  insert into PRODUCT    (PRD_ID,PRD_DESCRIPTION)   values (#id#,#description#) </insert>   Microsoft SQL Server : <insert id="insertProduct-MS-SQL" parameterClass="com.domain.Product">  insert into PRODUCT (PRD_DESCRIPTION) values (#description#)  < selectKey resultClass="int" keyProperty="id" >  SELECT @@IDENTITY AS ID  </selectKey> </insert> 5. Spring的注入另一种形式 < beans >     < bean id = "dataSource"     class = "org.springframework.jdbc.datasource.DriverManagerDataSource" >        < property name = "driverClassName" >            < value > com.mysql.jdbc.Driver </ value >        </ property >        < property name = "url" >            < value > jdbc:mysql://localhost/bikestore </ value >        </ property >        < property name = "username" >            < value > bikestore </ value >        </ property >     </ bean >     < bean id = "rentaBike" class = "com.springbook.IBatisRentABike" >        < property name = "storeName" >            < value > Bruce's Bikes </ value >        </ property >        < property name = "dataSource" >            < ref local = "dataSource" />        </ property >        < property name = "sqlMap" >            < ref local = "sqlMap" />        </ property >     </ bean >     < bean id = "sqlMap"        class = "org.springframework.orm.ibatis.SqlMapFactoryBean" >        < property name = "configLocation" >            < value > /WEB-INF/ibatis.config </ value >        </ property >     </ bean >     < bean id = "transactionManager"          class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" >        < property name = "dataSource" >            < ref local = "dataSource" />        </ property >     </ bean >     ibatis.config <?xml version="1.0" encoding="UTF-8"?> <sql-map-config> <sql-map resource="Bike.xml" /> <sql-map resource="Customer.xml" /> <sql-map resource="Reservation.xml" /> </sql-map-config>  

    最新回复(0)