SSM整合(2.0)

SSM整合(2.0)

Spring MVC整合MyBatis

1,需要在web.xml中文件里面追加Servlet配置;
<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationContext.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.action</url-pattern> 
	</servlet-mapping>
2,在applicationContext.xml文件里面定义spring mvc 的相关处理;
	<mvc:annotation-driven/>
	<mvc:default-servlet-handler/>
3,定义IMessageDAO接口
package cn.mldn.dao;
import java.util.List;
import cn.mldn.vo.Message;
public interface IMessageDAO {
	public boolean doCreate(Message vo) throws Exception;

	public List<Message> findAllSplit(String column, String keyWord,
			Integer currentPage, Integer lineSize) throws Exception;
} 
定义其子类:
package cn.mldn.dao.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Component;
import cn.mldn.dao.IMessageDAO;
import cn.mldn.vo.Message;
@Component
public class MessageDAOImpl implements IMessageDAO {
	@Resource
	private SqlSessionFactory sessionFactory;
	@Override
	public boolean doCreate(Message vo) throws Exception {
		return this.sessionFactory.openSession().insert(
				"cn.mldn.mapping.MessageNS.doCreate", vo) > 0;
	}
	@Override
	public List<Message> findAllSplit(String column, String keyWord,
			Integer currentPage, Integer lineSize) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if (!"".equals(keyWord)) { // 现在不是空
			map.put("keyWord", "%" + keyWord + "%");
		}
		map.put("column", column);
		map.put("start", (currentPage - 1) * lineSize);
		map.put("lineSize", lineSize);
		return this.sessionFactory.openSession().selectList(
				"cn.mldn.mapping.MessageNS.findAllSplit", map);
	}
}
4,定义IMessageService接口
package cn.mldn.service;
import java.util.List;
import cn.mldn.vo.Message;
public interface IMessageService {
	public boolean add(Message vo) throws Exception;
	public List<Message> list(String column, String keyWord, int currentPage,
			int lineSize) throws Exception;
}
定义其子类:
package cn.mldn.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.mldn.dao.IMessageDAO;
import cn.mldn.service.IMessageService;
import cn.mldn.vo.Message;
@Service
public class MessageServiceImpl implements IMessageService {
	@Resource
	private IMessageDAO messageDAO;
	@Override
	public boolean add(Message vo) throws Exception {
		return this.messageDAO.doCreate(vo);
	}
	@Override
	public List<Message> list(String column, String keyWord, int currentPage,
			int lineSize) throws Exception {
		return this.messageDAO.findAllSplit(column, keyWord, currentPage,
				lineSize);
	}
}
5,定义MessageAction程序类进行处理。
package cn.mldn.action;
import java.text.SimpleDateFormat;
import javax.annotation.Resource;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import cn.mldn.service.IMessageService;
import cn.mldn.vo.Message;
@Controller
@RequestMapping("/pages/back/msg/*")
public class MessageAction {
	@Resource
	private IMessageService messageService;
	@RequestMapping("add") 
	public ModelAndView add(Message msg) {
		try {
			System.out.println(this.messageService.add(msg));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@RequestMapping("list")
	public ModelAndView list(String col, String kw, int cp, int ls) {
		try {
			System.out.println(this.messageService.list(col, kw, cp, ls));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@InitBinder
	public void initBindler(WebDataBinder binder) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(
				sdf, true));
	}
}

数据增加:
在这里插入图片描述
数据查询:
在这里插入图片描述
文档结构图:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值