在昨天的工作基础上,今天实现了数据的查询和新增操作。在操作的过程中又学习了一些新的知识,这种边学边练的方式相对看书和视频来比效率还是挺高的。下面是今天的部分重要代码。
LthController.java
@Controller
public class LthController {
private static final Logger logger = LoggerFactory.getLogger(LthController.class);
@Autowired
private LthService lthService;
@RequestMapping(value = "/Lth", method = RequestMethod.GET)
public String Lth() {
logger.info("执行lth界面!!! ");
return "lth";
}
@RequestMapping(value = "/selectLth", method = RequestMethod.POST)
public @ResponseBody String selectLth(@RequestBody String str) {
logger.info("selectLth " + str);
JSONObject jsonObj = JSONObject.fromObject(str);
MyBatisPage page = new MyBatisPage();
page.setPage(jsonObj.getInt("page"));
page.setPageSize(jsonObj.getInt("pageSize"));
page.setSkip(jsonObj.getInt("skip"));
page.setTake(jsonObj.getInt("take"));
logger.info( lthService.selectLth(page));
return lthService.selectLth(page);
}
@RequestMapping(value="/addLth",method=RequestMethod.POST)
public String addLth(@RequestBody String str){
logger.info("addLth" +str);
JSONObject jsonObj=JSONObject.fromObject(str);
lthService.addLth(jsonObj);
return null;
}
LthDaoImpl.java
@Repository
public class LthDAOImpl implements LthDao {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
@Override
public List<Map<String, Object>> getAll(MyBatisPage page) {
return sqlSessionTemplate.selectList("LthDao.selectFenYe",page);
}
@Override
public int getSum() {
List<Map<String,Object>> a =sqlSessionTemplate.selectList("LthDao.selectAll");
return a.size();
}
@Override
public void add(Jc_Zwxx zwxx) {
sqlSessionTemplate.insert("LthDao.insertZwxx", zwxx);
}
}
LthServiceImpl.java
@Service
public class LthServiceImpl implements LthService{
@Autowired
private LthDao lthDao;
@Override
public String selectLth(MyBatisPage page) {
int sum = lthDao.getSum();
List<Map<String,Object>>lth = lthDao.getAll(page);
JSONObject jsonObj = new JSONObject();
jsonObj.accumulate("total", sum);
jsonObj.accumulate("data", JSONArray.fromObject(lth).toString());
return jsonObj.toString();
}
@Override
public String addLth(JSONObject jsonObj) {
// TODO Auto-generated method stub
lthDao.add(processZwxx(jsonObj));
return null;
}
public Date StringToDate(String dateStr){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
if(dateStr==null || dateStr.equals("")){
return null;
}else{
Date date = null;
try {
date = df.parse(dateStr);
} catch (ParseException e) {
return null;
}
return date;
}
}
private Jc_Zwxx processZwxx(JSONObject json){
Jc_Zwxx zwxx = new Jc_Zwxx();
if(json.get("YHID")!=null){
zwxx.setYhid(Integer.parseInt(json.get("YHID").toString()));
}
if(json.get("TZ01")!=null){
zwxx.setTz01(json.get("TZ01").toString());
}
if(json.get("TZ02")!=null){
zwxx.setTz02(json.get("TZ02").toString());
}
if(json.get("TZ03")!=null){
zwxx.setTz03(json.get("TZ03").toString());
}
if(json.get("TZ04")!=null){
zwxx.setTz04(json.get("TZ04").toString());
}
if(json.get("TZ05")!=null){
zwxx.setTz05(json.get("TZ05").toString());
}
if(json.get("TZ06")!=null){
zwxx.setTz06(json.get("TZ06").toString());
}
if(json.get("TZ07")!=null){
zwxx.setTz07(json.get("TZ07").toString());
}
if(json.get("TZ08")!=null){
zwxx.setTz08(json.get("TZ08").toString());
}
if(json.get("TZ09")!=null){
zwxx.setTz09(json.get("TZ09").toString());
}
if(json.get("TZ10")!=null){
zwxx.setTz10(json.get("TZ10").toString());
}
if(json.get("XM")!=null){
zwxx.setXm(json.get("XM").toString());
}
if(json.get("SCSJ")!=null){
zwxx.setScsj(StringToDate(json.get("SCSJ").toString()));
}
if(json.get("GXSJ")!=null){
zwxx.setGxsj(StringToDate(json.get("GXSJ").toString()));
}
if(json.get("BZ")!=null){
zwxx.setBz(json.getString("BZ").toString());
}
return zwxx;
}
}