服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - JAVA中SSM框架的搭建实现CRUD的方法

JAVA中SSM框架的搭建实现CRUD的方法

2021-02-22 11:37zuidongfeng Java教程

这篇文章主要介绍了JAVA中SSM框架的搭建实现CRUD的方法,小编把运行代码列了出来,供大家参考。

最近在开发公司的一个系统,系统的框架是用ssm的框架搭建的,当然和这次写博客的不一样,它拥有很多的配置文件,企业级的开发所需要的配置文件是非常繁琐的,今天记录一下一个简单的ssm框架的搭建和实现一个crud的操作。

JAVA中SSM框架的搭建实现CRUD的方法

使用的是maven插件来配置我们需要的jar包,由于操作不多,所以并没有配置很多,要注意自己使用的jdk的版本,选择不同版本号的jdk

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelversion>4.0.0</modelversion>
 <groupid>com.lr</groupid>
 <artifactid>ssm</artifactid>
 <packaging>war</packaging>
 <version>0.0.1-snapshot</version>
 <name>ssm maven webapp</name>
 <url>http://maven.apache.org</url>
 <!-- 用来设置版本号 -->
  <properties>
    <srping.version>4.0.2.release</srping.version>
    <mybatis.version>3.2.8</mybatis.version>
    <slf4j.version>1.7.12</slf4j.version>
    <log4j.version>1.2.17</log4j.version>
  </properties>
  <!-- 用到的jar包 -->
  <dependencies>
    <!-- 单元测试 -->
    <dependency>
      <groupid>junit</groupid>
      <artifactid>junit</artifactid>
      <version>4.11</version>
      <!-- 表示开发的时候引入,发布的时候不会加载此包 -->
      <scope>test</scope>
    </dependency>
    <!-- java ee包 -->
    <dependency>
      <groupid>javax</groupid>
      <artifactid>javaee-api</artifactid>
      <version>7.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
      <groupid>com.fasterxml.jackson.core</groupid>
      <artifactid>jackson-databind</artifactid>
      <version>2.8.8</version>
    </dependency>
    
    <!-- spring框架包 start -->
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-test</artifactid>
      <version>${srping.version}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-core</artifactid>
      <version>${srping.version}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-oxm</artifactid>
      <version>${srping.version}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-tx</artifactid>
      <version>${srping.version}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-jdbc</artifactid>
      <version>${srping.version}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-aop</artifactid>
      <version>${srping.version}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-context</artifactid>
      <version>${srping.version}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-context-support</artifactid>
      <version>${srping.version}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-expression</artifactid>
      <version>${srping.version}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-orm</artifactid>
      <version>${srping.version}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-web</artifactid>
      <version>${srping.version}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-webmvc</artifactid>
      <version>${srping.version}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-aspects</artifactid>
      <version>${srping.version}</version>
    </dependency>
    <!-- spring框架包 end -->
    <!-- mybatis框架包 start -->
    <dependency>
      <groupid>org.mybatis</groupid>
      <artifactid>mybatis</artifactid>
      <version>${mybatis.version}</version>
    </dependency>
    <dependency>
      <groupid>org.mybatis</groupid>
      <artifactid>mybatis-spring</artifactid>
      <version>1.2.2</version>
    </dependency>
    <!-- mybatis框架包 end -->
    <!-- 数据库驱动 -->
    <dependency>
      <groupid>mysql</groupid>
      <artifactid>mysql-connector-java</artifactid>
      <version>5.1.35</version>
    </dependency>
    <!-- 导入dbcp的jar包,用来在applicationcontext.xml中配置数据库 -->
    <dependency>
      <groupid>commons-dbcp</groupid>
      <artifactid>commons-dbcp</artifactid>
      <version>1.4</version>
    </dependency>
    <!-- jstl标签类 -->
    <dependency>
      <groupid>jstl</groupid>
      <artifactid>jstl</artifactid>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupid>taglibs</groupid>
      <artifactid>standard</artifactid>
      <version>1.1.2</version>
    </dependency>
    <!-- log start -->
    <dependency>
      <groupid>log4j</groupid>
      <artifactid>log4j</artifactid>
      <version>${log4j.version}</version>
    </dependency>
    <dependency>
      <groupid>org.slf4j</groupid>
      <artifactid>slf4j-api</artifactid>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupid>org.slf4j</groupid>
      <artifactid>slf4j-log4j12</artifactid>
      <version>${slf4j.version}</version>
    </dependency>
    <!-- log end -->
    <!-- json -->
    <!-- 格式化对象,方便输出日志 -->
    <dependency>
      <groupid>com.alibaba</groupid>
      <artifactid>fastjson</artifactid>
      <version>1.2.6</version>
    </dependency>
    <dependency>
      <groupid>org.codehaus.jackson</groupid>
      <artifactid>jackson-mapper-asl</artifactid>
      <version>1.9.13</version>
    </dependency>
    <!-- 上传组件包 start -->
    <dependency>
      <groupid>commons-fileupload</groupid>
      <artifactid>commons-fileupload</artifactid>
      <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupid>commons-io</groupid>
      <artifactid>commons-io</artifactid>
      <version>2.4</version>
    </dependency>
    <dependency>
      <groupid>commons-codec</groupid>
      <artifactid>commons-codec</artifactid>
      <version>1.10</version>
    </dependency>
    <!-- 上传组件包 end -->
 
    <!-- al相关添加 -->
    <dependency>
      <groupid>net.sourceforge.jexcelapi</groupid>
      <artifactid>jxl</artifactid>
      <version>2.6</version>
    </dependency>
    <dependency>
      <groupid>org.apache.poi</groupid>
      <artifactid>poi</artifactid>
      <version>3.8</version>
    </dependency>
    <dependency>
      <groupid>org.apache.poi</groupid>
      <artifactid>poi-ooxml</artifactid>
      <version>3.9</version>
    </dependency>
    <!-- al相关添加 -->
  </dependencies>
 
   <build>
    <finalname>maven_project</finalname>
    <plugins>
      <plugin>
        <groupid>org.apache.maven.plugins</groupid>
        <artifactid>maven-compiler-plugin</artifactid>
        <version>2.3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build> 
</project>

然后配置数据库的连接,改成自己的数据库就行了

?
1
2
3
4
5
6
7
8
driver=com.mysql.jdbc.driver
url=jdbc\:mysql\://locahost\:3306/db
username=root
password=root
maxactive=20
maxidle=20
minidle=1
maxwait=60000

配置文件spring-dao.xml,spring会自动查找其下的类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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">
             
             
  <!-- dao接口所在包名,spring会自动查找其下的类 -->
  <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer">
    <!--basepackage指定要扫描的包,在此包之下的映射器都会被搜索到。
     可指定多个包,包与包之间用逗号或分号分隔-->
    <property name="basepackage" value="com.lr.dao" />
    <property name="sqlsessionfactory" ref="sqlsessionfactory"></property>
  </bean>            
             
</beans>

配置文件spring和mybatis的整合文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?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-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/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
 
  <!-- 引入配置文件 -->
<context:property-placeholder location="classpath:/jdbc.properties" />
  <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close">
    <property name="driverclassname" value="com.mysql.jdbc.driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/db?useunicode=true&characterencoding=utf8" />
    <property name="username" value="root" />
    <property name="password" value="root" />
    <!-- 初始化连接大小 -->
    <property name="initialsize" value="3" />
    <!-- 连接池最大数量 -->
    <property name="maxactive" value="20" />
    <!-- 连接池最大空闲 -->
    <property name="maxidle" value="20" />
    <!-- 连接池最小空闲 -->
    <property name="minidle" value="1" />
    <!-- 获取连接最大等待时间 -->
    <property name="maxwait" value="60000" />
  </bean>
 
  <!-- spring和mybatis完美整合,不需要mybatis的配置映射文件 -->
  <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
    <property name="datasource" ref="datasource" />
    <!-- 自动扫描mapping.xml文件 -->
    <property name="mapperlocations" value="classpath:com/lr/mapper/*.xml"></property>
  </bean>
   
</beans>

配置事物的文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  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/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
 
  <!-- (事务管理)transaction manager, use jtatransactionmanager for global tx -->
  <bean id="transactionmanager"
    class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
    <property name="datasource" ref="datasource" />
  </bean>
   
   <!-- 配置参与事务的类 -->
   <aop:config>
     <aop:pointcut id="allservicemethod" expression="execution(* com.lr.service.*.*(..))"/>
     <aop:advisor pointcut-ref="allservicemethod" advice-ref="txadvice" />
   </aop:config>
   
  <!-- 使用声明方式配置事务 -->
  <tx:advice id="txadvice" transaction-manager="transactionmanager">
     <tx:attributes>
        <tx:method name="*" propagation="required" rollback-for="java.lang.exception"/>
     </tx:attributes>
  </tx:advice>
   
</beans>

配置springmvc.xml文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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">
   
  <!-- 自动扫描 -->
  <context:component-scan base-package="com.lr.controller" />
   
  <mvc:annotation-driven />
  
  <mvc:default-servlet-handler/>
  
  <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
  <bean class="org.springframework.web.servlet.view.internalresourceviewresolver">
    
    <property name="prefix" value="/web-inf/jsp/" />
    <property name="suffix" value=".jsp" />
  </bean>

好了!!!需要配置的文件已经配置完成了,足够我们进行一波操作了

接下来是后台的一些代码,这里主要是实体类,dao层,service层,controller层,我把后台的文件一起上传了,mapper.xml文件是实现方法最关键的地方

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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.lr.dao.iuserdao">
  <resultmap id="baseresultmap" type="com.lr.dto.user">
    <result column="id" property="id" jdbctype="integer" />
    <result column="name" property="name" jdbctype="varchar" />
    <result column="password" property="password" jdbctype="varchar" />
    <result column="age" property="age" jdbctype="integer" />
  </resultmap>
  
  <sql id="base_column_list">
    id, name, password, age
  </sql>
  
  
   <!--  增加用户 -->
   <insert id="adduser" parametertype="com.lr.dto.user">
    insert into user(name,password,age) values(#{name},#{password},#{age})
  </insert>
  
   <!-- 查询用户-->
  <select id="querybyprimarykey" resultmap="baseresultmap"
    parametertype="java.lang.integer">
    select
    <include refid="base_column_list" />
    from user
    where id = #{id}
  </select>
  
  
   <!-- 删除用户 -->
  <delete id="deletebyprimarykey" parametertype="java.lang.integer">
    delete from user
    where id = #{id} 
  </delete>
  
  
  <!-- 更新用户 -->
  <update id="updatebyprimarykey" parametertype="com.lr.dto.user"
    update user set name=#{name},password=#{password},age=#{age} where id=#{id}
  </update>
  
  <select id="findalluser" resulttype="com.lr.dto.user">
    select * from user
  </select>
  
</mapper>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.lr.dao;
 
import java.util.list;
 
import com.lr.dto.user;
 
public interface iuserdao {
   //查询用户
  public user querybyprimarykey(int id);
   //删除用户
  public int deletebyprimarykey(int id);
  //更新用户
  public int updatebyprimarykey(user user);
  //添加用户
  public int adduser(user user);
  //查询所有用户
  public list<user> findalluser();
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.lr.service;
 
import java.util.list;
 
import com.lr.dto.user;
 
public interface iuserservice {
  //查询用户
   public user getuserbyid(int userid);
   //删除
   public void deleteuser(int id);
   //更新用户
   public void updateuser(user user);
  //添加用户
   public void adduser(user user);
   //查看所有用户
   public list<user> findalluser();
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.lr.service.impl;
 
import java.util.list;
 
import javax.annotation.resource;
 
import org.springframework.stereotype.service;
 
import com.lr.dao.iuserdao;
import com.lr.dto.user;
import com.lr.service.iuserservice;
@service("userservice")
public class userserviceimpl implements iuserservice{
  @resource
  private iuserdao userdao;
  
  public iuserdao getuserdao() {
    return userdao;
  }
  public void setuserdao(iuserdao userdao) {
    this.userdao = userdao;
  }
  //查询用户
  @override
  public user getuserbyid(int userid) {
    
    return userdao.querybyprimarykey(userid);
  }
  //更新用户
  @override
  public void updateuser(user user) {
    userdao.updatebyprimarykey(user);
    
  }
  //删除用户
  @override
  public void deleteuser(int id) {
    
    userdao.deletebyprimarykey(id);
  }
  
  //添加用户
  @override
  public void adduser(user user) {
    
    userdao.adduser(user);
  }
  
  //查询所有用户
  @override
  public list<user> findalluser() {
    
    return userdao.findalluser();
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.lr.controller;
 
import java.util.list;
 
import javax.servlet.http.httpservletrequest;
 
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 org.springframework.web.bind.annotation.responsebody;
 
import com.lr.dto.user;
import com.lr.service.iuserservice;
 
@controller
public class usercontroller {
  @autowired
  private iuserservice userservice;
  
  public iuserservice getuserservice() {
    return userservice;
  }
  public void setuserservice(iuserservice userservice) {
    this.userservice = userservice;
  }
  
  //主页面
  @requestmapping("/")
  public string usermgr() {
    return "showuser";
  }
  
  //添加用户
  @requestmapping("/adduser")
  @responsebody
  public void useradd(user user) {
    userservice.adduser(user);
  }
  
  //删除用户
  @requestmapping("/deleteuser")
  @responsebody
  public void deleteuser(int id){
    userservice.deleteuser(id);
  }
  
  //修改用户
  @requestmapping("/updateuser")
  @responsebody
  public void upadteuser(user user){
    
    userservice.updateuser(user);
    
  }
  //根据id查找用户
  @requestmapping("/showuser")
  @responsebody
  public user showuser(int id,model model){
     return userservice.getuserbyid(id);    
  }
  
  //查询所有用户
  @requestmapping("/findalluser")
  @responsebody
  public list<user> findalluser(){
    return userservice.findalluser();
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
 
<!doctype html>
<html>
 <head> 
  <meta charset="utf-8"><script type="text/javascript" src="<%= application.getcontextpath() %>/js/jquery-1.12.4.min.js"></script>
  <script type="text/javascript">
  
  
  
  //添加用户
    $(function(){
      $("#add").on("click", addnewuser);   
    })
    function addnewuser(){
      var name = $.trim($("#txtname").val());
      var password = $.trim($("#txtpassword").val());
      var age = $.trim($("#txtage").val());
      
      $.post("/ssm/adduser", {"name": name, "password": password, "age": age}, function(){
        alert("添加成功!")
      });
    }
    
    
    
  //删除用户
    $(function(){
      $("#delete").on("click",deleteuser);
    })
    function deleteuser(){
      var id=$.trim($("#deleteid").val());
      $.get("/ssm/deleteuser",{"id":id},function(){
        alert("删除成功!")
      });
    }
    
    
  //查询所有用户
    $(function(){
      $("#findalluser").click(function(){
        $.ajax({
          type:"post",
          datatype:"json",
          url:"/ssm/findalluser",
          success:function(msg){
            var str="";
            for(i in msg){
              str+="<tr><th>"+msg[i].id+"</th><th>"+msg[i].name+"</th><th>"
              +msg[i].password+"</th><th>"+msg[i].age+"</th><tr>"
            }
            $("#findall").append(str);
          }
        });
      });
    });
    
    
    //根据id查找一个用户
    
    $(function(){
      $("#find").click(function(){
        $.ajax({
          type:"post",
          data:{id:$("#findid").val()},
          datatype:"json",
          url:"/ssm/showuser",
          success:function(user){
            var str="";
              str+="<tr><th>"+user.id+"</th><th>"+user.name+"</th><th>"
              +user.password+"</th><th>"+user.age+"</th><tr>"
            $("#finduserbyid").append(str);
          }
          
        })
      })
    })
    
    
    //根据id修改用户信息
    $(function(){
      $("#update").on("click",updateuser);
    })
    function updateuser(){
       alert($.trim($("#updateid").val()))
      alert($.trim($("#updatename").val()))
      alert($.trim($("#updatepassword").val()))
      alert($.trim($("#updateage").val()))
    
      var id=$.trim($("#updateid").val());
      var name=$.trim($("#updatename").val());
      var password=$.trim($("#updatepassword").val());
      var age=$.trim($("#updateage").val());
      $.post("/ssm/updateuser",{"id":id,"name":name,"password":password,"age":age},function(){
        alert("修改成功!")
      });
    }
  </script>
  
  
 <title>用户管理</title> 
 </head>
 <body>
  <div>
    <p>姓名:<input type="text" id="txtname"></p>
    <p>密码:<input type="password" id="txtpassword"></p>
    <p>年龄:<input type="text" id="txtage"></p>
    <p><button id="add">添加</button></p>
  </div>
  <hr style="height:1px;border:none;border-top:1px dashed #0066cc;" />
  
  <div>
    <p>输入用户id:<input type="text" id="deleteid"></p>
    <p><button id="delete">删除</button></p>
  </div>
  
  <hr style="height:1px;border:none;border-top:1px dashed #0066cc;" />
  
  <div><p><button id="findalluser">查询所有</button></p></div>
  <div>
    <table width="300" border="1" >
      <thead id="findall">
        <tr>
          <th width="50">id</th>
          <th width="50">姓名</th>
          <th width="50">密码</th>
          <th width="50">年龄</th>
        </tr>
      </thead>
    </table>
  </div>
  <hr style="height:1px;border:none;border-top:1px dashed #0066cc;" />
  
  <div>
    <p>输入用户id:<input type="text" id="findid"></p>
    <p><button id="find">查询</button></p>
  </div>
   <div>
    <table width="300" border="1" >
      <thead id="finduserbyid">
        <tr>
          <th width="50">id</th>
          <th width="50">姓名</th>
          <th width="50">密码</th>
          <th width="50">年龄</th>
        </tr>
      </thead>
    </table>
  </div>
  <hr style="height:1px;border:none;border-top:1px dashed #0066cc;" />
  <div>
    <p>输入用户id:<input type="text" id="updateid"></p>
    <p>输入用户姓名:<input type="text" id="updatename"></p>
    <p>输入密码:<input type="password" id="updatepassword"></p>
    <p>输入用户年龄:<input type="password" id="updateage"></p>
    <p><button id="update">修改</button></p>
  </div>
 </body>
</html>

原文链接:https://www.cnblogs.com/zuidongfeng/p/7903570.html

延伸 · 阅读

精彩推荐