Spring Mvc Mybatis(初次学习)

 Spring+MVC+Mybatis环境的搭建

自己到我的网盘下载里面的所有东西,待会会使用到。       密码:146a

1.新建项目名称为:springmvcmybatis  

  Target runtime选择apache tomacet v8.0

  其他的默认

2.建好的目录结构:

3.接下来就是框架的搭建  

  a 首先导入相应的jar包    

    1)在网盘上将lib解压下来的包如下图所示,把下面的包全部复制到lib\

    

    2)复制完目录结构为:

    

 b接下来建立数据库:

    1)数据库语句:参照网盘的test.sql(Mysql)

    2)把网盘上的mybatis-generator-core-1.3.2这个文件进行解压,找到这个目录

    

    3)对generatorConfig.xml进行修改,比较模糊,付上代码

    

    

     

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE generatorConfiguration  
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">  
<generatorConfiguration>  
<!-- 数据库驱动-->  
    <classPathEntry  location="mysql-connector-java-5.1.25-bin.jar"/>  
    <context id="DB2Tables"  targetRuntime="MyBatis3">  
        <commentGenerator>  
            <property name="suppressDate" value="true"/>  
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
            <property name="suppressAllComments" value="true"/>  
        </commentGenerator>  
        <!--数据库链接URL,用户名、密码 -->  
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/test" userId="root" password="ddxkj">  
        </jdbcConnection>  
        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false"/>  
        </javaTypeResolver>  
        <!-- 生成模型的包名和位置-->  
        <javaModelGenerator targetPackage="stream.ice.model" targetProject="sr/main/java">  
            <property name="enableSubPackages" value="true"/>  
            <property name="trimStrings" value="true"/>  
        </javaModelGenerator>  
        <!-- 生成映射文件的包名和位置-->  
        <sqlMapGenerator targetPackage="stream.ice.mapping" targetProject="sr/main/java">  
            <property name="enableSubPackages" value="true"/>  
        </sqlMapGenerator>  
        <!-- 生成DAO的包名和位置-->  
        <javaClientGenerator type="XMLMAPPER" targetPackage="stream.ice.dao" targetProject="sr/main/java">  
            <property name="enableSubPackages" value="true"/>  
        </javaClientGenerator>  
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->  
        <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    <table tableName="comment" domainObjectName="Comment" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>  
</generatorConfiguration>  

      4)在此目录下继续建立文件夹sr\main\java

      

      5)按住shift右击鼠标:

      

      6)输入:java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml –overwrite

      回车键,成功后显示如下页面:

      

      7)stream整个文件夹复制到src的目录下

      

  c  spring 与 mybatis的整合

    a 建立JDBC属性文件 jdbc.properties  内容如下:

      

      1)jdbc内容如下:        

     driverClassName=com.mysql.jdbc.Driver
     url=jdbc:mysql://127.0.0.1:3306/test?unicode=true&characterEncoding=utf8
     username=root
     password=ddxkj
     initialSize=10
     maxActive=20
     maxIdle=10
     minIdle=5
     maxWait=6000

      2)建立spring-mybatis.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- 自动扫描 -->
    <context:component-scan base-package="stream.ice.service" />
    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${driverClassName}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:stream/ice/mapping/*.xml"></property>
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="stream.ice.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <!-- 拦截器方式配置事物 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="append*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="repair" 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="search*" propagation="REQUIRED" read-only="true" />
            <tx:method name="datagrid*" propagation="REQUIRED" read-only="true" />

            <tx:method name="*" propagation="REQUIRED" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="transactionPointcut" expression="execution(* stream.ice.service..*Impl.*(..))" />
        <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
    </aop:config>

</beans>

 

        4)c Log4j的配置 建立log4j.properties:

        

log4j.rootLogger=Debug,Console,File
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
log4j.appender.File = org.apache.log4j.RollingFileAppender
log4j.appender.File.File = logs/ssm.log
log4j.appender.File.MaxFileSize = 10MB
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

    b建立IUserService接口和实现IUserService的类IUserService

    

      1)IUserService代码:

      

package stream.ice.service;

import java.util.List;

import stream.ice.model.User;

public interface IUserService {
    public int addUser(User user);
    
}

      2)UserServiceImpl代码:

        

package stream.ice.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import stream.ice.dao.UserMapper;
import stream.ice.model.User;
import stream.ice.service.IUserService;
@Service("userService")
public class UserServiceImpl implements IUserService{
    @Autowired
    private UserMapper userDao;
    @Override
    public int addUser(User user) {
        // TODO Auto-generated method stub
        return userDao.insertSelective(user);
    }
    

}

  d接下来就是建立测试类 
    1)如下图所示:

      

      2)测试类代码:

package stream.ice.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import stream.ice.model.User;
import stream.ice.service.IUserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring-mybatis.xml" })
public class UserTest {
    @Autowired
    private IUserService userService;
    /**
     * 添加用户
     */
    @Test
    public void test4() {
        User u = new User();
        u.setAddress("安溪县");
        u.setAge(33);
        u.setEmail("1234568@qq.com");
        u.setName("测试22");
        u.setPhone("1378526636");
        userService.addUser(u);
        System.out.println("刚添加的用户id:" + u.getId());
    }

}

      3)效果图如下:

        

      至此,测试已经基本完成。

  e整合SpringMVC

    1)配置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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
     <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />          
    
    <!-- 静态文件处理 -->
    <mvc:resources mapping="/resources/**" location="/resources/" />   

                        
    <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
    <context:component-scan base-package="stream.ice.controller" />
    <!--避免IE执行AJAX时,返回JSON出现下载文件 -->
    <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" />    <!-- JSON转换器 -->
            </list>
        </property>
    </bean>
    <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
    <bean id="multipartResolver"  
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="utf-8" />  
        <!-- 文件大小最大值 -->
        <property name="maxUploadSize" value="10485760000" />  
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="40960" />  
    </bean> 
    
  

</beans>

 

      2)配置web.xml文件

      

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <!-- Spring和mybatis的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mybatis.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>
    <!-- 防止Spring内存溢出监听器 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

    <!-- Spring MVC servlet -->
    <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>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

    3)编写controller测试

    

        4)对IUserService接口添加方法

        

package stream.ice.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import stream.ice.model.User;
import stream.ice.service.IUserService;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService iUserService;
    /**
     * 查找所有用户
     * 
     * @param model
     * @return
     */
    @RequestMapping("/findAll")
    public String findAllUser(Model model) {
        List<User> userList = iUserService.getUserList();
        if (userList != null && userList.size() > 0) {
            model.addAttribute("userList", userList);
        }
        return "list";
    }
}

      5)向usermapper.xml添加数据库语句

      

<?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="stream.ice.dao.UserMapper" >
  <resultMap id="BaseResultMap" type="stream.ice.model.User" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="email" property="email" jdbcType="VARCHAR" />
    <result column="phone" property="phone" jdbcType="VARCHAR" />
    <result column="age" property="age" jdbcType="INTEGER" />
    <result column="address" property="address" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, name, email, phone, age, address
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="stream.ice.model.User" >
    insert into user (id, name, email, 
      phone, age, address
      )
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, 
      #{phone,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, #{address,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="stream.ice.model.User" >
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="name != null" >
        name,
      </if>
      <if test="email != null" >
        email,
      </if>
      <if test="phone != null" >
        phone,
      </if>
      <if test="age != null" >
        age,
      </if>
      <if test="address != null" >
        address,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="name != null" >
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="email != null" >
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="phone != null" >
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="age != null" >
        #{age,jdbcType=INTEGER},
      </if>
      <if test="address != null" >
        #{address,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="stream.ice.model.User" >
    update user
    <set >
      <if test="name != null" >
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="email != null" >
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="phone != null" >
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="age != null" >
        age = #{age,jdbcType=INTEGER},
      </if>
      <if test="address != null" >
        address = #{address,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="stream.ice.model.User" >
    update user
    set name = #{name,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      phone = #{phone,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER},
      address = #{address,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
   <select id="getUserList" resultMap="BaseResultMap">
   select 
 <include refid="Base_Column_List" />
    FROM  user
  </select>
</mapper>

    6)向usermapper添加方法

package stream.ice.dao;

import java.util.List;

import stream.ice.model.User;

public interface UserMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
    List<User> getUserList();
}

      7)实现IUserService的方法,并修改返回值

        

package stream.ice.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import stream.ice.dao.UserMapper;
import stream.ice.model.User;
import stream.ice.service.IUserService;
@Service("userService")
public class UserServiceImpl implements IUserService{
    @Autowired
    private UserMapper userDao;
    @Override
    public int addUser(User user) {
        // TODO Auto-generated method stub
        return userDao.insertSelective(user);
    }
    @Override
    public List<User> getUserList() {
        
        return userDao.getUserList();
    }


}

      8)建立index.jsp页面

        

      代码如下:

      

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <a href="user/findAll">用户列表</a><br>
 <a href="user/showUser?id=1">单个用户</a>
</body>
</html>

      9)建立jsp文件夹和list.jsp列表

      

      代码如下:

      

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>用户列表</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet"
    href="http://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css"
    href="../resources/css/bootstrap.min.css">
<link rel="STYLESHEET" type="text/css"
    href="${pageContext.request.contextPath}/dhtmlxSuitepro3.6/dhtmlxGrid/codebase/dhtmlxgrid.css">
<link rel="stylesheet" type="text/css"
    href="${pageContext.request.contextPath}/dhtmlxSuitepro3.6/dhtmlxGrid/codebase/skins/dhtmlxgrid_dhx_skyblue.css">
<script
    src="${pageContext.request.contextPath}/dhtmlxSuitepro3.6/dhtmlxGrid/codebase/dhtmlxcommon.js"></script>
<script
    src="${pageContext.request.contextPath}/dhtmlxSuitepro3.6/dhtmlxGrid/codebase/dhtmlxgrid.js"></script>
<script
    src="${pageContext.request.contextPath}/dhtmlxSuitepro3.6/dhtmlxGrid/codebase/dhtmlxgridcell.js"></script>
<script
    src="${pageContext.request.contextPath}/dhtmlxSuitepro3.6/dhtmlxGrid/codebase/ext/dhtmlxgrid_pgn.js"></script>
<style type="text/css">
table td {
    height: 40px;
    text-align: center;
}

table th {
    text-align: center;
}

.bg {
    position: absolute;
    background-color: hsla(0, 0%, 50%, 0.5);
    top: 0;
    left: 0;
}

.box {
    position: absolute;
    top: 130px;
    left: 150px;
    z-index: 99;
    border-radius: 4px;
    padding: 5px;
    background-color: #fff;
    line-height: 20px;
    font-size: 12px;
    color: #666;
    font-weight: bold;
    width: 35%;
    height: 50%;
}

.box a {
    display: block;
    position: absolute;
    z-index: 100;
    top: -8px;
    left: 498px;
    border-radius: 9px;
    border: 2px solid #e4e4e4;
    background-color: #bbb;
    line-height: 14px;
    width: 14px;
    text-align: center;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 14px;
    color: #FFF;
    text-decoration: none;
}

.box button {
    margin-left: auto;
}

.box form {
    padding: 30px;;
}
</style>
</head>
<body>
    <!-- 添加用户开始 -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog"
        aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"
                        aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel">添加用户</h4>
                </div>
                <div class="modal-body">

                    <input type='text' placeholder='请输入您的姓名' class='form-control'
                        id="name"><br> <input type='text'
                        placeholder='请输入您的邮箱' class='form-control' id="email"><br>
                    <input type='text' placeholder='请输入您的手机' class='form-control'
                        id="phone"><br> <input type='text'
                        placeholder='请输入您的年龄' class='form-control' id="age"><br>
                    <input type='text' placeholder='请输入您的地址' class='form-control'
                        id="address"><br>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" data-dismiss="modal"
                        onclick="addUser();">添加</button>
                </div>
            </div>
        </div>
    </div>
    <!-- 添加用户结束 -->
    <!-- 修改用户开始 -->
    <div class="modal fade" id="updateUser" tabindex="-1" role="dialog"
        aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"
                        aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title">修改用户</h4>
                </div>
                <div class="modal-body">
                    <input type="hidden" id="update_id"><br> <input
                        type='text' placeholder='请输入您的姓名' class='form-control'
                        id="update_name"><br> <input type='text'
                        placeholder='请输入您的邮箱' class='form-control' id="update_email"><br>
                    <input type='text' placeholder='请输入您的手机' class='form-control'
                        id="update_phone"><br> <input type='text'
                        placeholder='请输入您的年龄' class='form-control' id="update_age"><br>
                    <input type='text' placeholder='请输入您的地址' class='form-control'
                        id="update_address"><br>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" data-dismiss="modal"
                        onclick="updateUser();">修改</button>
                </div>
            </div>
        </div>
    </div>
    <!-- 修改用户结束 -->
    <div class="container-fluid">
        <div class="row-fluid" style="margin-top: 10px">
            <div
                class="col-md-3 col-md-push-9 col-sm-6 col-sm-push-6 col-xs-6 col-xs-push-6"
                style="margin-left: -28px">
                <div class="input-group">
                    <input type="text" class="form-control" placeholder="Search for..."
                        id="search"> <span class="input-group-btn">
                        <button class="btn btn-primary" type="button"
                            onclick="searchUser()">
                            <span class="glyphicon glyphicon-search" aria-hidden="true"></span>
                        </button>
                    </span>
                </div>
            </div>
            <button type="button" class="btn btn-default" style="float: right"
                aria-hidden="true" data-toggle="modal" data-target="#myModal">
                <span class="glyphicon glyphicon-plus"></span>
            </button>
        </div>
    </div>
    <div class="row-fluid">
        <br>
        <table
            class="table table-striped table-bordered table-hover datatable">
            <thead>
                <tr>
                    <th>姓名</th>
                    <th>年龄</th>
                    <th>邮箱</th>
                    <th>手机</th>
                    <th>地址</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody id="table2">
                <c:forEach items="${userList}" var="user">
                    <tr class="odd grade">
                        <td>${user.name}</td>
                        <td>${user.age}</td>
                        <td>${user.email}</td>
                        <td>${user.phone}</td>
                        <td>${user.address}</td>
                        <td><a href="javascript:;"
                            onclick="toUpdateUser('${user.id}')">修改</a>&nbsp<a
                            href="javascript:;" onclick="deleteUser('${user.id}',this)">删除</a></td>
                    </tr>
                </c:forEach>



            </tbody>
        </table>
        <div id="currentPage">
            <div id="Pagination" class="pagination"></div>
        </div>
    </div>
    <table width="500">
        <tr>
            <td id="recinfoArea"></td>
        </tr>
        <tr>
            <td>
                <div id="gridbox"
                    style="widht: 500px; height: 150px; background-color: white; overflow: hidden"></div>
            </td>
        </tr>
        <tr>
            <td id="pagingArea"></td>
        </tr>
    </table>
    <script type="text/javascript" src="../resources/js/jquery.min.js"></script>
    <script type="text/javascript" src="../resources/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="../resources/js/addUser.js"></script>
</body>
</html>

      11)导入框架bootstrap和页面用到的js文件。

      

  F最终效果图如下所示:

  

  初次学习,不足的地方还希望各位大神不吝赐教,QQ:491166159,同时在此要感谢微信企业号开发交流 (89714226)的下雨天。。。(379186704) ,欢迎大家到这个群里或者加我QQ共同学习!

  源码下载: 下载  密码:b7a3

 

 

 

 

 

 

 

 

  

 

 

 

 

      

    

 

 

 

  

posted @ 2015-04-22 09:47  streamice  阅读(613)  评论(0编辑  收藏  举报