这篇文章用SSM框架整合来完成一个全查商品表(items),并将所有的商品信息展示到页面(showitems.jsp)中这样的功能,让大家快速熟练使用SSM框架。
因为spring框架功能强大,涉及到整个web分层,所以这次的整合以spring框架为基础,mybatis负责dao层,spring mvc 负责controller层
最终项目文件结构如下图:
新建web工程,导入SSM框架项目需要的jar包。另外需要在数据库(我这边使用mysql数据库)创建items表,添加数据,数据库表如下图:
log4j.properties:
# Global logging configuration
# developer-->DEBUG product-->INFO or ERROR
log4j.rootLogger=DEBUG, stdout
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
db.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
public class Items {
private int id;
private String name;
private double price;
private String detail;
private Date createtime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
@Override
public String toString() {
return "Items [id=" + id + ", name=" + name + ", price=" + price
+ ", detail=" + detail + ", createtime=" + createtime + "]";
}
}
public interface ItemsMapper {
// 全查商品表
public List findAllItem();
}
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
ItemsMapper itemsMapper = (ItemsMapper)ac.getBean("itemsMapper");
List items = itemsMapper.findAllItem();
for(Items item: items){
System.out.println(item);
}
}
执行结果:
出现上述结果,说明整个dao层已经写好,接下来到service层
ItemsService类:
@Service // 相当于配置了一个标识符为ItemsService类型首字母小写的bean
public class ItemsService {
// mapper扫描器会自动生成一个标识符为itemsMapper的bean
// 注解@Autowired 可以将生成的bean赋值给全局变量itemsMapper
@Autowired
private ItemsMapper itemsMapper;
// 全查items表
public List findAllItem(){
return itemsMapper.findAllItem();
}
}
@Controller
public class ItemsController{
@Autowired
private ItemsService itemsService;
@RequestMapping("/queryItems1.action")
public ModelAndView queryItems1(){ // 方法名可以任意
List items = itemsService.findAllItem();
ModelAndView mav = new ModelAndView();
// 添加数据
mav.addObject("ITEMS", items);
// 设置jsp页面路径
mav.setViewName("jsp/showitems.jsp");
return mav;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
查询商品列表
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
springmvc
*.action
contextConfigLocation
classpath:spring.xml
org.springframework.web.context.ContextLoaderListener
整个配置完成,进行最终的测试:
运行项目,在showitems.jsp页面点击查询显示数据库表中的所有商品信息
http://localhost:8080/ssm/jsp/showitems.jsp