日期:2014-05-16  浏览次数:20411 次

mybatis3+spring 3+ext js整合套路
mybatis3+spring 3+ext js整合套路,可以小结一下.先来看mybatis 3及模型

 
1 首先是MODEL层,比如对于用户管理
   先来个user.java,是个POJO

2 再搞一个persisten层,存放mybatis 3的mapper接口和文件,注意把它们都放
入一个包中,比如
 
public interface UserMapper {
	Long getId();
	User login(Map<String,Object> param);
	User getUser(Map<String,Object> param);
	List<User> getUserList();
	List<Map<String,Object>> getUserNameList();
	void insertUser(User user);


   usermapper.xml:
  <mapper namespace="com.user.persistence.UserMapper">
   <insert id="insertUser" parameterType="User" >
  	INSERT INTO t_user (id,name, password, create_date)
    VALUES (#{id},#{name}, #{password}, #{createDate,jdbcType=TIMESTAMP})
  </insert>
   



3 userdao层

 

@Component
public class UserDao extends SqlSessionDaoSupport {
public void insertUser(User user) {
		getSqlSession().insert("com.liao.user.persistence.UserMapper.insertUser", user);
	}

 ..............


  
4 service层
   
@Service
//默认将类中的所有函数纳入事务管理.
@Transactional
public class UserService{
	@Autowired
	private UserMapper userMapper;

 public void insertUser(User user,Map<String,Object> param) {
		//新增用户
		userMapper.insertUser(user);




5 controller层
  
@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;

@RequestMapping(value="/save",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> saveOrUpdate(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();

  User user = new User();
				Long newId = userService.getId();
								user.setId(newId);
				user.setCreateDate(new Date());
				user.setName(name);
				user.setPassword(password);
				Map<String,Object> param = new HashMap<String,Object>();
				param.put("user_id", newId);
								userService.insertUser(user, param);
				responseMap.put("method", "Create");
				responseMap.put("success", "true");
				responseMap.put("info", "新增成功!");
			}
			return responseMap;




  注意,由于要返回相关的信息JSON格式给前端EXT,因此注意这里使用了
spring mvc 3中的很多技术,加个@responsebody就能生成JSON了,十分方便
,所以返回时,用一个MAP保存了要给前端显示的EXT JS信息了.


6 前端EXT JS要点:
    getForm: function() {
            var form = new Ext.form.FormPanel({
url:'user/save',
                        ....................
            ..........
  
  submit: function() {
            if (this.form.getForm().isValid()) {
                var id = this.form.getForm().findField("id").getValue();
                this.form.getForm().submit({
                    waitTitle: '保存数据',
                    waitMsg: '正在保存……',
                    scope: this,
                    method: 'post',
                    params: '',
                    success: function(form, action) {
                        var info = action.result.info;
                        if (action.result.success) {
                            this.store.reload();
                            if (action.result.method == "Create") {
                                this.form.getForm().reset();
                            } else {
                                this.dlg.hide();
                            }
                        }
                        Ext.Msg.aler