在这里,我根据收到的客户端名称动态创建dao bean,并调用dao方法
public static DynamicService get(String client) {
try {
return (DynamicService)Support.context.getBean(client+"DynamicService");
}catch (NoSuchBeanDefinitionException e) {
GenericBeanDefinition gbd = new GenericBeanDefinition();
gbd.setBeanClass(DynamicService.class);
gbd.getPropertyValues().addPropertyValue("client", client);
Support.context.registerBeanDefinition(client+"DynamicService", gbd);
return (DynamicService)Support.context.getBean(client+"DynamicService");
}
}
public void setClient(String client) {
this.client=client;
dynamicDao=DynamicDao.get(client);
}
public String getUserById(int id) throws SQLException {
return dynamicDao.getUserById(id);
}
@Transactional(value="chainedTransactionManager")
public ZcMap getUsers(JqGridUtil jqGridUtil) throws Exception {
return dynamicDao.getUsers(jqGridUtil);
}
@Transactional(rollbackFor = Exception.class)
public int updateUser(ZcMap reqData, int id) {
return dynamicDao.updateUser(reqData,id);
}
@Transactional(rollbackFor = Exception.class)
public int saveUser(ZcMap reqData) throws Exception {
return dynamicDao.saveUser(reqData);
}
@Transactional(rollbackFor = Exception.class)
public void deleteUser(int id) {
dynamicDao.deleteUser(id);
}
}
在这里,我正在根据客户端动态注册dao bean和datasorce,并为该数据源注册Transaction
public static DynamicDao get(String client) {
try {
return (DynamicDao)Support.context.getBean(client+"DynamicDao");
}catch (NoSuchBeanDefinitionException e) {
GenericBeanDefinition gbd = new GenericBeanDefinition();
gbd.setBeanClass(DynamicDao.class);
gbd.getPropertyValues().addPropertyValue("client", client);
Support.context.registerBeanDefinition(client+"DynamicDao", gbd);
return (DynamicDao)Support.context.getBean(client+"DynamicDao");
}
}
public void setClient(String client) throws SQLException {
jdbcTemplate=new JdbcTemplate(AppDataSource.get(client));
}
private JdbcTemplate jdbcTemplate;
public String getUserById(int id) {
if(jdbcTemplate==null) return null;
try {
Map<String, Object> data= jdbcTemplate.queryForMap("SELECT * FROM app_user WHERE pk_id=?",id);
return data.get("name")+"";
}catch (Exception e) {
e.printStackTrace();
return null;
}
}
@SuppressWarnings("serial")
public ZcMap getUsers(JqGridUtil req) throws Exception {
if(jdbcTemplate==null) return null;
ZcMap cols = new ZcMap(new Object[][]{{"name","name"},{"pk_id", "pk_id"}});
if(ZcUtil.isBlank(req.sidx))req.sidx=cols.getS("name");
else req.sidx=cols.getS(req.sidx);
String query="SELECT SQL_CALC_FOUND_ROWS name,pk_id FROM app_user LIMIT "+req.start+","+req.rows+"";
List<Map<String, Object>> data=jdbcTemplate.queryForList(query);
int totalCount=ZcUtil.parseInt(jdbcTemplate.queryForMap("SELECT found_rows() rows").get("rows"));
return new ZcMap(){{
put("rows",data);
put("totalCount",totalCount);
}};
}
public int updateUser(ZcMap reqData, int id) {
jdbcTemplate.update("UPDATE app_user SET name=? WHERE pk_id=?",reqData.getS("name"),id);
jdbcTemplate.queryForList("SELECT * FROM app_user WHERE ;pk_id=1;");
return id;
//
}
public int saveUser(ZcMap reqData) throws Exception {
int id=getGeneratedKey(jdbcTemplate, "INSERT INTO app_user(`name`) VALUES (?)", new String[] {reqData.getS("name")});
return id;
}
public void deleteUser(int id) {
jdbcTemplate.update("DELETE FROM app_user WHERE pk_id=?",id);
}
但是如何访问或使用交易
这里我要为dao创建bean,并为该数据源配置Transaction
public static BasicDataSource get(String client) {
try {
return (BasicDataSource) Support.context.getBean(client + "BasicDataSource");
} catch (NoSuchBeanDefinitionException e) {
GenericBeanDefinition gbd = new GenericBeanDefinition();
gbd.setBeanClass(BasicDataSource.class);
Map<String, Object> data = clientDetails(client);
for (Map.Entry<String, Object> entry : data.entrySet()) {
gbd.getPropertyValues().addPropertyValue(entry.getKey(), entry.getValue());
}
Support.context.registerBeanDefinition(client + "BasicDataSource", gbd);
BasicDataSource basicDataSource = (BasicDataSource) Support.context.getBean(client + "BasicDataSource");
attachTransactionManager(basicDataSource, client);
return basicDataSource;
}
}
public static void attachTransactionManager(BasicDataSource basicDataSource, String client) {
GenericBeanDefinition gbd = new GenericBeanDefinition();
gbd.setBeanClass(DataSourceTransactionManager.class);
gbd.getPropertyValues().addPropertyValue("dataSource", basicDataSource);
Support.context.registerBeanDefinition(client + "TransactionManager", gbd);
}
public static Map<String, Object> clientDetails(String client) {
String host = "";
String db = client;
if ("jagadeesh".equals(client)) {
host = "192.168.0.43";
} else if ("praneeth".equals(client)) {
host = "192.168.0.5";
} else if ("vikram".equals(client)) {
host = "192.168.0.47";
}
Map<String, Object> data = new HashMap<String, Object>();
data.put("driverClassName", "com.mysql.cj.jdbc.Driver");
data.put("url", "jdbc:mysql://" + host + ":3306/" + db
+ "?allowMultiQueries=true&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC");
data.put("username", "admin");
data.put("password", "the@123");
data.put("poolPreparedStatements", "true");
data.put("maxIdle", 10);
data.put("validationQuery", "SELECT 1");
data.put("testOnBorrow", "true");
data.put("connectionProperties", "autoReconnect=true;useUnicode=true;characterEncoding=UTF-8");
return data;
}
}