1、用MyEclipse创建一个WEB工程
2、下载mybatis的jar包和连接mysql的jar包(我使用的是mybatis-3.1.1.jar,mysql-connector-java-5.1.25-bin.jar)
3、将下载的2个jar包拷贝到工程WebRoot-->WEB-INF-->lib目录下
4、在工程src目录下,配置conf.xml文件
5、根据数据库的每一个表,在bean(entity)包下建立对应的实体
package com.se7en.bean;
import java.io.Serializable;
public class DepartmentInfo implements Serializable {
private int companyId;
private int departmentId;
private String departmentName;
private String departmentDesc;
public int getCompanyId() {
return companyId;
}
public void setCompanyId(int companyId) {
this.companyId = companyId;
}
public int getDepartmentId() {
return departmentId;
}
public void setDepartmentId(int departmentId) {
this.departmentId = departmentId;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public String getDepartmentDesc() {
return departmentDesc;
}
public void setDepartmentDesc(String departmentDesc) {
this.departmentDesc = departmentDesc;
}
}
6、最好新建一个mapping包,建立对应实体的mapper.xml文件,我是直接在bean包下新建的,别忘了在第4步的conf.xml文件中进行注册
insert into
t_department(dep_com_id,dep_name,dep_desc)
values(#{companyId},#{departmentName},#{departmentDesc})
delete from t_department where
dep_id=#{departmentId}
update t_department set
dep_com_id=#{companyId},dep_name=#{departmentName},dep_desc=#{departmentDesc}
where dep_id=#{departmentId}
7、在dao层的实现类进行调用
package com.se7en.dao.impl;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.se7en.bean.DepartmentInfo;
import com.se7en.dao.DepartmentDaoIf;
import com.se7en.utils.MyBatisUtil;
public class DepartmentDaoImpl implements DepartmentDaoIf {
public DepartmentInfo getDepById(int depId) {
SqlSession session = null;
try{
session= MyBatisUtil.getFactory().openSession();
String statement = "com.se7en.bean.departmentMapper.getDepById";
DepartmentInfo info = session.selectOne(statement,depId);
session.commit();
return info;
}catch(Exception e){
e.printStackTrace();
session.rollback();
}finally{
if(session != null){
session.close();
}
}
return null;
}
public List getDepByComId(int comId) {
SqlSession session = null;
try{
session= MyBatisUtil.getFactory().openSession();
String statement = "com.se7en.bean.departmentMapper.getDepByComId";
List list = session.selectList(statement,comId);
session.commit();
return list;
}catch(Exception e){
e.printStackTrace();
session.rollback();
}finally{
if(session != null){
session.close();
}
}
return null;
}
public boolean init(int comId) {
DepartmentInfo info = new DepartmentInfo();
info.setCompanyId(comId);
info.setDepartmentName("未命名");
info.setDepartmentDesc("无");
return add(info);
}
public boolean add(DepartmentInfo info) {
SqlSession session = null;
try{
session= MyBatisUtil.getFactory().openSession();
String statement = "com.se7en.bean.departmentMapper.add";
session.insert(statement, info);
session.commit();
return true;
}catch(Exception e){
e.printStackTrace();
session.rollback();
}finally{
if(session != null){
session.close();
}
}
return false;
}
public boolean del(int depId) {
SqlSession session = null;
try{
session= MyBatisUtil.getFactory().openSession();
String statement = "com.se7en.bean.departmentMapper.del";
session.delete(statement, depId);
session.commit();
return true;
}catch(Exception e){
e.printStackTrace();
session.rollback();
}finally{
if(session != null){
session.close();
}
}
return false;
}
public boolean edit(DepartmentInfo info) {
SqlSession session = null;
try{
session= MyBatisUtil.getFactory().openSession();
String statement = "com.se7en.bean.departmentMapper.edit";
session.update(statement, info);
session.commit();
return true;
}catch(Exception e){
e.printStackTrace();
session.rollback();
}finally{
if(session != null){
session.close();
}
}
return false;
}
}
8、附上MyBatisUtil.java
package com.se7en.utils;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtil {
//定义方法
public static SqlSessionFactory getFactory(){
String resource = "conf.xml";
//读取配置文件,为了解决项目在发布以后的路径带来的问题我们可以去使用反射机制来封装
InputStream is = MyBatisUtil.class.getClassLoader().getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
return factory;
}
}
9、附上servlet,记得在WebRoot-->WEB-INF目录下的web.xml进行注册(直接在工程中右键新建Servlet可以自动注册,复制的话可能忘了注册)
package com.se7en.action.manage;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.se7en.bean.DepartmentInfo;
import com.se7en.dao.impl.DepartmentDaoImpl;
import com.se7en.service.DepartmentServiceIf;
import com.se7en.service.impl.DepartmentServiceImpl;
import com.se7en.utils.Constants;
/**
* 部门管理
*
* @author Se7en
*
*/
public class Department extends HttpServlet {
private DepartmentServiceIf sv = new DepartmentServiceImpl();
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
// 获取远程地址
String addr = request.getRemoteAddr();
System.out.println(addr + "部门管理");
String str = "";
/** 0 增加、1 删除、2 修改 */
int actionType = Integer.parseInt(request.getParameter(Constants.ACTIONTYPE));
switch (actionType) {
case 0:
str = add(request);
break;
case 1:
str = del(request);
break;
case 2:
str = edit(request);
break;
default:
break;
}
PrintWriter out = response.getWriter();
System.out.println(str);
out.write(str);
out.close();
}
private String add(HttpServletRequest request) {
int companyId = Integer.parseInt(request.getParameter(Constants.COMPANYID));
String departmentName = request.getParameter(Constants.DEPARTMENTNAME);
String departmentDesc = request.getParameter(Constants.DEPARTMENTDESC);
DepartmentInfo dep = new DepartmentInfo();
dep.setCompanyId(companyId);
dep.setDepartmentName(departmentName);
dep.setDepartmentDesc(departmentDesc);
String res = sv.add(dep);
return res;
}
private String del(HttpServletRequest request) {
int departmentId = Integer.parseInt(request.getParameter(Constants.DEPARTMENTID));
String res = sv.del(departmentId);
return res;
}
private String edit(HttpServletRequest request) {
int departmentId = Integer.parseInt(request.getParameter(Constants.DEPARTMENTID));
String departmentName = request.getParameter(Constants.DEPARTMENTNAME);
String departmentDesc = request.getParameter(Constants.DEPARTMENTDESC);
DepartmentInfo info = new DepartmentDaoImpl().getDepById(departmentId);
info.setDepartmentName(departmentName);
info.setDepartmentDesc(departmentDesc);
String res = sv.edit(info);
return res;
}
}
10、附上service层的实现类
package com.se7en.service.impl;
import java.util.List;
import net.sf.json.JSONObject;
import com.se7en.bean.DepartmentInfo;
import com.se7en.entity.DepRes;
import com.se7en.service.DepartmentServiceIf;
import com.se7en.utils.BaseService;
import com.se7en.utils.Result;
public class DepartmentServiceImpl extends BaseService implements DepartmentServiceIf{
public String add(DepartmentInfo info) {
Result res = new Result();
boolean b = departmentDao.add(info);
if(b){
res.setSuccess(true);
res.setMessage("增加部门成功");
}else{
res.setSuccess(false);
res.setMessage("未知错误,请联系管理员");
}
return JSONObject.fromObject(res).toString();
}
public String del(int depId) {
Result res = new Result();
boolean b = departmentDao.del(depId);
if(b){
res.setSuccess(true);
res.setMessage("删除部门成功");
}else{
res.setSuccess(false);
res.setMessage("未知错误,请联系管理员");
}
return JSONObject.fromObject(res).toString();
}
public String edit(DepartmentInfo info) {
Result res = new Result();
boolean b = departmentDao.edit(info);
if(b){
res.setSuccess(true);
res.setMessage("修改部门成功");
}else{
res.setSuccess(false);
res.setMessage("未知错误,请联系管理员");
}
return JSONObject.fromObject(res).toString();
}
public String get(int comId) {
List list = departmentDao.getDepByComId(comId);
if(list==null){
Result res = new Result();
res.setSuccess(false);
res.setMessage("未知错误,请联系管理员");
return JSONObject.fromObject(res).toString();
}else{
DepRes res = new DepRes();
res.setSuccess(true);
res.setMessage("获取部门成功");
res.setDepartment(list);
return JSONObject.fromObject(res).toString();
}
}
}
以上是使用Tomcat+MySQL+MyBatis搭建的一个简易服务端框架,大概流程是这样的:
1、安卓端使用HTTP协议发送请求
2、在servlet下进行接收请求并做出响应
3、servlet调用service层的接口以及实现类,返回json字符串
4、service层的实现类调用dao层的接口以及实现类,对数据库进行CRUD操作
5、dao层的实现类使用mybatis框架,与数据库进行数据交互