SSM框架整合应用

1.按照how2j网站上的步骤编写代码,

问题解决方案1:applicationContext.xml中需要改密码(看自己的密码是不是root,如果是,需要把admin改为root,或者直接按照网站步骤改密码)

 <property name="password">  
          <value>root</value>  

问题解决方案2:代码全队但出现404错误,解压源码则会发现com.how2java.test包和MybatisTest文件还未创建,这个步骤网站上没有,需要自己完成

问题解决方案3:如果已经创建但还是出现404错误,则把log4j这个properties文件复制粘贴到自己的ssm文件夹中,再次刷新运行。

问题解决方案4:运行jsp文件时,导航为http://localhost:8080/ssm/WEB-INF/jsp/listCategory.jsp

需要改为http://localhost:8080/ssm/listCategory.jsp才能运行出来

问题解决方案5:如果所有代码都没问题,但是dynamic web project图标左下角出现×,可能是jar包导入有问题,在java build path删除再重新导入即可

二、1.SSM分页(category_表分页)

按照how2j网站编写代码

 2.SSM分页(Product_表分页)

package com.how2java.service;
 
import java.util.List;
 
import com.how2java.pojo.Category;
import com.how2java.util.Page;
 
public interface CategoryService {
 
    List<Category> list();
    int total();
    List<Category> list(Page page);
 
}
package com.how2java.service.impl;
 
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.how2java.mapper.CategoryMapper;
import com.how2java.pojo.Category;
import com.how2java.service.CategoryService;
import com.how2java.util.Page;
 
@Service
public class CategoryServiceImpl  implements CategoryService{
    @Autowired
    CategoryMapper categoryMapper;
     
    public List<Category> list(){
        return categoryMapper.list();
    }
 
    @Override
    public List<Category> list(Page page) {
        // TODO Auto-generated method stub
        return categoryMapper.list(page);
    }
 
    @Override
    public int total() {
        return categoryMapper.total();
    };
 
}
package com.how2java.controller;
 
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
import com.how2java.pojo.Category;
import com.how2java.service.CategoryService;
import com.how2java.util.Page;
 
// 告诉spring mvc这是一个控制器类
@Controller
@RequestMapping("")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
 
    @RequestMapping("listCategory")
    public ModelAndView listCategory(Page page){
     
        ModelAndView mav = new ModelAndView();
        List<Category> cs= categoryService.list(page);
        int total = categoryService.total();
         
        page.caculateLast(total);
         
        // 放入转发参数
        mav.addObject("cs", cs);
        // 放入jsp路径
        mav.setViewName("listCategory");
        return mav;
    }
 
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.*"%>
  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  
 <div style="width:500px;margin:0px auto;text-align:center">
    <table align='center' border='1' cellspacing='0'>
        <tr>
            <td>id</td>
            <td>name</td>
            <td>price</td>
            <td>cid</td>
        </tr>
        <c:forEach items="${cs}" var="c" varStatus="st">
            <tr>
                <td>${c.id}</td>
                <td>${c.name}</td>
                <td>${c.price}</td>
                <td>${c.cid}</td>
            </tr>
        </c:forEach>
    </table>
    <div style="text-align:center">
        <a href="?start=0">首  页</a>
        <a href="?start=${page.start-page.count}">上一页</a>
        <a href="?start=${page.start+page.count}">下一页</a>
        <a href="?start=${page.last}">末  页</a>
    </div>
 </div>
package com.how2java.test;
 
import java.util.List;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
import com.how2java.mapper.CategoryMapper;
import com.how2java.pojo.Category;
import com.how2java.util.Page;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class MybatisTest {
 
    @Autowired
    private CategoryMapper categoryMapper;
 
//  @Test
    public void testAdd() {
        for (int i = 0; i < 100; i++) {
            Category category = new Category();
            category.setName("new Category");
            categoryMapper.add(category);
        }
 
    }
     
    @Test
    public void testTotal() {
        int total = categoryMapper.total();
        System.out.println(total);
    }
 
    @Test
    public void testList() {
        Page p = new Page();
        p.setStart(2);
        p.setCount(3);
        List<Category> cs=categoryMapper.list(p);
        for (Category c : cs) {
            System.out.println(c.getName());
        }
    }
 
}
package com.how2java.pojo;
 
public class Category {
    private int id;
    private String name;
    private String price;
    private int cid;
    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 String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    
    public int getCid() {
        return cid;
    }
    public void setCid(int cid) {
        this.cid = cid;
    }
    @Override
    public String toString() {
        return "Category [id=" + id + ", name=" + name + ", price=" + price + ", cid=" + cid + "]";
    }
     
}

问题解决方案1:Category.xml中主要更改category_,改为product_

问题解决方案2:修改category.java,新增price和cid属性

三、SSM CRUD

按照how2j网站上成功运行出category_

 

 

自己再尝试运行product_

发现没有数据,还在找原因。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值