ssm整合的增删改查

时间:2021-10-06 05:10:07

maven+ssm+bootstrap实现简单的增删改查
学习完ssm框架以后 做的第一个完整的增删改查
是在博客园找到的一个小demo,我自己稍微修改了一下,加入了bootstrap和mybatis的分页插件,有些地方并没有处理好,等以后水平进步了在继续改进一下吧

工程结构
ssm整合的增删改查

pom文件

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.summer</groupId>
<artifactId>ssmdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<dependencies>
<!-- springMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>

<!-- spring jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>

<!-- spring aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>

<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>

<!-- mybatis与spring整合 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 数据库连接池 和驱动-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>

<!-- jstl junit servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<!-- spring单元测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
<scope>test</scope>
</dependency>

<!-- 引入Mybatis分页插件 -->
<!-- 引入5.0版本在测试的时候会报错 4.2版本也会报错
4.2版本总是出现String无法转换为某个对象的错误
-->

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.5</version>
</dependency>
</dependencies>
</project>

jar包一览
ssm整合的增删改查

1:实体类:

package com.ssm.model;
/**
* 描述:用户实体类<BR>
* 创建人:<BR>
* 时间:2017年7月10日下午6:11:17<BR>
* @version
*/

public class User {

//主键
private int id;
//用户名
private String username;
//年龄
private String age;

public User() {
super();
}

public User(int id, String username, String age) {
super();
this.id = id;
this.username = username;
this.age = age;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

}
2:*创建DAO接口【在mybatis中 mapper就相当于dao】*
package com.ssm.mapper;

import java.util.List;

import com.ssm.model.User;
/**
* 描述:用户mapper接口<BR>
* 创建人:<BR>
* 时间:2017年7月10日下午6:14:22<BR>
* @version
*/

public interface UserMapper {

void saveUser(User user);

boolean updateUser(User user);

boolean deleteUser(int id);

User findUserById(int id);

List<User> findAll();
}
*3:实现dao接口【mybatis中就是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">

<!--
namespace:必须与对应的接口全类名一致 UserMapper.java
id :必须与对应接口的某个对应的方法名一致即必须要和UserMapper.java接口中的方法同名。
-->

<mapper namespace="com.ssm.mapper.UserMapper">

<!-- 新增 -->
<insert id="saveUser" parameterType="User" >
insert into t_user(user_name,user_age) values (#{username},#{age})
</insert>

<!-- 修改 -->
<update id="updateUser" parameterType="User" >
update t_user set user_name=#{username},user_age=#{age} where user_id=#{id}
</update>

<!-- 删除 -->
<delete id="deleteUser" parameterType="int">
delete from t_user where user_id=#{id}
</delete>

<!-- 根据id查找单个用户 -->
<select id="findUserById" parameterType="int" resultType="User">
select user_id id,user_name username,user_age age from t_user where user_id=#{id}
</select>

<!-- 查询所有 -->
<select id="findAll" resultType="User">
select user_id id,user_name userName,user_age age from t_user
</select>
</mapper>
4:spring配置文件
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
"
>


<!-- 注解扫描包 -->
<context:component-scan base-package="com.summer" />
<!-- 加载jdbc.properties文件 -->

<context:property-placeholder location="classpath:jdbc.properties"/>
<!--第一步: 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>

<!-- 配置和mybatis的整合 -->
<!--第二步: 创建sqlSessionFactory工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 指定Mybatis全局配置文件的位置 -->
<property name="configLocation" value="classpath:sqlConfig.xml"></property>
</bean>

<!-- 配置mybatis的代理接口开发 * 接口类名和映射文件必须同名 * 接口类和映射文件必须在同一个目 录下 * 接口的映射文件的namespace名称必须是接口的全限定名
* 接口的方法名必须和映射的statement的id一致 -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描所有dao接口的实现,加入到ioc容器中 -->
<property name="basePackage" value="com.ssm.mapper"></property>
<property name="SqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>

<!--第三步: 事物 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 配置拦截service -->
<aop:config>
<aop:pointcut expression="execution(* com.ssm.service..*(..))" id="txPoint"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>

<!-- 配置事物增强,事物如何切入-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 所有方法都是事物方法 -->
<tx:method name="*"/>
<!-- 以get开始的所有方法 -->
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>

</beans>
5:mybatis配置文件
<?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.ssm.model" />
</typeAliases>

<!-- mybatis分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper"></plugin>
</plugins>

</configuration>
6:log4j.properties
### set log levels ###
log4j.rootLogger = INFO , console , debug , error

### console ###
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 = %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n

### log file ###
log4j.appender.debug = org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.File = ../logs/springmvc-demo.log
log4j.appender.debug.Append = true
log4j.appender.debug.Threshold = INFO
log4j.appender.debug.layout = org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n

### exception ###
log4j.appender.error = org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.File = ../logs/springmvc-demo_error.log
log4j.appender.error.Append = true
log4j.appender.error.Threshold = ERROR
log4j.appender.error.layout = org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n


###\u9700\u8981\u58F0\u660E\uFF0C\u7136\u540E\u4E0B\u65B9\u624D\u53EF\u4EE5\u4F7Fdruid sql\u8F93\u51FA\uFF0C\u5426\u5219\u4F1A\u629B\u51FAlog4j.error.key not found
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %l %c%n%p: %m%n

### druid sql ###
log4j.logger.druid.sql=warn,stdout
log4j.logger.druid.sql.DataSource=warn,stdout
log4j.logger.druid.sql.Connection=warn,stdout
log4j.logger.druid.sql.Statement=warn,stdout
log4j.logger.druid.sql.ResultSet=warn,stdout
7:springmvc.xml springmvc的配置文件
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/context/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
"
>

<!-- 扫描注解包 -->
<context:component-scan base-package="com.ssm"></context:component-scan>

<!-- 配置sprigmvc视图解析器:解析逻辑试图
后台返回逻辑试图:index
视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/page/index.jsp-->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<!--1: mvc:annotation-driven默认创建了 RequestMappingHandlerMapping和 RequestMappingHandlerAdapter对象
提供对JSon数据格式的支持-->

<mvc:annotation-driven />
<!-- 2.配置静态资源访问 -->
<mvc:default-servlet-handler/>
</beans>
8:jdbc.properties

jdbc.url = jdbc\:mysql\:///ssm
jdbc.driver = com.mysql.jdbc.Driver
jdbc.username= root
jdbc.password= root
9: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_2_5.xsd" id="WebApp_ID" version="2.5">

<display-name>ssmdemo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- 加载Spring容器配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 配置SpringMVC核心控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 拦截所有请求 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- 字符编码过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


</web-app>
10:建立service以及service实现【UserService.java】
package com.ssm.service;

import java.util.List;

import com.ssm.model.User;
/**
* 描述:用户service<BR>
* 创建人:<BR>
* 时间:2017年7月11日下午7:06:59<BR>
* @version
*/

public interface UserService {

void saveUser(User user);

boolean updateUser(User user);

boolean deleteUser(int id);

User findUserById(int id);

List<User> findAll();
}
service实现类
package com.ssm.service.impl;

import java.util.List;

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

import com.ssm.mapper.UserMapper;
import com.ssm.model.User;
import com.ssm.service.UserService;
@Service
/**
* 描述:用户service实现类<BR>
* 创建人:<BR>
* 时间:2017年7月10日下午7:11:57<BR>
* @version
*/

public class UserServiceImpl implements UserService {

/**
* 注入UserMapper接口
*/

@Autowired
private UserMapper userMapper;

/**
* 新增用户
*/

@Override
public void saveUser(User user) {
userMapper.saveUser(user);
}

/**
* 更新用户
*/

@Override
public boolean updateUser(User user) {
return userMapper.updateUser(user);
}

/**
* 根据Id删除用户
*/

@Override
public boolean deleteUser(int id) {
return userMapper.deleteUser(id);
}

/**
* 根据id查找用户
*/

@Override
public User findUserById(int id) {
User user = userMapper.findUserById(id);
return user;
}

/**
* 查询所有用户
*/

@Override
public List<User> findAll() {
List<User> allUser = userMapper.findAll();
return allUser;
}

}
11:建立controller
package com.ssm.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 org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.model.User;
import com.ssm.service.UserService;

@Controller
@RequestMapping("/user")
/**
* 描述:用户controller<BR>
* 创建人:<BR>
* 时间:2017年7月10日下午7:15:58<BR>
* @version
*/

public class UserController {

@Autowired
private UserService userService;

/**
*跳转到添加用户界面
*/

@RequestMapping("toAddUser")
public String toAddUser(){
return "addUser";
}

/**
* 添加用户并重定向
* @param model
* @param user
* @return
*/

@RequestMapping("addUser")
public String addUser(Model model,User user){
if(user != null){
userService.saveUser(user);
}
return "redirect:/user/userInfo";
}

/**
* 修改用户
* @param model
* @param request
* @param user
* @return
*/

@RequestMapping("updateUser")
public String UpdateUser(Model model,User user){
if(userService.updateUser(user)){
user = userService.findUserById(user.getId());
model.addAttribute("user", user);
return "redirect:/user/userInfo";
}
return "/error";
}

/**
* 查询所有用户
* @param request
* @param model
* @return
*/

@RequestMapping("getAllUser")
public String getAllUser(Model model){
List<User> user = userService.findAll();
model.addAttribute("userList",user);
return "allUser";
}

/**
* 查询单个用户
* @param id
* @param request
* @param model
* @return
*/

@RequestMapping("/getUser")
public String getUser(int id,Model model){
model.addAttribute("user", userService.findUserById(id));
return "editUser";
}
/**
* 根据id删除用户
* @param id
* @param request
* @param response
*/

@RequestMapping("/delUser")
public String deleteUser(int id,Model model){
model.addAttribute("user", userService.deleteUser(id));
return "redirect:/user/userInfo";
}

/**
* 分页查询用户信息
* @param pn 默认从第一页开始 请求参数
* @param model
* @return
*/

@RequestMapping("userInfo")
public String getUsers(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){
//从第一条开始 每页查询五条数据
PageHelper.startPage(pn, 5);
List<User> users = userService.findAll();
//将用户信息放入PageInfo对象里
PageInfo page = new PageInfo(users,5);
model.addAttribute("pageInfo", page);
return "allUser";
}
}

好啦 后端部分至此基本结束
下面就是前台页面

由于除了首页 其他页面都是放在WEB-INF下 所以我们通过首页来跳转一下
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<%
pageContext.setAttribute("path", request.getContextPath());
%>

<!DOCTYPE htm >
<html>
<head>
<title>首页</title>
<style type="text/css">
a {
text-decoration: none;
color: #fff;
font-size: 14px;
}


h3 {
width: 180px;
height: 38px;
margin: 100px auto;
text-align: center;
line-height: 38px;
background: #5BC0DE;
border-radius: 4px;
}

</style>
</head>
<body>
<h3>
<a href="${path }/user/userInfo">进入用户管理页</a>
</h3>
</body>
</html>

ssm整合的增删改查

点击这里可以跳转到用户列表界面
在页面引入bootstrap需要的文件 他提供的按钮样式 列表样式 分页导航等很方便,省的我们在去写前端的东西

用户列表界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
pageContext.setAttribute("path", request.getContextPath());
%>


<!DOCTYPE HTML>
<html>
<head>
<title>用户列表</title>
<link
href="${path}/static/bootstrap-3.3.7-dist/css/bootstrap.min.css"
rel="stylesheet">

<script type="text/javascript"
src="${path}/static/bootstrap-3.3.7-dist/js/jquery-1.10.1.min.js">
</script>
<script type="text/javascript"
src="${path}/static/bootstrap-3.3.7-dist/js/bootstrap.min.js">
</script>
</head>

<body>

<div class="container">
<!-- 标题 -->
<div class="row">
<div class="col-md-12">
<h1>用户管理</h1>
</div>
</div>
<!-- 按钮 -->
<div class="row">
<div class="col-md-4 col-md-offset-8">
<a class="btn btn-primary" href="${path}/user/toAddUser">新增</a>
</div>
</div>
<!-- 表格 -->
<div class="row">
<div class="col-md-12">
<table class="table table-hover">
<tr>
<th>id</th>
<th>用户名</th>
<th>年龄</th>
<th>操作</th>
</tr>
<c:forEach items="${pageInfo.list }" var="user">
<tr>
<td>${user.id}</td>
<td>${user.username }</td>
<td>${user.age }</td>
<!-- <td><a class="edit">编辑</a> <a
class="delete">删除</a></td> -->

<td>
<a type="button" href="${path}/user/getUser?id=${user.id}" class="btn btn-info btn-sm">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
编辑</a>
<a type="button" href="${path}/user/delUser?id=${user.id}" class="btn btn-danger btn-sm">
<span class="glyphicon glyphicon-trash" aria-hidden="true" ></span>
删除</a>
</td>
</tr>
</c:forEach>
</table>
</div>
</div>

<hr style="height:1px;border:none;border-top:1px solid #ccc;" />
<!-- 分页导航栏 -->

<!-- 分页信息 -->
<div class="row">
<!-- 分页文字信息,其中分页信息都封装在pageInfo中 -->
<div class="col-md-6">
当前第:${pageInfo.pageNum}页,总共:${pageInfo.pages}页,总共:${pageInfo.total}条记录
</div>

<!-- 分页条 -->
<div class="col-md-6">
<nav aria-label="Page navigation">
<ul class="pagination">
<li><a href="${path}/user/userInfo?pn=1">首页</a></li>
<c:if test="${pageInfo.hasPreviousPage }">
<li>
<a href="${path}/user/userInfo?pn=${pageInfo.pageNum-1}" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
</c:if>

<c:forEach items="${pageInfo.navigatepageNums }" var="page_Num">
<c:if test="${page_Num == pageInfo.pageNum }">
<li class="active"><a href="#">${ page_Num}</a></li>
</c:if>
<c:if test="${page_Num != pageInfo.pageNum }">
<li><a href="${path}/user/userInfo?pn=${ page_Num}">${ page_Num}</a></li>
</c:if>
</c:forEach>
<c:if test="${pageInfo.hasNextPage }">
<li>
<a href="${path}/user/userInfo?pn=${pageInfo.pageNum+1}" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</c:if>
<li><a href="${path}/user/userInfo?pn=${pageInfo.pages}">末页</a></li>
</ul>
</nav>
</div>
</div>
</div>
</body>
</html>

通过首页 就跳到了这里来 然后进行增删改查
在引入mybatis分页插件的时候5.0 版本会出现找不到sqlSessionFactory的异常 4.2版本会报转换异常 郁闷 所以这里 如果你们也遇到了同样的问题 可以注意一下
ssm整合的增删改查

用户添加界面
为了省事 没必要引入bootstrap了
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<html>
<title>添加用户</title>
</head>
<body>
<h1>添加用户</h1>
<form action="" name="userForm">
姓名:<input type="text" name="username"><br>
年龄:<input type="text" name="age"><br>
<input type="button" value="添加"
onclick="addUser()">

</form>

<script type="text/javascript">
function addUser() {
var form = document.forms[0];
form.action = "<%=basePath %>user/addUser";
form.method = "post";
form.submit();
}
</script>
</body>
</html>

ssm整合的增删改查

修改界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>编辑用户</title>

<script type="text/javascript">
function updateUser() {
var form = document.forms[0];
form.action = "<%=basePath %>user/updateUser";
form.method = "post";
form.submit();
}
</script>

</head>
<body>
<h1>添加用户</h1>
<form action="" name="userForm">
<input type="hidden" name="id" value="${user.id }" /> 姓名:<input
type="text" name="username" value="${user.username }" />
年龄:<input
type="text" name="age" value="${user.age }" />
<input type="button"
value="编辑" onclick="updateUser()" />

</form>
</body>

</html>

ssm整合的增删改查

当然 删除点击一下就直接删除了,然后会回显到用户列表页面

最后就是数据库的建表语句 很简单了 我这里就贴上来 万一有小伙伴要照着我这个来一遍 可以直接复制粘贴过去就好了

DROP TABLE IF EXISTS `t_user`;  
CREATE TABLE `t_user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(20) NOT NULL,
`user_age` varchar(20) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

ssm整合的增删改查

我的maven自己另外设置了一下,有的时候maven工程会毛病多多,老是出现找不到jar包的问题
找到c盘这里来
ssm整合的增删改查

在进去
ssm整合的增删改查

<!-- 阿里云*仓库-->
在setting.xml中我配置的是阿里云的*仓库 跟着视频来的
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>

<!-- 设置jdk编译版本-->
<profile>
<id>jdk1.7</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.7</jdk>
</activation>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
</properties>
</profile>

好了 整个domo至此结束,如果你照着我这个来的时候,遇到了问题 可以给我留言
总结完毕!