通过这么长时间的学习,终于见到了效果,确实有点乐不思蜀。虽然下面还有很多东西要学习,但是也要为自己的第一篇整合篇写下一些留念。
数据库:Oracle xe10g
表 : 客户信息表
场景描述:实现查询所有的客户信息;
框架:Spring + Springmvc + Mybatis
Spring的作用 :
表现层:springMvc
业务层:service接口
持久层:mybatis
数据库:Oracle / Mysql
Spring将各层进行整合:
1) 通过Spring管理持久层的mapping(相当于dao接口)
2) 通过Spring管理业务层Service,Service中可以调用Mapping接口;可以进行事务控制(AOP)
3) 通过spring 管理表现层Handler,Handler中可以调用Service接口;
mapper,service,handler都是javabean ;
(1)创建客户信息表
CREATE TABLE F_CLIENT(
ID NUMBER(12) PRIMARY KEY,--用户编号
USERNAME VARCHAR2(20) NOT NULL,--用户姓名
CLIENT_CERTIFICATE_NO VARCHAR2(20) NOT NULL UNIQUE,--证件号码
BORN_DATE DATE,--出生日期
FAMILY_REGISTER_ADDRESS VARCHAR2(200),--家庭住址
NOW_ADDRESS VARCHAR2(200) NOT NULL,--现在住址
CONTACT_MODE VARCHAR2(50) NOT NULL,--联系方式
URGENCY_CONTACT_MODE VARCHAR2(50) NOT NULL,--紧急联系方式
CREATE_DATE DATE NOT NULL--创建时间
);
insert into f_client(id,username,client_certificate_no,born_date,family_register_address,now_address,contact_mode,urgency_contact_mode,create_date) values (14,
'yuan','311209070127',to_date('1993-03-12','yyyy-mm-dd'),'河南省焦作市','河南省河南理工大学','150000000','110',sysdate);
insert into f_client(id,username,client_certificate_no,born_date,family_register_address,now_address,contact_mode,urgency_contact_mode,create_date) values(
15,'yang','311209070126',to_date('1993-04-12','yyyy-mm-dd'),'河南温县','河南理工大学','3987321','110',sysdate);
insert into f_client values(
16,'yang','311209070129',to_date('1997-04-12','yyyy-mm-dd'),'河南新乡','河南理工大学','3987321','110',sysdate);
insert into F_CLIENT (ID, USERNAME, CLIENT_CERTIFICATE_NO, BORN_DATE, FAMILY_REGISTER_ADDRESS, NOW_ADDRESS, CONTACT_MODE, URGENCY_CONTACT_MODE, CREATE_DATE)
values (1, 'sawyer', '1593224056', to_date('10-10-1980', 'dd-mm-yyyy'), '北京市海淀区东北旺', 'peaking', '123456789', '987654321', to_date('15-12-2015 08:47:13', 'dd-mm-yyyy hh24:mi:ss'));
insert into F_CLIENT (ID, USERNAME, CLIENT_CERTIFICATE_NO, BORN_DATE, FAMILY_REGISTER_ADDRESS, NOW_ADDRESS, CONTACT_MODE, URGENCY_CONTACT_MODE, CREATE_DATE)
values (2, 'yangly', '1593224057', to_date('11-10-1980', 'dd-mm-yyyy'), '北京市海淀区东北旺', 'peaking', '123456789', '987654321', to_date('15-12-2015 08:47:13', 'dd-mm-yyyy hh24:mi:ss'));
insert into F_CLIENT (ID, USERNAME, CLIENT_CERTIFICATE_NO, BORN_DATE, FAMILY_REGISTER_ADDRESS, NOW_ADDRESS, CONTACT_MODE, URGENCY_CONTACT_MODE, CREATE_DATE)
values (3, 'gaozhy', '1593224058', to_date('12-12-1980', 'dd-mm-yyyy'), '湖北省洪山区黄家湖', 'shanghai', '123456789', '987654321', to_date('15-12-2015 08:47:13', 'dd-mm-yyyy hh24:mi:ss'));
insert into F_CLIENT (ID, USERNAME, CLIENT_CERTIFICATE_NO, BORN_DATE, FAMILY_REGISTER_ADDRESS, NOW_ADDRESS, CONTACT_MODE, URGENCY_CONTACT_MODE, CREATE_DATE)
values (4, 'wangmj', '1593224059', to_date('13-04-1980', 'dd-mm-yyyy'), '湖北省洪山区黄家湖', 'hubei', '123456789', '987654321', to_date('15-12-2015 08:47:13', 'dd-mm-yyyy hh24:mi:ss'));
insert into F_CLIENT (ID, USERNAME, CLIENT_CERTIFICATE_NO, BORN_DATE, FAMILY_REGISTER_ADDRESS, NOW_ADDRESS, CONTACT_MODE, URGENCY_CONTACT_MODE, CREATE_DATE)
values (5, 'lijing', '1593224060', to_date('14-07-1980', 'dd-mm-yyyy'), '湖北省洪山区黄家湖', 'tianjing', '123456789', '987654321', to_date('15-12-2015 08:47:13', 'dd-mm-yyyy hh24:mi:ss'));
insert into F_CLIENT (ID, USERNAME, CLIENT_CERTIFICATE_NO, BORN_DATE, FAMILY_REGISTER_ADDRESS, NOW_ADDRESS, CONTACT_MODE, URGENCY_CONTACT_MODE, CREATE_DATE)
values (6, 'wangpl', '1593224061', to_date('15-09-1980', 'dd-mm-yyyy'), '北京市朝阳区百子湾', 'peaking', '123456789', '987654321', to_date('15-12-2015 08:47:13', 'dd-mm-yyyy hh24:mi:ss'));
insert into F_CLIENT (ID, USERNAME, CLIENT_CERTIFICATE_NO, BORN_DATE, FAMILY_REGISTER_ADDRESS, NOW_ADDRESS, CONTACT_MODE, URGENCY_CONTACT_MODE, CREATE_DATE)
values (7, 'lixiao', '1593224062', to_date('16-09-1980', 'dd-mm-yyyy'), '北京市朝阳区百子湾', 'henan', '123456789', '987654321', to_date('15-12-2015 08:47:13', 'dd-mm-yyyy hh24:mi:ss'));
insert into F_CLIENT (ID, USERNAME, CLIENT_CERTIFICATE_NO, BORN_DATE, FAMILY_REGISTER_ADDRESS, NOW_ADDRESS, CONTACT_MODE, URGENCY_CONTACT_MODE, CREATE_DATE)
values (8, 'zhaoyf', '1593224063', to_date('17-07-1980', 'dd-mm-yyyy'), '北京市海淀区五道口', 'henan', '123456789', '987654321', to_date('15-12-2015 08:47:13', 'dd-mm-yyyy hh24:mi:ss'));
insert into F_CLIENT (ID, USERNAME, CLIENT_CERTIFICATE_NO, BORN_DATE, FAMILY_REGISTER_ADDRESS, NOW_ADDRESS, CONTACT_MODE, URGENCY_CONTACT_MODE, CREATE_DATE)
values (9, 'suyibo', '1593224064', to_date('18-09-1980', 'dd-mm-yyyy'), '北京市海淀区东北旺', 'hebei', '123456789', '987654321', to_date('15-12-2015 08:47:13', 'dd-mm-yyyy hh24:mi:ss'));
insert into F_CLIENT (ID, USERNAME, CLIENT_CERTIFICATE_NO, BORN_DATE, FAMILY_REGISTER_ADDRESS, NOW_ADDRESS, CONTACT_MODE, URGENCY_CONTACT_MODE, CREATE_DATE)
values (10, 'luojh', '1593224065', to_date('19-09-1980', 'dd-mm-yyyy'), '北京市朝阳区百子湾', 'heilongjiang', '123456789', '987654321', to_date('15-12-2015 08:47:13', 'dd-mm-yyyy hh24:mi:ss'));
insert into F_CLIENT (ID, USERNAME, CLIENT_CERTIFICATE_NO, BORN_DATE, FAMILY_REGISTER_ADDRESS, NOW_ADDRESS, CONTACT_MODE, URGENCY_CONTACT_MODE, CREATE_DATE)
values (11, 'mazhb', '1593224066', to_date('10-08-1980', 'dd-mm-yyyy'), '北京市海淀区东北旺', 'peaking', '123456789', '987654321', to_date('15-12-2015 08:47:13', 'dd-mm-yyyy hh24:mi:ss'));
insert into F_CLIENT (ID, USERNAME, CLIENT_CERTIFICATE_NO, BORN_DATE, FAMILY_REGISTER_ADDRESS, NOW_ADDRESS, CONTACT_MODE, URGENCY_CONTACT_MODE, CREATE_DATE)
values (12, 'gaohf', '1593224067', to_date('18-08-1980', 'dd-mm-yyyy'), '北京市朝阳区百子湾', 'peaking', '123456789', '987654321', to_date('15-12-2015 08:47:13', 'dd-mm-yyyy hh24:mi:ss'));
insert into F_CLIENT (ID, USERNAME, CLIENT_CERTIFICATE_NO, BORN_DATE, FAMILY_REGISTER_ADDRESS, NOW_ADDRESS, CONTACT_MODE, URGENCY_CONTACT_MODE, CREATE_DATE)
values (13, 'chenb', '1593224068', to_date('13-08-1980', 'dd-mm-yyyy'), '北京市海淀区西二旗', 'peaking', '123456789', '987654321', to_date('15-12-2015 08:47:13', 'dd-mm-yyyy hh24:mi:ss'));
(1)Mapper.java实现
/**
* 客户信息Dao
* TODO
* 作者:原明卓
* 时间:2016年1月5日 上午9:03:16
* 工程:SpringMvcMybatis1Demo
*/
public interface FClientMapper {
//根据id查询用户信息
FClient findClientById(int id) throws Exception;
//获得所有用户信息
List findClientList() throws Exception;
//更新新客户信息
int updateClientIfo(FClient fc) throws Exception;
//删除客户信息
int deleteClientIfo(int id) throws Exception;
//新增客户信息
int insertClientIfo(FClient fc) throws Exception;
}
update
f_client
username=#{username},
client_certificate_no=#{client_certificate_no},
born_date=#{born_date},
family_register_address=#{family_register_address},
now_address=#{now_address},
contact_mode=#{contact_mode},
urgency_contact_mode=#{urgency_contact_mode}
id=#{id}
delete from f_client where id=#{id}
insert into
f_client
values(
#{id},
#{username},
#{client_certificate_no},
#{born_date},
#{family_register_address},
#{now_address},
#{contact_mode},
#{urgency_contact_mode},
sysdate)
(4)Mybatis与Spring整合 :实现ApplicationContext-dao.xml
jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc\:oracle\:thin\:@localhost\:1521\:xe
jdbc.name=hr
jdbc.pass=hr
(1)Service接口
public interface FClientService {
//根据id查询用户信息
FClient findClientById(int id) throws Exception;
//获得所有用户信息
List findClientList() throws Exception;
//更新新客户信息
String updateClientIfo(FClient fc) throws Exception;
//删除客户信息
String deleteClientIfo(int id) throws Exception;
//新增客户信息
String insertClientIfo(FClient fc) throws Exception;
}
(2)Service 接口实现
/**
* 接口实现
* TODO
* 作者:原明卓
* 时间:2016年1月5日 上午9:05:20
* 工程:SpringMvcMybatis1Demo
*/
public class FClientServiceImpl implements FClientService{
//Mapper
@Autowired
private FClientMapper fcmapper;
@Override
public FClient findClientById(int id) throws Exception {
return fcmapper.findClientById(id);
}
@Override
public List findClientList() throws Exception {
return fcmapper.findClientList();
}
@Override
public String updateClientIfo(FClient fc) throws Exception {
return fcmapper.updateClientIfo(fc)>0?"success":"failed";
}
@Override
public String deleteClientIfo(int id) throws Exception {
return fcmapper.deleteClientIfo(id)>0?"success":"failed";
}
@Override
public String insertClientIfo(FClient fc) throws Exception {
return fcmapper.insertClientIfo(fc)>0?"success":"failed";
}
}
(4)Service中使用Spring进行事务控制 :ApplicationContext-transcation.xml
(1)Handler实现
package cn.labelnet.ssm.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 cn.labelnet.ssm.po.FClient;
import cn.labelnet.ssm.service.FClientService;
/**
* TODO 作者:原明卓 时间:2016年1月5日 下午3:23:11 工程:SpringMvcMybatis1Demo
*/
@Controller
public class FClientController {
@Autowired
private FClientService fcService;
@RequestMapping("/clientslist")
public ModelAndView clientsList(){
ModelAndView view=new ModelAndView();
List list;
try {
list = fcService.findClientList();
view.addObject("clients",list);
view.setViewName("clientindex.jsp");
} catch (Exception e) {
view.addObject("error",e.getMessage());
view.setViewName("error.jsp");
}
return view;
}
}
加载Spring容器采用通配符的方式实现,使得可以拓展;
contextConfigLocation
/WEB-INF/classes/spring/applicationContext-*.xml
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springmvc.xml
springmvc
*.action
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
客户信息表
客户信息管理
客户信息管理
编号
姓名
代码
生日
家庭住址
现居住地
联系方式
紧急联系方式
注册日期
操作
${c.id}
${c.username}
${c.client_certificate_no}
${c.born_date}
${c.family_register_address}
${c.now_address}
${c.contact_mode}
${c.urgency_contact_mode}
${c.create_date}
查看
测试的话,访问Handler中的RequestMapping("") 中的url地址 ,比如 :
http://localhost:8989/SpringMvcMybatis1Demo/clientslist.action
首先是Mybatis的实现,在实现的时候,每做一个方法,测试一个方法,以保证正确性;其次在配置SpringMvc和加载Spring容器的时候,采用通配符的方式实现,以保证拓展性;最后,MVC中实现Handler的时候,采用注解的方式,方便开发,一个类中可以写多个方法实现;
在这个例子 中,还有很多功能需要完善,比如查询,分页,修改,删除,都是需要实现的;如果类比Asp.net MVC的话,那么Handler的返回值,参数等都需要学习;
http://download.csdn.net/detail/lablenet/9392008