代码例如以下:
mybatis配置文件:
<?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="mybatis-config/jdbc.properties" /> <typeAliases> <typeAlias alias="SupplierInfo" type="ec.qa.autotest.ui.dao.model.SupplierInfo" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="mybatis-sqlmap/supplier.xml" /> </mappers> </configuration>
JDBC配置文件:
##for mysql jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://db-XXXX:3306?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&allowMultiQueries=true jdbc.username=qa jdbc.password=XXXXXX
供应商数据对象类:
package ec.qa.autotest.ui.dao.model; /** * * @author xin.wang * 供应商信息 * */ public class SupplierInfo { private String email; private String supplier_id; private String company_name; private int logical_del; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getSupplier_id() { return supplier_id; } public void setSupplier_id(String supplier_id) { this.supplier_id = supplier_id; } public String getCompany_name() { return company_name; } public void setCompany_name(String company_name) { this.company_name = company_name; } public int getLogical_del() { return logical_del; } public void setLogical_del(int logical_del) { this.logical_del = logical_del; } }
DAO层接口
package ec.qa.autotest.ui.dao; import java.util.List; import ec.qa.autotest.ui.dao.model.SupplierInfo; public interface SupplierOperationDao { public SupplierInfo seletSupplierInfo(String emali); public List<SupplierInfo> seletSupplierInfo(List<String > emalis); public void updateSupplierInfo(SupplierInfo info); public void updateSupplierInfo(List<SupplierInfo > infos); public void logicDeleteSupplierInfo(String attr); }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.qa.autotest.ui.dao"> <select id="selectSupplierInfo" resultType="SupplierInfo"> select * from supplier.sup_supplier where email = #{email,jdbcType=VARCHAR} and logical_del = 0 </select> <update id="deleteSupplier"> update supplier.sup_supplier set logical_del = 1 where supplier_id = #{supplier_id,jdbcType=VARCHAR} </update> <update id="deleteSupplierInfo"> update supplier.sup_auth_info set logical_del = 1 where supplier_id = #{supplier_id,jdbcType=VARCHAR} </update> <update id="deleteSupplierInfoLog"> update supplier.sup_auth_info_log set logical_del = 1 where supplier_id = #{supplier_id,jdbcType=VARCHAR} </update> <update id="deleteSupplierLinkEffectiveness"> update supplier.sup_link_effectiveness set logical_del = 1 where email = #{email,jdbcType=VARCHAR} </update> </mapper>
DAO实现类(注意加入pageobject标签,为用例自己主动注入此DAO实例):
package ec.qa.autotest.ui.dao.impl; import java.util.List; import org.apache.ibatis.session.SqlSession; import ec.qa.autotest.ui.dao.SupplierOperationDao; import ec.qa.autotest.ui.dao.model.SupplierInfo; import ec.qa.autotest.ui.framework.annotation.PageObject; import ec.qa.autotest.ui.framework.init.InitMybatisSession; /** * @author xin.wang 供应商数据库操作 * */ @PageObject public class SupplierOpertaionDaoImpl implements SupplierOperationDao { private static SqlSession session; public SupplierInfo seletSupplierInfo(String emali) { SupplierInfo si = null; session = InitMybatisSession.getSessionFactory().openSession(); try { si = session.selectOne("com.qa.autotest.ui.dao.selectSupplierInfo", emali); session.commit(); } catch (Exception e) { } finally { session.close(); } return si; } public List<SupplierInfo> seletSupplierInfo(List<String> emalis) { // TODO Auto-generated method stub return null; } public void updateSupplierInfo(SupplierInfo info) { // TODO Auto-generated method stub } public void updateSupplierInfo(List<SupplierInfo> infos) { // TODO Auto-generated method stub } public void logicDeleteSupplierInfo(String email) { SupplierInfo si = null; session = InitMybatisSession.getSessionFactory().openSession(false); try { si = session.selectOne("com.qa.autotest.ui.dao.selectSupplierInfo", email); session.update("com.qa.autotest.ui.dao.deleteSupplier", si.getSupplier_id()); session.update("com.qa.autotest.ui.dao.deleteSupplierInfo", si.getSupplier_id()); session.update("com.qa.autotest.ui.dao.deleteSupplierInfoLog", si.getSupplier_id()); session.update("com.qa.autotest.ui.dao.deleteSupplierLinkEffectiveness", si.getEmail()); session.commit(true); } catch (Exception e) { session.rollback(); e.printStackTrace(); } finally { session.close(); } } }
package ec.qa.autotest.ui.admin.portal.testcases; import org.testng.annotations.Test; import ec.qa.autotest.ui.dao.impl.SupplierOpertaionDaoImpl; import ec.qa.autotest.ui.framework.annotation.AutoInject; import ec.qa.autotest.ui.testbase.AdminPortalTestBase; /** * * @auther xin.wang *依据邮箱名逻辑删除SUPPLIER信息 */ //@Listeners({ TestngRetryListener.class }) public class LoginAdminPortal extends AdminPortalTestBase{ @AutoInject private SupplierOpertaionDaoImpl sd; @Test(invocationCount = 1) public void loginAdminPortal() throws Exception{ sd.logicDeleteSupplierInfo("123456@qq.com"); } }