java +testNG 接口测试框架搭建------连接数据库(二)
在编写的测试脚本中导入连接数据的包:
import com.alibaba.fastjson.JSONObject; import com.github.yongchristophertang.database.annotations.SqlDB;//注入数据库包 import com.github.yongchristophertang.database.testng.TestNGDBInjectionModuleFactory; import com.github.yongchristophertang.engine.java.ProxyFactories; import com.google.inject.Inject; import com.qiming.qqzw.resource.api.oversea.IHanyuOverSeaSceneService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; import org.testng.annotations.BeforeClass; import org.testng.annotations.Guice; import org.testng.annotations.Test; import org.springframework.jdbc.core.JdbcTemplate;//引入包 import org.uncommons.reportng.annotation.Case; import javax.annotation.Resource; import java.util.*; import java.util.Map; @Guice(moduleFactory = TestNGDBInjectionModuleFactory.class) @ContextConfiguration(locations = "classpath:bean*.xml") @SqlDB(url="jdbc:mysql://110.1.1.1:8888/qqzw_resource_center?useUnicode=true&&characterEncoding=UTF-8",userName = "rrrr",password = "123")//输入数据库地址、用户名、密码 @Service public class TestApply extends AbstractTestNGSpringContextTests{ @Autowired private IHanyuOverSeaSceneService hanyuOverSeaSceneServiceProxy; private IHanyuOverSeaSceneService hanyuOverSeaSceneService; @Inject private JdbcTemplate jdbcTemplate; @BeforeClass public void init() { hanyuOverSeaSceneService = ProxyFactories.createLoggerProxy(hanyuOverSeaSceneServiceProxy); } @Test(priority=1) @Case("BVT test") public void pageOverseaScene_BVT_Test() { Map<String, Object> listOverseaSceneDialogueBySceneId_param = new HashMap<String, Object>(); listOverseaSceneDialogueBySceneId_param.put("pageNum",2); listOverseaSceneDialogueBySceneId_param.put("pageSize",6); JSONObject listResourceById_result= hanyuOverSeaSceneService.pageOverseaScene(listOverseaSceneDialogueBySceneId_param); Map<String, Object> data =jdbcTemplate.queryForMap("select *from resource_oversea_scene where id=?","1185");//数据使用 System.out.println(data); } }
jdbcTemplate方法的基本使用:
1.Map<String, Object>=jdbcTemplate.queryForMap("select *from resource_oversea_scene where id=?","1185");//以map的格式查找
2.List data=jdbcTemplate.queryForList("select *from resource_oversea_scene where id=?","1185");//以list格式查找
3.jdbcTemplate.update("delete from t_account where user_id=?", "26412477482795008");//删除数据