1. After a few days, I wrote a small project and wrote it down for my future self.
The first is a screenshot of the project
and then the database table that needs to be used, only part of the data of the table is used
2. The following is the implementation of the code (the order of reverse engineering)
(1) ibatis_team_quit.xml (the configuration of sqlmaqconfig.xml is not much to say)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap> <cacheModel id="teamIntegral" type="memory" readOnly="true" serialize="false"> <flushInterval hours="24" /> </cacheModel> <update id="updateTeamUserState" > <![CDATA[ UPDATE t_team_user SET visit_state=9 WHERE user_id = (SELECT user_id FROM t_user_info WHERE openfire_username=#openfire_username#) AND team_id = #team_id# ]]> </update> </sqlMap>
(2)TeamQuitDao.java
package com.enjoy.golf.dao; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.stereotype.Repository; @Repository public class TeamQuitDao extends BaseDao{ public int quitTeam(String openfire_username,int team_id){ Map<String,Object> map = new HashMap<String, Object>(); map.put("openfire_username", openfire_username); map.put("team_id", team_id); return this.getSqlMapClientTemplate().update("updateTeamUserState",map); } }
(3)TeamQuitService.java
package com.enjoy.golf.service; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.enjoy.golf.dao.TeamQuitDao; import com.enjoy.golf.po.golf.response.BaseResponse; @Service public class TeamQuitService { @Resource TeamQuitDao teamquitDao; @Transactional(readOnly = false) //Update the player's exit status public BaseResponse quitTeam(String openfire_username,int team_id){ return teamquitDao.quitTeam(openfire_username, team_id) == 1? new BaseResponse("0", "Successfully exited"): new BaseResponse("1","Unsuccessful exit"); // int flg= teamquitDao.quitTeam(openfire_username, team_id) // if(flg == 1){ // return new BaseResponse("0", "Exited successfully"); // } // return new BaseResponse("1","Exit failed"); } }
(4)TeamQuitController.java
package com.enjoy.golf.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.enjoy.golf.po.golf.response.BaseResponse; import com.enjoy.golf.service.TeamQuitService; @Controller @RequestMapping("teamquit") public class TeamQuitController { @Resource private TeamQuitService teamquitService; @RequestMapping(value = "quit",method = RequestMethod.POST) public @ResponseBody BaseResponse quitTeam( @RequestParam(defaultValue = "")String openfire_username, @RequestParam(defaultValue = "")int team_id){ return teamquitService.quitTeam(openfire_username, team_id); } }
(5)TeamQuitDaoTest.java
package test.dao; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.enjoy.golf.dao.TeamQuitDao; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:spring/root-context.xml" }) public class TeamQuitDaoTest { @Resource TeamQuitDao teamquitDao; @Test public void testTeamQuit(){ System.out.println(teamquitDao.quitTeam("oStHpjncrfHDW_Ozk6vtVnYuDsNU", 14)); } }