1.导包(共42个)
- spring(包括springmvc)
- mybatis
- mybatis-spring整合包
- 数据库驱动
- 第三方连接池。
- Json依赖包Jackson
2.整合思路
Dao层:
- SqlMapConfig.xml,空文件即可,但是需要文件头和别名。
- applicationContext-dao.xml
a)数据库连接Druid(高级一点的连接数据库的什么?不清楚)(dubbo是什么)
b)SqlSessionFactory对象,需要spring和mybatis整合包下的。
c)配置mapper文件扫描器。Mapper动态代理开发 增强版
Service层:
- applicationContext-service.xml包扫描器,扫描@service注解的类。
- applicationContext-trans.xml配置事务。
暂时不用(晚点写)
Controller层:
- Springmvc.xml
a)包扫描器,扫描@Controller注解的类。
b)配置注解驱动
c)配置视图解析器,并对静态资源方形(因为在web.xml的前端控制器配置中配置了‘/’来拦截,会拦截所有除了jsp,所以要对静态资源放行,不然显示不出效果来)
Web.xml文件:
- 配置spring监听器
- 配置前端控制器。
- 配置过滤器,解决post的乱码问题
3.分页标签(有点高级)
- 在前端页面
- tld是标签的配置,里面有个配置文件,它导入到WEB-IN目录下面,utils是Java类导入src即可
- 前端页面的数据循环当中放入这行代码,之后配置相应的跳转页面即可(这个链接是所在表单提交的链接)
大概就是这样,具体使用百度吧。
4.使用ajax查询(修改)(删除)数据
- 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>
- 后端-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";
}
- 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>