ssm_crud
环境配置
-
新建maven的web项目
-
配置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 -->
-
-
配置
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)。
- 配置
springmvc-servlet.xml
文件 - 配置
applicationContext.xml
- 利用
mybatis generator
逆向生成与数据库对应的JavaBean、mapper接口、mapper映射文件。- 另外需要在pom.xml文件中引入
mybatis-generator
jar包。 - 根据官方配置文件(利用注解生成器的相关属性,抑制生成注解…)。
- 如果需要,对生成的文件进行相应的增加。
- 另外需要在pom.xml文件中引入
- 在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 查询
-
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; }
-
-
服务器将查询到的数据以json的形式返回给浏览器
-
浏览器根据收到的json数据,利用js进行解析。(通过dom Document Object Model 增删改改变页面)
create 创建
-
点击新增按钮,弹出新增对话框,同时发送ajax请求查出部门信息至对话框。
-
数据校验
- 数据格式校验:jquery前端校验,后端(JSR303)校验。
- 数据重复校验: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(); } }
-
数据保存。
update 更新
-
单击新增按钮,弹出更新对话框,同时发送ajax请求查询部门信息至对话。
//单击加载修改模态框 //on()方法绑定事件:按钮是通过jquery之后添加,不能直接使用.click()点击事件 $(document).on("click", ".update_btn", function(){});
-
查询部门信息的同时,查询对应的表单信息,将服务器查到的数据,显示在对话框中。
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]); } }); }
-
更新数据并查询至当前页。
delete 删除
- 单击删除按钮,弹出验证框。找的员工名称显示在验证框中。
- 验证框点击确定,发送删除的ajax请求。
- 删除结束后,查询当前页,刷新。
补充知识
注解方面
- @RequestParam (请求参数)如:emp?page=5
- @PathVariable(路径变量)如:/emp/5
jquery
查找
:find 搜索所有与指定表达式匹配的元素。parent 取得一个包含着所有匹配元素的唯一父元素的元素集合。属性
:text 取得所有匹配元素的内容,val 取得元素的当前值。- attr 设置或返回被选元素的属性值,一般是自定义的属性,prop一般是dom的原生的属性。
选择器
:#id 根据给定的id匹配一个元素,element 根据给定的元素匹配所有的元素,.class 根据给定的class类匹配对应的元素,* 匹配所有元素。