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));
}
}
数据增加:
数据查询:
文档结构图: