使用SSM框架实现增删改查
Posted yangshuyuan1009
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用SSM框架实现增删改查相关的知识,希望对你有一定的参考价值。
一、目录结构:
二、数据库
数据库设计,数据脚本下载:https://download.csdn.net/download/qq_41879385/10696480,因为博客已经没有免费下载的功能了,所以C币只选最少的,1个就行。
三、项目开始
在pojo包中创建Novel.java(代表novel表)、Coltype.java(代表coltype),当然,里面的字段、类型跟这两张表是一样的。
但是我还要再加一张表:PageNovel.java,这个是分页的表,呃,实体类的话大家都会,我这里就不在贴出来了。贴出一个分页的类:
PageNovel.java:
package cn.item.pojo;
public class PageNovel
private String novelName;
private String novelColumn;
private Integer page = 1;
private Integer start;
private Integer size = 10;
public String getNovelName()
return novelName;
public void setNovelName(String novelName)
this.novelName = novelName;
public String getNovelColumn()
return novelColumn;
public void setNovelColumn(String novelColumn)
this.novelColumn = novelColumn;
public Integer getPage()
return page;
public void setPage(Integer page)
this.page = page;
public Integer getStart()
return start;
public void setStart(Integer start)
this.start = start;
public Integer getSize()
return size;
public void setSize(Integer size)
this.size = size;
在Java Resources下创建一个与src同路径的文件夹config,注意包的名字
在config下创建ApplicationContext-dao.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="driverClassName" value="$jdbc.driver" />
<property name="url" value="$jdbc.url" />
<property name="username" value="$jdbc.username" />
<property name="password" value="$jdbc.password" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- mapper配置 -->
<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
</bean>
<!-- 配置Mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.item.dao"/>
</bean>
</beans>
ApplicationContext-service.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- @Service扫描 -->
<context:component-scan base-package="cn.item.service"></context:component-scan>
</beans>
ApplicationContext-trans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* cn.item.service.*.*(..))" />
</aop:config>
</beans>
db.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/你的表名写这?characterEncoding=utf-8
jdbc.username=root
jdbc.password=你的密码写这
log4j.properties:
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
SpringMvc.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:dubbo="http://code.alibabatech.com/schema/dubbo" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 引入字典资源文件 -->
<context:property-placeholder location="classpath:resource.properties"/>
<!-- @Controller注解扫描 -->
<context:component-scan base-package="cn.item.controller"></context:component-scan>
<!-- 注解驱动:
替我们显示的配置了最新版的注解的处理器映射器和处理器适配器 -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<!-- 配置视图解析器
作用:在controller中指定页面路径的时候就不用写页面的完整路径名称了,可以直接写页面去掉扩展名的名称
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 真正的页面路径 = 前缀 + 去掉后缀名的页面名称 + 后缀 -->
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置自定义转换器
注意: 一定要将自定义的转换器配置到注解驱动上
-->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
</bean>
</beans>
SqlMapConfig.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>
</configuration>
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" id="WebApp_ID" version="3.0">
<display-name>NovelBook</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
<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:SpringMvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.action</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>
以上的是配置文件,其实我都是直接复制的,然后在修改一下里面的一些包名。注意包名
业务层:service:
NovelService.java:
package cn.item.service;
import java.util.List;
import cn.item.pojo.Coltype;
import cn.item.pojo.Novel;
import cn.item.pojo.PageNovel;
public interface NovelService
public List<Coltype> findDictByCode(String code);
public List<Novel> findCustomerByVo(PageNovel vo);
public Integer findCustomerByVoCount(PageNovel vo);
public Novel findNovelById(Long id);
public void updateNovelById(Novel novel);
public void deleteNovelById(long id);
public void insertNovelById(Novel novel);
NovelServiceImpl.java:
package cn.item.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.item.dao.DictMapper;
import cn.item.dao.NovelMapping;
import cn.item.pojo.Coltype;
import cn.item.pojo.Novel;
import cn.item.pojo.PageNovel;
@Service
public class NovelServiceImpl implements NovelService
@Autowired
private DictMapper dictMapper;
@Autowired
private NovelMapping novelMapping;
@Override
public List<Coltype> findDictByCode(String code)
List<Coltype> list = dictMapper.findDictByCode(code);
return list;
@Override
public List<Novel> findCustomerByVo(PageNovel vo)
List<Novel> list = novelMapping.findCustomerByVo(vo);
return list;
@Override
public Integer findCustomerByVoCount(PageNovel vo)
Integer count = novelMapping.findCustomerByVoCount(vo);
return count;
@Override
public Novel findNovelById(Long id)
Novel novel = novelMapping.findNovelById(id);
return novel;
@Override
public void updateNovelById(Novel novel)
novelMapping.updateNovelById(novel);
@Override
public void deleteNovelById(long id)
novelMapping.deleteNovelById(id);
@Override
public void insertNovelById(Novel novel)
novelMapping.insertNovelById(novel);
数据访问层:dao,也有人用Mapping,都可以:
NovelMapping.java:是小说主表的sql语句的方法,方法名是与service相同的:
package cn.item.dao;
import java.util.List;
import cn.item.pojo.Novel;
import cn.item.pojo.PageNovel;
public interface NovelMapping
public List<Novel> findCustomerByVo(PageNovel vo);
public Integer findCustomerByVoCount(PageNovel vo);
public Novel findNovelById(Long id);
public void updateNovelById(Novel novel);
public void deleteNovelById(long id);
public void insertNovelById(Novel novel);
接下就写映射文件:NovelMapping.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="cn.item.dao.NovelMapping">
<sql id="novel_where">
<where>
<if test="novelName !=null and novelName !=‘‘ ">
and a.novel_name LIKE ‘%$novelName%‘
</if>
<if test="novelColumn !=null and novelColumn !=‘‘ ">
and a.novel_column=#novelColumn
</if>
</where>
</sql>
<select id="findCustomerByVo" parameterType="cn.item.pojo.PageNovel" resultType="cn.item.pojo.Novel">
SELECT a.`novel_id`,a.`novel_name`,a.`author_name`,a.`author_Introduction`,
a.`novel_content`,b.`col_name` novel_column
FROM `novel` a
LEFT JOIN `coltype` b ON a.`novel_column` = b.`col_id`
<include refid="novel_where"></include>
LIMIT #start,#size
</select>
<select id="findCustomerByVoCount" parameterType="cn.item.pojo.PageNovel" resultType="int">
SELECT COUNT(*)
FROM novel a
LEFT JOIN coltype c ON a.novel_column = c.col_id
<include refid="novel_where"></include>
</select>
<select id="findNovelById" parameterType="long" resultType="cn.item.pojo.Novel">
SELECT * FROM novel WHERE novel_id=#id
</select>
<update id="updateNovelById" parameterType="cn.item.pojo.Novel">
UPDATE novel
<set>
<if test="novel_name !=null and novel_name !=‘‘ ">
novel_name=#novel_name,
</if>
<if test="novel_column !=null and novel_column !=‘‘ ">
novel_column=#novel_column,
</if>
<if test="author_name !=null and author_name !=‘‘ ">
author_name=#author_name,
</if>
<if test="author_Introduction !=null and author_Introduction !=‘‘ ">
author_Introduction=#author_Introduction,
</if>
<if test="novel_content !=null and novel_content !=‘‘ ">
novel_content=#novel_content,
</if>
</set>
WHERE novel_id=#novel_id
</update>
<delete id="deleteNovelById" parameterType="long">
delete from novel where novel_id =#id
</delete>
<insert id="insertNovelById" parameterType="cn.item.pojo.Novel">
INSERT INTO `novel`(`novel_name`,`novel_column`,`author_name`,`author_Introduction`,`novel_content`)
VALUE(#novel_name,#novel_column,#author_name,#author_Introduction,#novel_content);
</insert>
</mapper>
接下就写DictMapper.java,这个类是为了下拉做的:
package cn.item.dao;
import java.util.List;
import cn.item.pojo.Coltype;
public interface DictMapper
public List<Coltype> findDictByCode(String code);
和DictMapper.java的映射文件:DictMapper.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="cn.item.dao.DictMapper">
<select id="findDictByCode" parameterType="string" resultType="cn.item.pojo.Coltype">
select * from coltype a WHERE a.col_enable=1 AND a.col_code=#code ORDER BY a.col_sort
</select>
</mapper>
那么,实体类、数据访问层、业务层都写好了,接下可以写控制层:NovelController.java:
package cn.item.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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 cn.itcast.utils.Page;
import cn.item.dao.NovelMapping;
import cn.item.pojo.Coltype;
import cn.item.pojo.Novel;
import cn.item.pojo.PageNovel;
import cn.item.service.NovelService;
@Controller
@RequestMapping("/novel")
public class NovelController
@Resource
private NovelMapping novelMapping;
@Autowired
private NovelService novelService;
@Value("$novel.col.name")
private String column;
@RequestMapping("/list")
public String list(PageNovel vo,Model model) throws Exception
//小说类型
List<Coltype> columnList=novelService.findDictByCode(column);
if(vo.getNovelName()!=null)
vo.setNovelName(new String(vo.getNovelName().getBytes("iso8859-1"),"utf-8"));
if(vo.getPage()==null)
vo.setPage(1);
vo.setStart((vo.getPage() - 1) * vo.getSize());
//查询数据库的全部数据
List<Novel> resultList = novelService.findCustomerByVo(vo);
Integer count = novelService.findCustomerByVoCount(vo);
Page<Novel> page= new Page<Novel>();
page.setTotal(count); //总条数
page.setSize(vo.getSize()); //每页显示条数
page.setPage(vo.getPage()); //总页数
page.setRows(resultList); //分页的数据
model.addAttribute("page", page);
//下拉菜单
model.addAttribute("fromType", columnList);
model.addAttribute("novelName", vo.getNovelName());
model.addAttribute("novelcolumn", vo.getNovelColumn());
return "novel";
@RequestMapping("/detail")
@ResponseBody
public Novel detail(Long id) throws Exception
Novel novel = novelService.findNovelById(id);
return novel;
@RequestMapping("/update")
public String update(Novel novel) throws Exception
novelService.updateNovelById(novel);
return "novel";
@RequestMapping("/delete")
public String delete(long id) throws Exception
novelService.deleteNovelById(id);
return "novel";
@RequestMapping("/insert")
public String insert(Novel novel) throws Exception
novelService.insertNovelById(novel);
return "novel";
嗯,控制层就这么一个:接下来可以写jsp了。
第一步,在WEB-INF下创建jsp文件夹,里面创建novel.jsp,在SpringMvc.xml中已经配置完整视图解析器了。
在novel.jsp头部创建c标签:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
下面是完整的jsp页面代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>网站</title>
<style type="text/css">
#qrcode-wrapper
position:absolut;
left:80px;
top:500px;
</style>
<!-- Bootstrap Core CSS -->
<link href="<%=basePath%>css/bootstrap.min.css" rel="stylesheet">
<!-- MetisMenu CSS -->
<link href="<%=basePath%>css/metisMenu.min.css" rel="stylesheet">
<!-- DataTables CSS -->
<link href="<%=basePath%>css/dataTables.bootstrap.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="<%=basePath%>css/sb-admin-2.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="<%=basePath%>css/font-awesome.min.css" rel="stylesheet"
type="text/css">
<link href="<%=basePath%>css/boot-crm.css" rel="stylesheet"
type="text/css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn‘t work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div id="wrapper">
<div id="qrcode-wrapper" beoder="1">
<div id="qrcodeCanvas"></div>
</div>
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h1 class="page-wrapper">小说书籍网</h1>
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
<div class="panel panel-default">
<div class="panel-body">
<form class="form-inline" action="$pageContext.request.contextPath /novel/list.action" method="get">
<div class="form-group">
<label for="novelauthorname">小说名称</label>
<input type="text" class="form-control" id="novelauthorname" value="$novelName" name="novelName"></input>
</div>
<div class="form-group">
<label for="novelColumnFrom">选择小说类型</label>
<select class="form-control" id="novelColumnFrom" name="novelColumn">
<option value="">--请选择--</option>
<c:forEach items="$fromType" var="item">
<option value="$item.col_id"<c:if test="$item.col_id == novelColumn"> selected</c:if>>$item.col_name </option>
</c:forEach>
</select>
</div>
<button type="submit" class="btn btn-primary">查询</button>
<div class="btn btn-primary" data-toggle="modal" data-target="#novelInsert" onclick="editNovel($row.zeng_id)">新增小说</div>
</form>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">小说书籍网</div>
<!-- /.panel-heading -->
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>小说名称</th>
<th>所属栏目</th>
<th>小说作者</th>
<th>作者简介</th>
<th>小说摘要</th>
<th>操作</th>
</tr>
<!-- </thead>-->
<tbody>
<c:forEach items="$page.rows" var="row">
<tr>
<td>$row.novel_id</td>
<td>$row.novel_name</td>
<td>$row.novel_column</td>
<td>$row.author_name</td>
<td>$row.author_Introduction</td>
<td>$row.novel_content</td>
<td>
<a href="#" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#novelUpdate" onclick="editNovel($row.novel_id)">修改</a>
<a href="#" class="btn btn-danger btn-xs" onclick="deleteNovel($row.novel_id)">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<div class="col-md-12 text-right">
<itcast:page url="$pageContext.request.contextPath /novel/list.action" />
</div>
<!-- /.panel-body -->
</div>
<!-- /.panel -->
</div>
<!-- /.col-lg-12 -->
</div>
</div>
<!-- /#page-wrapper -->
</div>
<!-- 修改小说对话框 -->
<div class="modal fade" id="novelUpdate" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<!-- 对话框头部信息 -->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel">修改小说图书信息</h4>
</div>
<!-- 对话框字段 -->
<div class="modal-body">
<form class="form-horizontal" id="edit_novel_form">
<input type="hidden" id="edit_novel_id" name="novel_id"/>
<div class="form-group">
<label for="edit_novel_Name" class="col-sm-2 control-label">小说名称</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="edit_novel_Name" placeholder="小说名称" name="novel_name">
</div>
</div>
<div class="form-group">
<label for="edit_novelColumnFrom" style="float:left;padding:7px 15px 0 27px;">小说类型</label>
<div class="col-sm-10">
<select class="form-control" id="edit_novelColumnFrom" placeholder="小说类型" name="novel_column">
<option value="">--请选择--</option>
<c:forEach items="$fromType" var="item">
<option value="$item.col_id"<c:if test="$item.col_id == novelColumnFrom"> selected</c:if>>$item.col_name </option>
</c:forEach>
</select>
</div>
</div>
<div class="form-group">
<label for="edit_author_name" class="col-sm-2 control-label">小说作者</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="edit_author_name" placeholder="小说作者" name="author_name">
</div>
</div>
<div class="form-group">
<label for="edit_author_Introduction" class="col-sm-2 control-label">作者简介</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="edit_author_Introduction" placeholder="作者简介" name="author_Introduction">
</div>
</div>
<div class="form-group">
<label for="edit_novel_content" class="col-sm-2 control-label">小说内容</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="edit_novel_content" placeholder="小说内容摘要" name="novel_content">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
<button type="button" class="btn btn-primary" onclick="updateNovelById()">确定修改小说书籍信息</button>
</div>
</div>
</div>
</div>
<!-- /#wrapper -->
<!-- 添加小说对话框 -->
<div class="modal fade" id="novelInsert" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<!-- 对话框头部信息 -->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel">添加小说书籍</h4>
</div>
<!-- 对话框字段 -->
<div class="modal-body">
<form class="form-horizontal" id="edit_novel_zeng">
<input type="hidden" id="edit_novel_id" name="zeng_id"/>
<div class="form-group">
<label for="edit_novel_Name" class="col-sm-2 control-label">小说名称</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="edit_novel_Name" placeholder="小说名称" name="novel_name">
</div>
</div>
<div class="form-group">
<label for="edit_novelColumnFrom" style="float:left;padding:7px 15px 0 27px;">小说类型</label>
<div class="col-sm-10">
<select class="form-control" id="edit_novelColumnFrom" placeholder="小说类型" name="novel_column">
<option value="">--请选择--</option>
<c:forEach items="$fromType" var="item">
<option value="$item.col_id"<c:if test="$item.col_id == novelColumnFrom"> selected</c:if>>$item.col_name </option>
</c:forEach>
</select>
</div>
</div>
<div class="form-group">
<label for="edit_author_name" class="col-sm-2 control-label">小说作者</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="edit_author_name" placeholder="小说作者" name="author_name">
</div>
</div>
<div class="form-group">
<label for="edit_author_Introduction" class="col-sm-2 control-label">作者简介</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="edit_author_Introduction" placeholder="作者简介" name="author_Introduction">
</div>
</div>
<div class="form-group">
<label for="edit_novel_content" class="col-sm-2 control-label">小说内容</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="edit_novel_content" placeholder="小说内容摘要" name="novel_content">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
<button type="button" class="btn btn-primary" onclick="insertNovelById()">确定添加小说书籍</button>
</div>
</div>
</div>
</div>
<!-- /#wrapper -->
<!-- jQuery -->
<script src="<%=basePath%>js/jquery.min.js"></script>
<!-- QRCode -->
<script src="<%=basePath%>js/jquery-1.11.1.js"></script>
<script src="<%=basePath%>js/jquery.qrcode.js"></script>
<script src="<%=basePath%>js/qrcode.js"></script>
<script src="<%=basePath%>js/utf.js"></script>
<!-- Bootstrap Core javascript -->
<script src="<%=basePath%>js/bootstrap.min.js"></script>
<!-- Metis Menu Plugin JavaScript -->
<script src="<%=basePath%>js/metisMenu.min.js"></script>
<!-- DataTables JavaScript -->
<script src="<%=basePath%>js/jquery.dataTables.min.js"></script>
<script src="<%=basePath%>js/dataTables.bootstrap.min.js"></script>
<!-- Custom Theme JavaScript -->
<script src="<%=basePath%>js/sb-admin-2.js"></script>
<script type="text/javascript">
function editNovel(id)
$.ajax(
type:"get",
url:"<%=basePath%>novel/detail.action",
data:"id":id,
success:function(data)
$("#edit_novel_id").val(data.novel_id);
$("#edit_novel_name").val(data.novel_name);
$("#edit_novelColumnFrom").val(data.novel_column)
$("#edit_author_name").val(data.author_name);
$("#edit_author_Introduction").val(data.author_Introduction);
$("#edit_novel_content").val(data.novel_content)
);
function updateNovelById()
$.post("<%=basePath%>novel/update.action",$("#edit_novel_form").serialize(),function(data)
alert("小说图书书籍更新成功!");
window.location.reload();
);
function deleteNovel(id)
if(confirm(‘确实要删除该客户吗?‘))
$.post("<%=basePath%>novel/delete.action","id":id,function(data)
alert("小说删除更新成功!");
window.location.reload();
);
function insertNovelById()
$.post("<%=basePath%>novel/insert.action",$("#edit_novel_zeng").serialize(),function(data)
alert("新增小说书籍成功");
window.location.reload();
)
jQuery(‘#qrcodeCanvas‘).qrcode(
text: "https://blog.csdn.net/qq_41879385",
width: "110", //二维码的宽度
height: "110", //二维码的高度
background: "#ffffff", //二维码的后景色
foreground: "#000000", //二维码的前景色
src: "<%=basePath%>image/xingkong.jpg" //二维码中间的图片
);
</script>
</body>
</html>
运行效果:数据可能有点少,因为我之前数据库重装结果一不小心就把整个数据库的文件全部删除了,当时我真的心痛啊,编写了2年是SQL语句一下子全部没了,我叫那个心痛啊,不过还好本人数据库还算牛逼一点嘻嘻,很快有些了一个数据,呃,在我给的数据库脚本中是有添加的SQL语句的,用那些sql语句就可以添加了。
这个页面是有用到bootstrap的,在本文章的头部就有贴出bootstrap的使用教程。
增加效果:利用了bootstrap的模态框效果:
修改也是一样的:但是修改功能有个小细节,在点击修改的时候,在模态框都会显示不同数据的不同数据,什么意思呢,就比如你点8号的修改,在跳出来的模态框就回显示8号的数据信息,而点击7号的修改按钮,就会显示7号的数据信
以上是关于使用SSM框架实现增删改查的主要内容,如果未能解决你的问题,请参考以下文章