提要
由于博主的课程里出现冲突,故到此结束,后面会补充一下Spring事务的文章
阅读准备
在这章文章我们会分离前后端
- 前端纯静态HTML,使用到了Amaze UI | HTML5 跨屏前端框架 + layer官方演示与讲解(jQuery弹出层插件)
- 后端遵守REST规则并采用Ajax技术与前端同步数据,采用Spring框架与数据库进行交互
具体不多赘述,带大家写完全站(带数据库),相信你阅读完了本文,照着做你也可以和我做到一样的效果
本文开发环境如下
- Tomcat7.0.91,下载链接:Apache Tomcat® - Apache Tomcat 7 Software Downloads
- JDK8,下载链接:Java SE Development Kit 8 — 下载
- IDEA编辑器,下载链接:IntelliJ IDEA: The Java IDE for Professional Developers by JetBrains
- 360极速浏览器(Chrome内核),下载链接:欢迎使用360极速浏览器
- 相关Spring的Jar包,下载链接:链接:https://pan.baidu.com/s/1TxfglpVNAGjKRpcRXk8YKg
提取码:t5af
复制这段内容后打开百度网盘手机App,操作更方便哦
下载链接及开发工具And Jar包都已提供,下面直接上代码了,至于代码中的详细含义,如有不懂之处请评论,我会保持每天早上阅读CSDN的好习惯
数据库的简易构建
在这里我们设计一下数据库,按照构思及结果,我决定就建立一个表,当然这里是为了教程方便阅读也是为了更便于大家理解
数据库关键字段如下:
这里大家只需要看注释
和名
即可,其他不用关注
有编写sql语句的能力,可以参照上图结构构建数据库,如果不会,没有关系,我在下面提供了sql语句,您直接到Oracle控制台执行即可
带数据的数据库结构sql语句
创建序列
CREATE SEQUENCE "SYSTEM"."Untitled" MINVALUE 1 MAXVALUE 999999999999 INCREMENT BY 1 START WITH 1000 ORDER NOCACHE
创建表及导入数据
/*
Navicat Premium Data Transfer
Source Server : oracle
Source Server Type : Oracle
Source Server Version : 110200
Source Host : localhost:1521
Source Schema : SYSTEM
Target Server Type : Oracle
Target Server Version : 110200
File Encoding : 65001
Date: 23/11/2018 10:26:49
*/
-- ----------------------------
-- Table structure for USER_COMMITS
-- ----------------------------
DROP TABLE "SYSTEM"."USER_COMMITS";
CREATE TABLE "SYSTEM"."USER_COMMITS" (
"COMMIT_ID" NUMBER NOT NULL ,
"USER_ID" NUMBER NOT NULL ,
"USER_COMMIT" VARCHAR2(255 BYTE) DEFAULT '我是一条可爱的小评' NOT NULL ,
"USER_COMMITS_LIKE" NUMBER DEFAULT 0 NOT NULL ,
"USER_COMMITS_NOTLIKE" NUMBER DEFAULT 0 NOT NULL ,
"USER_COMMITS_CREATE_TIME" TIMESTAMP(2) DEFAULT sysdate NOT NULL
)
TABLESPACE "SYSTEM"
LOGGING
NOCOMPRESS
PCTFREE 10
INITRANS 1
STORAGE (
INITIAL 65536
NEXT 1048576
MINEXTENTS 1
MAXEXTENTS 2147483645
FREELISTS 1
FREELIST GROUPS 1
BUFFER_POOL DEFAULT
)
PARALLEL 1
NOCACHE
DISABLE ROW MOVEMENT
;
COMMENT ON COLUMN "SYSTEM"."USER_COMMITS"."COMMIT_ID" IS '主键ID';
COMMENT ON COLUMN "SYSTEM"."USER_COMMITS"."USER_ID" IS '用户ID';
COMMENT ON COLUMN "SYSTEM"."USER_COMMITS"."USER_COMMIT" IS '评论内容';
COMMENT ON COLUMN "SYSTEM"."USER_COMMITS"."USER_COMMITS_LIKE" IS '赞数';
COMMENT ON COLUMN "SYSTEM"."USER_COMMITS"."USER_COMMITS_NOTLIKE" IS '踩数';
COMMENT ON COLUMN "SYSTEM"."USER_COMMITS"."USER_COMMITS_CREATE_TIME" IS '评论时间';
-- ----------------------------
-- Records of USER_COMMITS
-- ----------------------------
INSERT INTO "SYSTEM"."USER_COMMITS" VALUES ('2', '1', '这是第二条评论', '233', '0', TO_TIMESTAMP('2018-11-23 10:25:13.00', 'SYYYY-MM-DD HH24:MI:SS:FF2'));
INSERT INTO "SYSTEM"."USER_COMMITS" VALUES ('1000', '1', '欢迎访问我的csdn博客鸭', '0', '0', TO_TIMESTAMP('2018-11-23 10:26:02.00', 'SYYYY-MM-DD HH24:MI:SS:FF2'));
INSERT INTO "SYSTEM"."USER_COMMITS" VALUES ('1', '1', '欢迎使用留言板,这里是留言内容区域', '666', '0', TO_TIMESTAMP('2018-11-23 10:24:57.00', 'SYYYY-MM-DD HH24:MI:SS:FF2'));
-- ----------------------------
-- Primary Key structure for table USER_COMMITS
-- ----------------------------
ALTER TABLE "SYSTEM"."USER_COMMITS" ADD CONSTRAINT "SYS_C007069" PRIMARY KEY ("COMMIT_ID");
-- ----------------------------
-- Checks structure for table USER_COMMITS
-- ----------------------------
ALTER TABLE "SYSTEM"."USER_COMMITS" ADD CONSTRAINT "SYS_C007070" CHECK ("USER_ID" IS NOT NULL) NOT DEFERRABLE INITIALLY IMMEDIATE NORELY VALIDATE;
ALTER TABLE "SYSTEM"."USER_COMMITS" ADD CONSTRAINT "SYS_C007071" CHECK ("USER_COMMIT" IS NOT NULL) NOT DEFERRABLE INITIALLY IMMEDIATE NORELY VALIDATE;
ALTER TABLE "SYSTEM"."USER_COMMITS" ADD CONSTRAINT "SYS_C007072" CHECK ("USER_COMMITS_LIKE" IS NOT NULL) NOT DEFERRABLE INITIALLY IMMEDIATE NORELY VALIDATE;
ALTER TABLE "SYSTEM"."USER_COMMITS" ADD CONSTRAINT "SYS_C007073" CHECK ("USER_COMMITS_NOTLIKE" IS NOT NULL) NOT DEFERRABLE INITIALLY IMMEDIATE NORELY VALIDATE;
ALTER TABLE "SYSTEM"."USER_COMMITS" ADD CONSTRAINT "SYS_C007074" CHECK ("USER_COMMITS_CREATE_TIME" IS NOT NULL) NOT DEFERRABLE INITIALLY IMMEDIATE NORELY VALIDATE;
导入后的数据
后端代码实现
首先配置Spring框架文件
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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<!--HandlerMapping-->
<mvc:annotation-driven/>
<!--开启mvc的组件扫描-->
<context:component-scan base-package="mvc.controller"/>
</beans>
SpringDao.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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<!-- 扫描所有Dao -->
<context:component-scan base-package="mvc.model.dao"/>
<!--引入db.properties配置文件-->
<util:properties id="db" location="classpath:db.properties"/>
<!--配置数据源dataSource-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="#{db.driverClassName}"/>
<property name="url" value="#{db.url}"/>
<property name="username" value="#{db.username}"/>
<property name="password" value="#{db.password}"/>
</bean>
<!--配置JDBCTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource"></constructor-arg>
</bean>
<!--开启声明式事务 transaction-manager就是事务管理器的意思,proxy-target-class如果是false代表jdk的代理机制(必须要求你的这个类实现接口),如果是true代表使用cglib的动态代理-->
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
<!--创建事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
这样做完了配置,我们需要在设置一下web.xml的内容,这样方便请求到来走到DispatcherServlet
中去
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<filter>
<filter-name>AjaxFilter</filter-name>
<filter-class>mvc.model.filter.AjaxFilter</filter-class>
</filter>
<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*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>ajaxFilter</filter-name>
<filter-class>mvc.model.filter.AjaxFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ajaxFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
书写主要逻辑
编写dao接口:UserDao
package mvc.model.dao;
import mvc.model.bean.Commit;
import java.util.List;
/**
* @author biuaxia
*/
public interface UserDao {
/**
* 通过id查询评论
*
* @param userId 用户id
* @return 当前用户的所有评论
*/
List findAllCommitByUserId(Integer userI