ssm框架+easyui實現 增刪改查功能

spring版本:4.0.6

mybatis版本:3.2.5

所有jar包打包下載:http://pan.baidu.com/s/1qLEaU

1、項目目錄結構

技術分享

其中,controller包下存放控制層文件,dao下存放各個model類相關的數據庫操作接口,entity下放置各種model類,mappers下放置各個dao對應的映射文件,service服務層就不說了,放置各種service接口,impl是其具體實現類。

2、web.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>    
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- Spring配置文件 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <!-- 編碼過濾器 -->
  <filter>
      <filter-name>encodingFilter</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      <async-supported>true</async-supported>
      <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>encodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 配置Spring監聽器 -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 添加對SpringMVC的支持 -->
  <servlet>
      <servlet-name>SpringMVC</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring-mvc.xml</param-value>
      </init-param>
  </servlet>
  <servlet-mapping>
      <servlet-name>SpringMVC</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
</web-app>

 

3、log4j.properties配置日誌

log4j.rootLogger=DEBUG, Console  
  
#Console  
log4j.appender.Console=org.apache.log4j.ConsoleAppender  
log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n  
  
log4j.logger.java.sql.ResultSet=INFO  
log4j.logger.org.apache=INFO  
log4j.logger.java.sql.Connection=DEBUG  
log4j.logger.java.sql.Statement=DEBUG  
log4j.logger.java.sql.PreparedStatement=DEBUG  

 

4、mybatis-config.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!-- 別名 -->
    <typeAliases>
        <package name="com.java.entity" />    <!-- 後面書寫類全名時默認加該包名前綴如:<resultMap type="User"... -->
    </typeAliases>

</configuration>

 

5、spring-mvc.xml配置

<?xml version="1.0" encoding="UTF-8"?>    
<beans xmlns="http://www.springframework.org/schema/beans"    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    
    
    <!-- 使用註解的包,包括子集 -->
    <context:component-scan base-package="com.java.controller"></context:component-scan>
    
    <!-- 視圖解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>  

 

6、applicationContext.xml配置

<?xml version="1.0" encoding="UTF-8"?>    
<beans xmlns="http://www.springframework.org/schema/beans"    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    
        
    <!-- 自動掃描該包下面的bean文件 -->
    <context:component-scan base-package="com.java.service"></context:component-scan>
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!-- <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/> -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/crm"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    
    <!-- 配置MyBatis的sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>        
        <property name="mapperLocations" value="classpath:com/java/mappers/*.xml"></property>
    </bean>
    
    <!-- Mapper接口所在包名,Spring會自動查找其下的Mapper  -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.java.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    
    <!-- 配置transactionManager事務管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 配置事物通知屬性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- 定義事物傳播特性 -->
        <tx:attributes>
            <tx:method name="insert" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="edit*" propagation="REQUIRED" />  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="add*" propagation="REQUIRED" />  
            <tx:method name="new*" propagation="REQUIRED" />  
            <tx:method name="set*" propagation="REQUIRED" />  
            <tx:method name="remove*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="change*" propagation="REQUIRED" />  
            <tx:method name="check*" propagation="REQUIRED" />  
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="*" propagation="REQUIRED" read-only="true" />  
        </tx:attributes>
    </tx:advice>
    
    <!-- 配置事物切面 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.java.service.*.*(..))" id="serviceOperation"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
    </aop:config>
    
</beans>

 

7、增刪改查具體實現

主界面很簡單:

技術分享

詳細html實現:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP ‘userManage.jsp‘ starting page</title>
<link rel="stylesheet" type="text/css"
    href="${pageContext.request.contextPath}/jquery-easyui-1.3.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
    href="${pageContext.request.contextPath}/jquery-easyui-1.3.3/themes/icon.css">
<script type="text/javascript"
    src="${pageContext.request.contextPath}/jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript"
    src="${pageContext.request.contextPath}/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript"
    src="${pageContext.request.contextPath}/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>

<script type="text/javascript">
    var url;
    function searchUser() {
        $("#dg").datagrid(load, {
            "userName" : $("#s_userName").val()
        });
    }
    function openUserAddDialog() {
        $("#dlg").dialog("open").dialog("setTitle", "添加用戶信息");
        url = "${pageContext.request.contextPath}/user/save.do";
    }

    function openUserModifyDialog() {
        var selectedRows = $("#dg").datagrid("getSelections");
        if (selectedRows.length != 1) {
            $.messager.alert("系統提示", "請選擇一條要編輯的數據!");
            return;
        }
        var row = selectedRows[0];
        $("#dlg").dialog("open").dialog("setTitle", "編輯用戶信息");
        $("#fm").form("load", row);
        url = "${pageContext.request.contextPath}/user/save.do?id=" + row.id;
    }
    
    function saveUser() {
        $("#fm").form("submit", {
            url : url,
            onSubmit : function() {
                if ($("#roleName").combobox("getValue") == "") {
                    $.messager.alert("系統提示", "請選擇用戶角色!");
                    return false;
                }
                return $(this).form("validate");
            },
            success : function(result) {
                var result = eval(( + result + ));
                if (result.success) {
                    $.messager.alert("系統提示", "保存成功!");
                    resetValue();
                    $("#dlg").dialog("close");
                    $("#dg").datagrid("reload");
                } else {
                    $.messager.alert("系統提示", "保存失敗!");
                    return;
                }
            }
        });
    }

    function resetValue() {
        $("#userName").val("");
        $("#password").val("");
        $("#trueName").val("");
        $("#email").val("");
        $("#phone").val("");
        $("#roleName").combobox("setValue", "");
    }

    function closeUserDialog() {
        $("#dlg").dialog("close");
        resetValue();
    }

    function deleteUser() {
        var selectedRows = $("#dg").datagrid("getSelections");
        if (selectedRows.length == 0) {
            $.messager.alert("系統提示", "請選擇要刪除的數據!");
            return;
        }
        var strIds = [];
        for ( var i = 0; i < selectedRows.length; i++) {
            strIds.push(selectedRows[i].id);
        }
        var ids = strIds.join(",");
        $.messager.confirm("系統提示", "您確定要刪除這<font color=red>"
                + selectedRows.length + "</font>條數據嗎?", function(r) {
            if (r) {
                $.post("${pageContext.request.contextPath}/user/delete.do", {
                    ids : ids
                }, function(result) {
                    if (result.success) {
                        $.messager.alert("系統提示", "數據已成功刪除!");
                        $("#dg").datagrid("reload");
                    } else {
                        $.messager.alert("系統提示", "數據刪除失敗,請聯繫系統管理員!");
                    }
                }, "json");
            }
        });
    }
</script>
</head>

<body style="margin: 1px">

    <table id="dg" title="用戶管理" class="easyui-datagrid" fitColumns="true"
        pagination="true" rownumbers="true"
        url="${pageContext.request.contextPath}/user/list.do" fit="true"
        toolbar="#tb">
        <thead>
            <tr>
                <th field="cb" checkbox="true" align="center"></th>
                <th field="id" width="50" align="center">編號</th>
                <th field="userName" width="50" align="center">用戶名</th>
                <th field="password" width="50" align="center">密碼</th>
                <th field="trueName" width="50" align="center">真實姓名</th>
                <th field="email" width="50" align="center">郵件</th>
                <th field="phone" width="50" align="center">聯繫電話</th>
                <th field="roleName" width="50" align="center">角色</th>
            </tr>
        </thead>
    </table>
    <div id="tb">
        <a href="javascript:openUserAddDialog()" class="easyui-linkbutton"
            iconCls="icon-add" plain="true">添加</a> <a
            href="javascript:openUserModifyDialog()" class="easyui-linkbutton"
            iconCls="icon-edit" plain="true">修改</a> <a
            href="javascript:deleteUser()" class="easyui-linkbutton"
            iconCls="icon-remove" plain="true">刪除</a>
        <div>
            &nbsp;用戶名:&nbsp;<input type="text" id="s_userName" size="20"
                onkeydown="if(event.keyCode == 13)searchUser()" /> <a
                href="javascript:searchUser()" class="easyui-linkbutton"
                iconCls="icon-search" plain="true">查詢</a>
        </div>

        <div id="dlg-buttons">
            <a href="javascript:saveUser()" class="easyui-linkbutton"
                iconCls="icon-ok">保存</a> <a href="javascript:closeUserDialog()"
                class="easyui-linkbutton" iconCls="icon-cancel">關閉</a>
        </div>

        <div id="dlg" class="easyui-dialog"
            style="width: 730px;height:280px;padding:10px 10px;" closed="true"
            buttons="#dlg-buttons">
            <form method="post" id="fm">
                <table cellspacing="8px;">
                    <tr>
                        <td>用戶名:</td>
                        <td><input type="text" id="userName" name="userName"
                            class="easyui-validatebox" required="true" />&nbsp;<span
                            style="color: red">*</span>
                        </td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                        <td>密碼:</td>
                        <td><input type="password" id="password" name="password"
                            class="easyui-validatebox" required="true" />&nbsp;<span
                            style="color: red">*</span>
                        </td>
                    </tr>
                    <tr>
                        <td>真實姓名:</td>
                        <td><input type="text" id="trueName" name="trueName"
                            class="easyui-validatebox" required="true" />&nbsp;<span
                            style="color: red">*</span>
                        </td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                        <td>郵箱:</td>
                        <td><input type="text" id="email" name="email"
                            validType="email" class="easyui-validatebox" required="true" />&nbsp;<span
                            style="color: red">*</span>
                        </td>
                    </tr>
                    <tr>
                        <td>聯繫電話:</td>
                        <td><input type="text" id="phone" name="phone"
                            class="easyui-validatebox" required="true" />&nbsp;<span
                            style="color: red">*</span>
                        </td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                        <td>用戶角色:</td>
                        <td><select name="roleName" class="easyui-combobox"
                            id="roleName" style="width: 154px;" editable="false"
                            panelHeight="auto">
                                <option value="">請選擇角色</option>
                                <option value="系統管理員">系統管理員</option>
                                <option value="銷售主管">銷售主管</option>
                                <option value="客戶經理">客戶經理</option>
                                <option value="高管">高管</option>
                        </select> &nbsp;<span style="color: red">*</span>
                        </td>
                    </tr>
                </table>
            </form>
        </div>
</body>
</html>

 

再來看看目錄結構:

技術分享

UserController:

package com.java.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.java.entity.PageBean;
import com.java.entity.User;
import com.java.service.UserService;
import com.java.util.ResponseUtil;
import com.java.util.StringUtil;
import com.sun.mail.iap.Response;

/**
 * 用戶Controller層
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
    
    @Resource
    private UserService userService;
    
    /**
     * 刪除用戶
     * @param ids
     * @param res
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete")
    public String delete(@RequestParam(value="ids") String ids,HttpServletResponse res) throws Exception{
        String[] idStr = ids.split(",");
        JSONObject jsonObject = new JSONObject();
        for (String id : idStr) {
            userService.delete(Integer.parseInt(id));
        }
        jsonObject.put("success", true);
        ResponseUtil.write(res, jsonObject);
        return null;
    }
    
    /**
     * 添加或者修改
     * @param user
     * @param res
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    public String save(User user,HttpServletResponse res) throws Exception{
        //操作記錄條數,初始化爲0
        int resultTotal = 0;
        if (user.getId() == null) {
            resultTotal = userService.add(user);
        }else{
            resultTotal = userService.update(user);
        }
        JSONObject jsonObject = new JSONObject();
        if(resultTotal > 0){   //說明修改或添加成功
            jsonObject.put("success", true);
        }else{
            jsonObject.put("success", false);
        }
        ResponseUtil.write(res, jsonObject);
        return null;
    }
    
    /**
     * 用戶登錄
     * @param user
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request) throws Exception{
        User resultUser = userService.login(user);
        if (resultUser == null) {
            request.setAttribute("user", resultUser);
            request.setAttribute("errorMsg", "用戶名或密碼錯誤");
            return "login";
        }else {
            HttpSession session = request.getSession();
            session.setAttribute("currentUser", resultUser);
            return "redirect:/main.jsp";
        }
    }
    
    /**
     * 用戶分頁查詢
     * @param page
     * @param rows
     * @param s_user
     * @param res
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    public String list(@RequestParam(value="page",required=false) String page,@RequestParam(value="rows",required=false) String rows,User s_user,HttpServletResponse res) throws Exception{
        PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("userName", StringUtil.formatLike(s_user.getUserName()));
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        List<User> userList=userService.find(map);
        Long total=userService.getTotal(map);
        JSONObject result=new JSONObject();
        JSONArray jsonArray=JSONArray.fromObject(userList);
        result.put("rows", jsonArray);
        result.put("total", total);
        ResponseUtil.write(res, result);
        return null;
    }
    
}

 

UserDao:

package com.java.dao;

import java.util.List;
import java.util.Map;

import com.java.entity.User;

/**
 * 用戶DAO接口
 * 
 * @author Administrator
 * 
 */
public interface UserDao {
    
    /**
     * 用戶登錄
     * @param user
     * @return
     */
    public User login(User user);
    
    /**
     * 用戶查詢
     * @param map
     * @return用戶集合
     */
    public List<User> find(Map<String, Object> map);
    
    /**
     * 獲取總記錄數
     * @param map
     * @return獲取的total數
     */
    public Long getTotal(Map<String, Object> map);
    
    /**
     * 修改用戶
     * @param user
     * @return影響的記錄數
     */
    public int update(User user);
    
    /**
     * 添加用戶
     * @param user
     * @return影響的記錄數
     */
    public int add(User user);
    
    /**
     * 刪除用戶
     * @param id
     * @return
     */
    public int delete(Integer id);
    
    
}

 

PageBean,用來分頁的一個工具類:

package com.java.entity;

/**
 * 分頁Model類
 * @author 
 *
 */
public class PageBean {

    private int page; // 第幾頁
    private int pageSize; // 每頁記錄數
    private int start;  // 起始頁
    
    public PageBean(int page, int pageSize) {
        super();
        this.page = page;
        this.pageSize = pageSize;
    }
    
    public int getPage() {
        return page;
    }
    public void setPage(int page) {
        this.page = page;
    }
    
    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getStart() {
        return (page-1)*pageSize;
    }
    
    
}

 

User,用戶實體類:

package com.java.entity;

/**
 * 用戶實體
 * @author Administrator
 *
 */
public class User {
    private Integer id;
    private String userName;
    private String password;
    private String trueName;
    private String email;
    private String phone;
    private String roleName;    //角色名稱:系統管理員、銷售主管、客戶經理、高管
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getTrueName() {
        return trueName;
    }
    public void setTrueName(String trueName) {
        this.trueName = trueName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getRoleName() {
        return roleName;
    }
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
    
}

 

UserService:

package com.java.service;

import java.util.List;
import java.util.Map;

import com.java.entity.User;

/**
 * UserService接口
 * @author Administrator
 *
 */
public interface UserService {
    
    /**
     * 用戶登錄
     * @param user
     * @return
     */
    public User login(User user);
    /**
     * 用戶查詢
     * @param map
     * @return
     */
    public List<User> find(Map<String, Object> map);
    
    /**
     * 獲取總記錄數
     * @param map
     * @return
     */
    public Long getTotal(Map<String, Object> map);
    
    /**
     * 修改用戶
     * @param user
     * @return影響的記錄數
     */
    public int update(User user);
    
    /**
     * 添加用戶
     * @param user
     * @return影響的記錄數
     */
    public int add(User user);
    
    /**
     * 刪除用戶
     * @param id
     * @return
     */
    public int delete(Integer id);
}

 

UserServiceImpl:

package com.java.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.java.dao.UserDao;
import com.java.entity.User;
import com.java.service.UserService;

/**
 * UserService實現類
 * @author Administrator
 *
 */
@Service("userService")
public class UserServiceImpl implements UserService{
    
    @Resource
    private UserDao userDao;
    
    @Override
    public User login(User user) {
        return userDao.login(user);
    }
    
    @Override
    public List<User> find(Map<String, Object> map) {
        return userDao.find(map);
    }
    
    @Override
    public Long getTotal(Map<String, Object> map) {
        return userDao.getTotal(map);
    }

    @Override
    public int update(User user) {
        return userDao.update(user);
    }

    @Override
    public int add(User user) {
        return userDao.add(user);
    }

    @Override
    public int delete(Integer id) {
        return userDao.delete(id);
    }
}

 

ResponseUtil工具類,主要用於返回json數據:

package com.java.util;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;


public class ResponseUtil {

    public static void write(HttpServletResponse response,Object object)throws Exception{
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        out.println(object);
        out.flush();
        out.close();
    }
}

 

StringUtil,字符串工具類:

package com.java.util;

/**
 * 字符串工具類
 * @author 
 *
 */
public class StringUtil {

    /**
     * 判斷是否是空
     * @param str
     * @return
     */
    public static boolean isEmpty(String str){
        if(str==null||"".equals(str.trim())){
            return true;
        }else{
            return false;
        }
    }
    
    /**
     * 判斷是否不是空
     * @param str
     * @return
     */
    public static boolean isNotEmpty(String str){
        if((str!=null)&&!"".equals(str.trim())){
            return true;
        }else{
            return false;
        }
    }
    
    /**
     * 格式化模糊查詢
     * @param str
     * @return
     */
    public static String formatLike(String str){
        if(isNotEmpty(str)){
            return "%"+str+"%";
        }else{
            return null;
        }
    }
}

 

UserMapper.xml,mybatis所謂的動態sql語句,全部封裝在這裏:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.java.dao.UserDao">
    
    <!-- 先定義一個Interface,UserMapper,然後Mapper文件的namespace指向的就是這個Interface, UserInterface裏定義的方法和UserMapper.xml文件中的一樣, 
        然後代碼裏直接使用接口 -->
        
    <resultMap type="User" id="UserResult">
        <result property="id" column="id" />
        <result property="userName" column="userName" />
        <result property="password" column="password" />
        <result property="trueName" column="trueName" />
        <result property="email" column="email" />
        <result property="phone" column="phone" />
        <result property="roleName" column="roleName" />
    </resultMap>
    
    <!-- 所謂的動態SQL語句 -->
    <select id="login" parameterType="User" resultMap="UserResult">
        select * from userInfo where userName = #{userName} and password = #{password} and roleName = #{roleName}
    </select>
    
    <!-- 用戶查詢 -->
    <select id="find" parameterType="Map" resultMap="UserResult">
        select * from userInfo
        <where>
            <if test="userName!=null and userName!=‘‘ ">
                and userName like #{userName}
            </if>
        </where>
        <if test="start!=null and size!=null">
            limit #{start},#{size}
        </if>
    </select>
    <select id="getTotal" parameterType="Map" resultType="Long">
        select count(*) from userInfo
        <where>
            <if test="userName!=null and userName!=‘‘ ">
                and userName like #{userName}
            </if>
        </where>
    </select>
    
    <!-- 用戶添加 -->
    <insert id="add" parameterType="User">
        insert into userInfo values(null,#{userName},#{password},#{trueName},#{email},#{phone},#{roleName})
    </insert>
    
    <!-- 用戶修改 -->
    <update id="update" parameterType="User">
        update userInfo
        <set>
            <if test="userName != null and userName != ‘‘">userName = #{userName},</if>
            <if test="password != null and password != ‘‘">password = #{password},</if>
            <if test="trueName != null and trueName != ‘‘">trueName = #{trueName},</if>
            <if test="email != null and email != ‘‘">email = #{email},</if>
            <if test="phone != null and phone != ‘‘">phone = #{phone},</if>
            <if test="roleName != null and roleName != ‘‘">roleName = #{roleName},</if>
        </set>
        where id = #{id}
    </update>
    
    <!-- 用戶刪除 -->
    <delete id="delete" parameterType="Integer">
        delete from userInfo where id = #{id}
    </delete>
</mapper>

 

最後是數據表結構:

技術分享

技術分享

技術分享

技術分享

 

相比hibernate,mybatis輕量級很多,但是新手最容易也最難搞定的往往是環境搭建部分,一不留神就是各種jar包衝突,真叫人抓狂啊啊,以後還得留意呵。

 

spring版本:4.0.6

mybatis版本:3.2.5

所有jar包打包下載:http://pan.baidu.com/s/1qLEaU

1、項目目錄結構

技術分享

其中,controller包下存放控制層文件,dao下存放各個model類相關的數據庫操作接口,entity下放置各種model類,mappers下放置各個dao對應的映射文件,service服務層就不說了,放置各種service接口,impl是其具體實現類。

2、web.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>    
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- Spring配置文件 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <!-- 編碼過濾器 -->
  <filter>
      <filter-name>encodingFilter</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      <async-supported>true</async-supported>
      <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>encodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 配置Spring監聽器 -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 添加對SpringMVC的支持 -->
  <servlet>
      <servlet-name>SpringMVC</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring-mvc.xml</param-value>
      </init-param>
  </servlet>
  <servlet-mapping>
      <servlet-name>SpringMVC</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
</web-app>

 

3、log4j.properties配置日誌

log4j.rootLogger=DEBUG, Console  
  
#Console  
log4j.appender.Console=org.apache.log4j.ConsoleAppender  
log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n  
  
log4j.logger.java.sql.ResultSet=INFO  
log4j.logger.org.apache=INFO  
log4j.logger.java.sql.Connection=DEBUG  
log4j.logger.java.sql.Statement=DEBUG  
log4j.logger.java.sql.PreparedStatement=DEBUG  

 

4、mybatis-config.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!-- 別名 -->
    <typeAliases>
        <package name="com.java.entity" />    <!-- 後面書寫類全名時默認加該包名前綴如:<resultMap type="User"... -->
    </typeAliases>

</configuration>

 

5、spring-mvc.xml配置

<?xml version="1.0" encoding="UTF-8"?>    
<beans xmlns="http://www.springframework.org/schema/beans"    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    
    
    <!-- 使用註解的包,包括子集 -->
    <context:component-scan base-package="com.java.controller"></context:component-scan>
    
    <!-- 視圖解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>  

 

6、applicationContext.xml配置

<?xml version="1.0" encoding="UTF-8"?>    
<beans xmlns="http://www.springframework.org/schema/beans"    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    
        
    <!-- 自動掃描該包下面的bean文件 -->
    <context:component-scan base-package="com.java.service"></context:component-scan>
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!-- <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/> -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/crm"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    
    <!-- 配置MyBatis的sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>        
        <property name="mapperLocations" value="classpath:com/java/mappers/*.xml"></property>
    </bean>
    
    <!-- Mapper接口所在包名,Spring會自動查找其下的Mapper  -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.java.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    
    <!-- 配置transactionManager事務管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 配置事物通知屬性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- 定義事物傳播特性 -->
        <tx:attributes>
            <tx:method name="insert" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="edit*" propagation="REQUIRED" />  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="add*" propagation="REQUIRED" />  
            <tx:method name="new*" propagation="REQUIRED" />  
            <tx:method name="set*" propagation="REQUIRED" />  
            <tx:method name="remove*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="change*" propagation="REQUIRED" />  
            <tx:method name="check*" propagation="REQUIRED" />  
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="*" propagation="REQUIRED" read-only="true" />  
        </tx:attributes>
    </tx:advice>
    
    <!-- 配置事物切面 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.java.service.*.*(..))" id="serviceOperation"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
    </aop:config>
    
</beans>

 

7、增刪改查具體實現

主界面很簡單:

技術分享

詳細html實現:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP ‘userManage.jsp‘ starting page</title>
<link rel="stylesheet" type="text/css"
    href="${pageContext.request.contextPath}/jquery-easyui-1.3.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
    href="${pageContext.request.contextPath}/jquery-easyui-1.3.3/themes/icon.css">
<script type="text/javascript"
    src="${pageContext.request.contextPath}/jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript"
    src="${pageContext.request.contextPath}/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript"
    src="${pageContext.request.contextPath}/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>

<script type="text/javascript">
    var url;
    function searchUser() {
        $("#dg").datagrid(load, {
            "userName" : $("#s_userName").val()
        });
    }
    function openUserAddDialog() {
        $("#dlg").dialog("open").dialog("setTitle", "添加用戶信息");
        url = "${pageContext.request.contextPath}/user/save.do";
    }

    function openUserModifyDialog() {
        var selectedRows = $("#dg").datagrid("getSelections");
        if (selectedRows.length != 1) {
            $.messager.alert("系統提示", "請選擇一條要編輯的數據!");
            return;
        }
        var row = selectedRows[0];
        $("#dlg").dialog("open").dialog("setTitle", "編輯用戶信息");
        $("#fm").form("load", row);
        url = "${pageContext.request.contextPath}/user/save.do?id=" + row.id;
    }
    
    function saveUser() {
        $("#fm").form("submit", {
            url : url,
            onSubmit : function() {
                if ($("#roleName").combobox("getValue") == "") {
                    $.messager.alert("系統提示", "請選擇用戶角色!");
                    return false;
                }
                return $(this).form("validate");
            },
            success : function(result) {
                var result = eval(( + result + ));
                if (result.success) {
                    $.messager.alert("系統提示", "保存成功!");
                    resetValue();
                    $("#dlg").dialog("close");
                    $("#dg").datagrid("reload");
                } else {
                    $.messager.alert("系統提示", "保存失敗!");
                    return;
                }
            }
        });
    }

    function resetValue() {
        $("#userName").val("");
        $("#password").val("");
        $("#trueName").val("");
        $("#email").val("");
        $("#phone").val("");
        $("#roleName").combobox("setValue", "");
    }

    function closeUserDialog() {
        $("#dlg").dialog("close");
        resetValue();
    }

    function deleteUser() {
        var selectedRows = $("#dg").datagrid("getSelections");
        if (selectedRows.length == 0) {
            $.messager.alert("系統提示", "請選擇要刪除的數據!");
            return;
        }
        var strIds = [];
        for ( var i = 0; i < selectedRows.length; i++) {
            strIds.push(selectedRows[i].id);
        }
        var ids = strIds.join(",");
        $.messager.confirm("系統提示", "您確定要刪除這<font color=red>"
                + selectedRows.length + "</font>條數據嗎?", function(r) {
            if (r) {
                $.post("${pageContext.request.contextPath}/user/delete.do", {
                    ids : ids
                }, function(result) {
                    if (result.success) {
                        $.messager.alert("系統提示", "數據已成功刪除!");
                        $("#dg").datagrid("reload");
                    } else {
                        $.messager.alert("系統提示", "數據刪除失敗,請聯繫系統管理員!");
                    }
                }, "json");
            }
        });
    }
</script>
</head>

<body style="margin: 1px">

    <table id="dg" title="用戶管理" class="easyui-datagrid" fitColumns="true"
        pagination="true" rownumbers="true"
        url="${pageContext.request.contextPath}/user/list.do" fit="true"
        toolbar="#tb">
        <thead>
            <tr>
                <th field="cb" checkbox="true" align="center"></th>
                <th field="id" width="50" align="center">編號</th>
                <th field="userName" width="50" align="center">用戶名</th>
                <th field="password" width="50" align="center">密碼</th>
                <th field="trueName" width="50" align="center">真實姓名</th>
                <th field="email" width="50" align="center">郵件</th>
                <th field="phone" width="50" align="center">聯繫電話</th>
                <th field="roleName" width="50" align="center">角色</th>
            </tr>
        </thead>
    </table>
    <div id="tb">
        <a href="javascript:openUserAddDialog()" class="easyui-linkbutton"
            iconCls="icon-add" plain="true">添加</a> <a
            href="javascript:openUserModifyDialog()" class="easyui-linkbutton"
            iconCls="icon-edit" plain="true">修改</a> <a
            href="javascript:deleteUser()" class="easyui-linkbutton"
            iconCls="icon-remove" plain="true">刪除</a>
        <div>
            &nbsp;用戶名:&nbsp;<input type="text" id="s_userName" size="20"
                onkeydown="if(event.keyCode == 13)searchUser()" /> <a
                href="javascript:searchUser()" class="easyui-linkbutton"
                iconCls="icon-search" plain="true">查詢</a>
        </div>

        <div id="dlg-buttons">
            <a href="javascript:saveUser()" class="easyui-linkbutton"
                iconCls="icon-ok">保存</a> <a href="javascript:closeUserDialog()"
                class="easyui-linkbutton" iconCls="icon-cancel">關閉</a>
        </div>

        <div id="dlg" class="easyui-dialog"
            style="width: 730px;height:280px;padding:10px 10px;" closed="true"
            buttons="#dlg-buttons">
            <form method="post" id="fm">
                <table cellspacing="8px;">
                    <tr>
                        <td>用戶名:</td>
                        <td><input type="text" id="userName" name="userName"
                            class="easyui-validatebox" required="true" />&nbsp;<span
                            style="color: red">*</span>
                        </td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                        <td>密碼:</td>
                        <td><input type="password" id="password" name="password"
                            class="easyui-validatebox" required="true" />&nbsp;<span
                            style="color: red">*</span>
                        </td>
                    </tr>
                    <tr>
                        <td>真實姓名:</td>
                        <td><input type="text" id="trueName" name="trueName"
                            class="easyui-validatebox" required="true" />&nbsp;<span
                            style="color: red">*</span>
                        </td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                        <td>郵箱:</td>
                        <td><input type="text" id="email" name="email"
                            validType="email" class="easyui-validatebox" required="true" />&nbsp;<span
                            style="color: red">*</span>
                        </td>
                    </tr>
                    <tr>
                        <td>聯繫電話:</td>
                        <td><input type="text" id="phone" name="phone"
                            class="easyui-validatebox" required="true" />&nbsp;<span
                            style="color: red">*</span>
                        </td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                        <td>用戶角色:</td>
                        <td><select name="roleName" class="easyui-combobox"
                            id="roleName" style="width: 154px;" editable="false"
                            panelHeight="auto">
                                <option value="">請選擇角色</option>
                                <option value="系統管理員">系統管理員</option>
                                <option value="銷售主管">銷售主管</option>
                                <option value="客戶經理">客戶經理</option>
                                <option value="高管">高管</option>
                        </select> &nbsp;<span style="color: red">*</span>
                        </td>
                    </tr>
                </table>
            </form>
        </div>
</body>
</html>

 

再來看看目錄結構:

技術分享

UserController:

package com.java.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.java.entity.PageBean;
import com.java.entity.User;
import com.java.service.UserService;
import com.java.util.ResponseUtil;
import com.java.util.StringUtil;
import com.sun.mail.iap.Response;

/**
 * 用戶Controller層
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
    
    @Resource
    private UserService userService;
    
    /**
     * 刪除用戶
     * @param ids
     * @param res
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete")
    public String delete(@RequestParam(value="ids") String ids,HttpServletResponse res) throws Exception{
        String[] idStr = ids.split(",");
        JSONObject jsonObject = new JSONObject();
        for (String id : idStr) {
            userService.delete(Integer.parseInt(id));
        }
        jsonObject.put("success", true);
        ResponseUtil.write(res, jsonObject);
        return null;
    }
    
    /**
     * 添加或者修改
     * @param user
     * @param res
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    public String save(User user,HttpServletResponse res) throws Exception{
        //操作記錄條數,初始化爲0
        int resultTotal = 0;
        if (user.getId() == null) {
            resultTotal = userService.add(user);
        }else{
            resultTotal = userService.update(user);
        }
        JSONObject jsonObject = new JSONObject();
        if(resultTotal > 0){   //說明修改或添加成功
            jsonObject.put("success", true);
        }else{
            jsonObject.put("success", false);
        }
        ResponseUtil.write(res, jsonObject);
        return null;
    }
    
    /**
     * 用戶登錄
     * @param user
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request) throws Exception{
        User resultUser = userService.login(user);
        if (resultUser == null) {
            request.setAttribute("user", resultUser);
            request.setAttribute("errorMsg", "用戶名或密碼錯誤");
            return "login";
        }else {
            HttpSession session = request.getSession();
            session.setAttribute("currentUser", resultUser);
            return "redirect:/main.jsp";
        }
    }
    
    /**
     * 用戶分頁查詢
     * @param page
     * @param rows
     * @param s_user
     * @param res
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    public String list(@RequestParam(value="page",required=false) String page,@RequestParam(value="rows",required=false) String rows,User s_user,HttpServletResponse res) throws Exception{
        PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("userName", StringUtil.formatLike(s_user.getUserName()));
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        List<User> userList=userService.find(map);
        Long total=userService.getTotal(map);
        JSONObject result=new JSONObject();
        JSONArray jsonArray=JSONArray.fromObject(userList);
        result.put("rows", jsonArray);
        result.put("total", total);
        ResponseUtil.write(res, result);
        return null;
    }
    
}

 

UserDao:

package com.java.dao;

import java.util.List;
import java.util.Map;

import com.java.entity.User;

/**
 * 用戶DAO接口
 * 
 * @author Administrator
 * 
 */
public interface UserDao {
    
    /**
     * 用戶登錄
     * @param user
     * @return
     */
    public User login(User user);
    
    /**
     * 用戶查詢
     * @param map
     * @return用戶集合
     */
    public List<User> find(Map<String, Object> map);
    
    /**
     * 獲取總記錄數
     * @param map
     * @return獲取的total數
     */
    public Long getTotal(Map<String, Object> map);
    
    /**
     * 修改用戶
     * @param user
     * @return影響的記錄數
     */
    public int update(User user);
    
    /**
     * 添加用戶
     * @param user
     * @return影響的記錄數
     */
    public int add(User user);
    
    /**
     * 刪除用戶
     * @param id
     * @return
     */
    public int delete(Integer id);
    
    
}

 

PageBean,用來分頁的一個工具類:

package com.java.entity;

/**
 * 分頁Model類
 * @author 
 *
 */
public class PageBean {

    private int page; // 第幾頁
    private int pageSize; // 每頁記錄數
    private int start;  // 起始頁
    
    public PageBean(int page, int pageSize) {
        super();
        this.page = page;
        this.pageSize = pageSize;
    }
    
    public int getPage() {
        return page;
    }
    public void setPage(int page) {
        this.page = page;
    }
    
    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getStart() {
        return (page-1)*pageSize;
    }
    
    
}

 

User,用戶實體類:

package com.java.entity;

/**
 * 用戶實體
 * @author Administrator
 *
 */
public class User {
    private Integer id;
    private String userName;
    private String password;
    private String trueName;
    private String email;
    private String phone;
    private String roleName;    //角色名稱:系統管理員、銷售主管、客戶經理、高管
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getTrueName() {
        return trueName;
    }
    public void setTrueName(String trueName) {
        this.trueName = trueName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getRoleName() {
        return roleName;
    }
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
    
}

 

UserService:

package com.java.service;

import java.util.List;
import java.util.Map;

import com.java.entity.User;

/**
 * UserService接口
 * @author Administrator
 *
 */
public interface UserService {
    
    /**
     * 用戶登錄
     * @param user
     * @return
     */
    public User login(User user);
    /**
     * 用戶查詢
     * @param map
     * @return
     */
    public List<User> find(Map<String, Object> map);
    
    /**
     * 獲取總記錄數
     * @param map
     * @return
     */
    public Long getTotal(Map<String, Object> map);
    
    /**
     * 修改用戶
     * @param user
     * @return影響的記錄數
     */
    public int update(User user);
    
    /**
     * 添加用戶
     * @param user
     * @return影響的記錄數
     */
    public int add(User user);
    
    /**
     * 刪除用戶
     * @param id
     * @return
     */
    public int delete(Integer id);
}

 

UserServiceImpl:

package com.java.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.java.dao.UserDao;
import com.java.entity.User;
import com.java.service.UserService;

/**
 * UserService實現類
 * @author Administrator
 *
 */
@Service("userService")
public class UserServiceImpl implements UserService{
    
    @Resource
    private UserDao userDao;
    
    @Override
    public User login(User user) {
        return userDao.login(user);
    }
    
    @Override
    public List<User> find(Map<String, Object> map) {
        return userDao.find(map);
    }
    
    @Override
    public Long getTotal(Map<String, Object> map) {
        return userDao.getTotal(map);
    }

    @Override
    public int update(User user) {
        return userDao.update(user);
    }

    @Override
    public int add(User user) {
        return userDao.add(user);
    }

    @Override
    public int delete(Integer id) {
        return userDao.delete(id);
    }
}

 

ResponseUtil工具類,主要用於返回json數據:

package com.java.util;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;


public class ResponseUtil {

    public static void write(HttpServletResponse response,Object object)throws Exception{
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        out.println(object);
        out.flush();
        out.close();
    }
}

 

StringUtil,字符串工具類:

package com.java.util;

/**
 * 字符串工具類
 * @author 
 *
 */
public class StringUtil {

    /**
     * 判斷是否是空
     * @param str
     * @return
     */
    public static boolean isEmpty(String str){
        if(str==null||"".equals(str.trim())){
            return true;
        }else{
            return false;
        }
    }
    
    /**
     * 判斷是否不是空
     * @param str
     * @return
     */
    public static boolean isNotEmpty(String str){
        if((str!=null)&&!"".equals(str.trim())){
            return true;
        }else{
            return false;
        }
    }
    
    /**
     * 格式化模糊查詢
     * @param str
     * @return
     */
    public static String formatLike(String str){
        if(isNotEmpty(str)){
            return "%"+str+"%";
        }else{
            return null;
        }
    }
}

 

UserMapper.xml,mybatis所謂的動態sql語句,全部封裝在這裏:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.java.dao.UserDao">
    
    <!-- 先定義一個Interface,UserMapper,然後Mapper文件的namespace指向的就是這個Interface, UserInterface裏定義的方法和UserMapper.xml文件中的一樣, 
        然後代碼裏直接使用接口 -->
        
    <resultMap type="User" id="UserResult">
        <result property="id" column="id" />
        <result property="userName" column="userName" />
        <result property="password" column="password" />
        <result property="trueName" column="trueName" />
        <result property="email" column="email" />
        <result property="phone" column="phone" />
        <result property="roleName" column="roleName" />
    </resultMap>
    
    <!-- 所謂的動態SQL語句 -->
    <select id="login" parameterType="User" resultMap="UserResult">
        select * from userInfo where userName = #{userName} and password = #{password} and roleName = #{roleName}
    </select>
    
    <!-- 用戶查詢 -->
    <select id="find" parameterType="Map" resultMap="UserResult">
        select * from userInfo
        <where>
            <if test="userName!=null and userName!=‘‘ ">
                and userName like #{userName}
            </if>
        </where>
        <if test="start!=null and size!=null">
            limit #{start},#{size}
        </if>
    </select>
    <select id="getTotal" parameterType="Map" resultType="Long">
        select count(*) from userInfo
        <where>
            <if test="userName!=null and userName!=‘‘ ">
                and userName like #{userName}
            </if>
        </where>
    </select>
    
    <!-- 用戶添加 -->
    <insert id="add" parameterType="User">
        insert into userInfo values(null,#{userName},#{password},#{trueName},#{email},#{phone},#{roleName})
    </insert>
    
    <!-- 用戶修改 -->
    <update id="update" parameterType="User">
        update userInfo
        <set>
            <if test="userName != null and userName != ‘‘">userName = #{userName},</if>
            <if test="password != null and password != ‘‘">password = #{password},</if>
            <if test="trueName != null and trueName != ‘‘">trueName = #{trueName},</if>
            <if test="email != null and email != ‘‘">email = #{email},</if>
            <if test="phone != null and phone != ‘‘">phone = #{phone},</if>
            <if test="roleName != null and roleName != ‘‘">roleName = #{roleName},</if>
        </set>
        where id = #{id}
    </update>
    
    <!-- 用戶刪除 -->
    <delete id="delete" parameterType="Integer">
        delete from userInfo where id = #{id}
    </delete>
</mapper>

 

最後是數據表結構:

技術分享

技術分享

技術分享

技術分享

 

相比hibernate,mybatis輕量級很多,但是新手最容易也最難搞定的往往是環境搭建部分,一不留神就是各種jar包衝突,真叫人抓狂啊啊,以後還得留意呵。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章