java学习——ssm_crud项目学习笔记

4 篇文章 0 订阅
2 篇文章 0 订阅

ssm_crud


环境配置

  1. 新建maven的web项目

  2. 配置maven核心配置文件pom.xml

    • <!-- spring 相关 --> 3<!-- spring-web mvc --> <!-- spring aspect --> <!-- spring jdbc -->
      
    • <!-- mybatis 相关 --> 3<!-- mybatis --> <!-- c3p0 --> <!-- mybatis-connector-java -->
      
    • <!-- mybatis 与 spring 整合--> 1<!-- mybatis-spring -->
      
    • <!-- javaee 相关--> 2<!-- jstl --> <!-- servlet-api -->
      
    • <!-- 测试工具--> 2<!-- junit --> <!-- spring-text -->
      
  3. 配置web.xml文件

    • 1、启动spring容器 
      -->将spring核心配置文件放在类路径下:classpath:applicationContext.xml
      
    • 2、启动springmvc的前端控制器,拦截所有请求
      -->使用默认的springmvc配置文件</servlet-name>-servlet.xml	 
      
    • 3、字符集编码过滤器 
      --> (CharacterEncodingFilter)
      
    • 4、实现restful风格的过滤器
      -->HiddenHttpMethodFilter
      -->增加对除GET、POST之外请求的数据获取 -->FormContentFilter
      

    <url-pattern>/</url-pattern>会匹配到/springmvc这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url;

<url-pattern>/*</url-pattern>会匹配所有的url:路径型的和后缀型的url(包括/springmvc,.jsp,.js和*.html)。

  1. 配置springmvc-servlet.xml文件
  2. 配置applicationContext.xml
  3. 利用mybatis generator逆向生成与数据库对应的JavaBean、mapper接口、mapper映射文件。
    • 另外需要在pom.xml文件中引入mybatis-generatorjar包。
    • 根据官方配置文件(利用注解生成器的相关属性,抑制生成注解…)。
    • 如果需要,对生成的文件进行相应的增加。
  4. 在jsp文件中引入bootstrap
    • 查看官方文档

spring测试单元

package com.ys.crue.test;

import org.apache.ibatis.session.SqlSession;
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;
/**
 * 
 * 测试mybatis中dao层
 * 
 * 推荐spring的项目可以使用spring的测试单元,可以自动注入我们需要的组件。(而不需要使用mybatis的方法)
 * 
 * 1、导入spring-test模块对应的的jar
 * 2、利用ContentConfiguration 指定spring配置文件的位置
 * 3、Runwith,把所有的test交由SpringJUnit4ClassRunner.class处理
 * 4、直接注入mapper接口
 *
 *
 */

import com.ys.crud.bean.Employee;
import com.ys.crud.dao.DepartmentMapper;
import com.ys.crud.dao.EmployeeMapper;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/resources/applicationContext.xml")
public class MybatisMapperTest {
	
	@Autowired
	DepartmentMapper departmentMapper;
	@Autowired
	EmployeeMapper employeeMapper;
	@Autowired
	SqlSession sqlSession;

	@Test
	public void testCRUD() {
//		departmentMapper.insertSelective(new Department(null, "开发部"));
//		departmentMapper.insertSelective(new Department(null, "测试部"));
		
		Employee employee = employeeMapper.selectByPrimaryKeyWithDept(1);
		System.out.println(employee);
		
//		EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
//		for (int i = 0; i < 500; i++) {
//			String substring = UUID.randomUUID().toString().substring(0, 5);
//			mapper.insertSelective(new Employee(null, substring, "F", substring + "@ys.con", 1));
//		}
//		
	}
}

crud

retrieve 查询

  1. index.jsp页面直接发送ajax请求,进行ajax请求

    • 分页查询

      在pom.xml文件中引入<!-- pagehelper -->jar包
      
      <!-- 在mybatis分页插件的配置 -->
      <plugin interceptor="com.github.pagehelper.PageInterceptor">
         <property name="reasonable" value="true"/>
      </plugin>
      
    • 使用jquery的ajax形式

      $(function(){
          url,
          type,
          data,
          function(){
              
          }
      });
      
    • 以json的形式返回数据

      在pom.xml文件中引入<!-- jackson -->jar包
      
      @Responsebody 注解,表示以json的形式返回数据
      
    • 新建通用的返回类型

      利用包装模式的思想,返回包含状态、提示信息以及add()添加方法。

      public Msg add(String string, Object info) {
      		this.extend.put(string, info);
      		return this;
      	}
      
  2. 服务器将查询到的数据以json的形式返回给浏览器

  3. 浏览器根据收到的json数据,利用js进行解析。(通过dom Document Object Model 增删改改变页面)

create 创建

  1. 点击新增按钮,弹出新增对话框,同时发送ajax请求查出部门信息至对话框。

  2. 数据校验

    1. 数据格式校验:jquery前端校验,后端(JSR303)校验。
    2. 数据重复校验:jquery前端校验,数据库添加唯一约束。
    在pom.xml文件中引入<!-- hibernate-validator -->
    
    在javabean中需要校验的属性前添加注解
    @Pattern(regexp = "(^[a-z0-9_-]{3,16}$)|(^[\\u2E80-\\u9FFF]{2,5}$)", message = "用户名必须是3-16位的数字和字母组合,或者3-5位的中文")
    private String empName;
    
    @ResponseBody
    @RequestMapping(value = "/empsAjax", method = RequestMethod.POST)
    public Msg saveEmp(@Valid Employee employee, BindingResult result) {
    
        if(result.hasErrors()) {
            Map<String,Object> map = new HashMap<String, Object>();
            List<FieldError> errors = result.getFieldErrors();
            for (FieldError fieldError : errors) {
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return Msg.fail().add("fieldError", map);
    
        }else {
            employeeServiceImp.savaEmps(employee);
            return Msg.success();
        }
    
    }
    
  3. 数据保存。

update 更新

  1. 单击新增按钮,弹出更新对话框,同时发送ajax请求查询部门信息至对话。

    //单击加载修改模态框
    //on()方法绑定事件:按钮是通过jquery之后添加,不能直接使用.click()点击事件
    $(document).on("click", ".update_btn", function(){});
    
  2. 查询部门信息的同时,查询对应的表单信息,将服务器查到的数据,显示在对话框中。

    function getEmp(id){
        $.ajax({
            url:"${APP_PATH}/empAjax/" + id,
            type:"GET",
            success:function(message){
                var employee = message.extend.emp;
                //text 取得所有匹配元素的内容
                $("#update_static").text(employee.empName);
                //val 取得元素的当前值
                $("#update_email").val(employee.email);
                //单选框、下拉列表赋值
                $("#emp_update_modal input[name=gender]").val([employee.gender]);
                $("#emp_update_modal select").val([employee.cId]);
            }
        });
    }
    
  3. 更新数据并查询至当前页。

delete 删除

  1. 单击删除按钮,弹出验证框。找的员工名称显示在验证框中。
  2. 验证框点击确定,发送删除的ajax请求。
  3. 删除结束后,查询当前页,刷新。

补充知识

注解方面

  • @RequestParam (请求参数)如:emp?page=5
  • @PathVariable(路径变量)如:/emp/5

jquery

  • 查找:find 搜索所有与指定表达式匹配的元素。parent 取得一个包含着所有匹配元素的唯一父元素的元素集合。
  • 属性:text 取得所有匹配元素的内容,val 取得元素的当前值。
  • attr 设置或返回被选元素的属性值,一般是自定义的属性,prop一般是dom的原生的属性。
  • 选择器:#id 根据给定的id匹配一个元素,element 根据给定的元素匹配所有的元素,.class 根据给定的class类匹配对应的元素,* 匹配所有元素。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值