Mybatis、SpringMVC练习(第一次练习)(后台系统)

1.导包(共42个)

  1. spring(包括springmvc)
  2. mybatis
  3. mybatis-spring整合包
  4. 数据库驱动
  5. 第三方连接池。
  6. Json依赖包Jackson
    在这里插入图片描述

2.整合思路

Dao层:

  1. SqlMapConfig.xml,空文件即可,但是需要文件头和别名。
    在这里插入图片描述
  2. applicationContext-dao.xml
    a)数据库连接Druid(高级一点的连接数据库的什么?不清楚)(dubbo是什么)
    在这里插入图片描述
    b)SqlSessionFactory对象,需要spring和mybatis整合包下的。
    在这里插入图片描述
    c)配置mapper文件扫描器。Mapper动态代理开发 增强版
    在这里插入图片描述

Service层:

  1. applicationContext-service.xml包扫描器,扫描@service注解的类。
    在这里插入图片描述
  2. applicationContext-trans.xml配置事务。
    暂时不用(晚点写)

Controller层:

  1. Springmvc.xml
    a)包扫描器,扫描@Controller注解的类。
    在这里插入图片描述
    b)配置注解驱动
    在这里插入图片描述
    c)配置视图解析器,并对静态资源方形(因为在web.xml的前端控制器配置中配置了‘/’来拦截,会拦截所有除了jsp,所以要对静态资源放行,不然显示不出效果来)
    在这里插入图片描述

Web.xml文件:

  1. 配置spring监听器
    在这里插入图片描述
  2. 配置前端控制器。
    在这里插入图片描述
  3. 配置过滤器,解决post的乱码问题在这里插入图片描述

3.分页标签(有点高级)

  1. 在前端页面
    在这里插入图片描述
  2. tld是标签的配置,里面有个配置文件,它导入到WEB-IN目录下面,utils是Java类导入src即可
    在这里插入图片描述
  3. 前端页面的数据循环当中放入这行代码,之后配置相应的跳转页面即可(这个链接是所在表单提交的链接)
    在这里插入图片描述

大概就是这样,具体使用百度吧。

4.使用ajax查询(修改)(删除)数据

  1. js代码
	<script type="text/javascript">
		function editCustomer(id) {//查询数据
			$.ajax({//这个是通过id查询数据,并返回数据回显到窗口页面
				type:"get",//提交方法
				url:"<%=basePath%>customer/edit.action",//提交路径
				data:{"id":id},//提交的数据(id)
				success:function(data) {  //返回的数据(data)
					$("#edit_cust_id").val(data.cust_id);//把返回的数据从data中取出,并给相应的表单项赋值
					$("#edit_customerName").val(data.cust_name);
					$("#edit_customerFrom").val(data.cust_source)
					$("#edit_custIndustry").val(data.cust_industry)
					$("#edit_custLevel").val(data.cust_level)
					$("#edit_linkMan").val(data.cust_linkman);
					$("#edit_phone").val(data.cust_phone);
					$("#edit_mobile").val(data.cust_mobile);
					$("#edit_zipcode").val(data.cust_zipcode);
					$("#edit_address").val(data.cust_address);
					
				}
			});
		}
		function updateCustomer() {//修改数据
		//下面的serialize()会把选中的form标签中所有表单值序列化,序列化的值可在生成AJAX请求时用于URL查询字符串中
			$.post("<%=basePath%>customer/update.action",
				$("#edit_customer_form").serialize(),//提交的数据
				function(data){//后端要传一个“OK”给data,它才会进入这个函数,继续运行,“OK”是已经定义好的,死的
					alert("客户信息更新成功!");//返回提示数据
					window.location.reload();//刷新页面的方法
				});
		}
		
		function deleteCustomer(id) {//删除数据
			if(confirm('确实要删除该客户吗?')) {//confirm() 方法用于显示一个带有指定消息和OK及取消按钮的对话框
				$.post("<%=basePath%>customer/delete.action",
				{"id":id},//提交的数据
				function(data){//返回的数据(跟上面用法差不多)
					alert("客户删除更新成功!");
					window.location.reload();
				});
			}
		}
	</script>
  1. 后端-controller层
	//去修改页面(通过ID查询客户)
	@RequestMapping(value = "/customer/edit.action")
	public @ResponseBody
	Customer edit(Integer id){
		return customerService.selectCustomerById(id);
	}
	//修改保存(修改客户通过ID)
	@RequestMapping(value = "/customer/update.action")
	public @ResponseBody
	String update(Customer customer){
		//修改
		customerService.updateCustomerById(customer);
		return "OK";//返回OK(前端的提示信息)
	}
	//删除(通过ID 删除客户)
	@RequestMapping(value = "/customer/delete.action")
	public @ResponseBody
	String delete(Integer id){
		//删除
		customerService.deleteCustomerById(id);
		return "OK";
	}
  1. Dao层的sql代码
<!-- 		//通过ID查询客户 -->
<!-- 	public Customer selectCustomerById(Integer id); -->
	<select id="selectCustomerById" parameterType="Integer"
		resultType="Customer">
		select * from customer
		<where>
			cust_id = #{id}
		</where>
	</select>
	
<!-- 		//修改客户通过ID -->
<!-- 	public void updateCustomerById(Customer customer); -->
	<update id="updateCustomerById" parameterType="Customer">
		update customer 
		<set>
			<if test="cust_name != null">
				cust_name = #{cust_name},
			</if>
			<if test="cust_linkman != null">
				cust_linkman = #{cust_linkman}
			</if>
		</set>
		<where>
			cust_id = #{cust_id}
		</where>
	</update>
		
<!-- 	//通过ID 删除客户 -->
<!-- 	public void deleteCustomerById(Integer id); -->
		<delete id="deleteCustomerById" parameterType="Integer">
			delete from customer where cust_id = #{value}
		</delete>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值