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">
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 配置监听器 监听WebApplicationContext的整个生命周期 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring 配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置过滤器,只对post起作用 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
<!-- 配置SpringMVC -->
<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>*.do</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 开启peoperties配置文件的读取 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 开启注释 -->
<context:component-scan base-package="com.ai.service.impl"></context:component-scan>
<!-- 配置dataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.ai.domain"></property>
</bean>
<!-- 配置Mapper映射 (重点)-->
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ai.mapper"></property>
<property name="SqlSessionFactoryBeanName" value="factory"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="tsManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 如何进行事务控制 -->
<tx:advice id="txAdvice" transaction-manager="tsManager">
<tx:attributes>
<tx:method name="sel*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="insert*" read-only="false" rollback-for="java.lang.Exception" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="update*" read-only="false" rollback-for="java.lang.Exception" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="delete*" read-only="false" rollback-for="java.lang.Exception" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="register*" read-only="false" rollback-for="java.lang.Exception" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!-- 通过AOP完成对事务的配置 -->
<aop:config>
<aop:pointcut expression="execution(* com.ai.service.impl.*.*(..))" id="mypoint"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>
</aop:config>
</beans>
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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 开启peoperties配置文件的读取 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 开启注释 -->
<context:component-scan base-package="com.ai.service.impl"></context:component-scan>
<!-- 配置dataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.ai.domain"></property>
</bean>
<!-- 配置Mapper映射 (重点)-->
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ai.mapper"></property>
<property name="SqlSessionFactoryBeanName" value="factory"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="tsManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 如何进行事务控制 -->
<tx:advice id="txAdvice" transaction-manager="tsManager">
<tx:attributes>
<tx:method name="sel*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="insert*" read-only="false" rollback-for="java.lang.Exception" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="update*" read-only="false" rollback-for="java.lang.Exception" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="delete*" read-only="false" rollback-for="java.lang.Exception" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="register*" read-only="false" rollback-for="java.lang.Exception" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!-- 通过AOP完成对事务的配置 -->
<aop:config>
<aop:pointcut expression="execution(* com.ai.service.impl.*.*(..))" id="mypoint"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>
</aop:config>
</beans>
comment_1Mapper.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="com.ai.mapper.Comment_1Mapper">
<resultMap type="comment_1" id="myMap">
<id property="comment_1_id" column="comment_1_id"/>
<result property="comment_1_content" column="comment_1_content"/>
<result property="article_id" column="article_id"/>
<result property="user_name" column="user_name"/>
<result property="nowDate" column="nowDate"/>
<result property="photo_address" column="photo_address"/>
<collection property="list_comment_2" ofType="comment_2" select="com.ai.mapper.Comment_2Mapper.selComment_2ByComment_1_id" column="comment_1_id">
</collection>
</resultMap>
<select id="selComment_1ByArticle_id" resultMap="myMap">
select * from comment_1
<where>
<if test="#{0} !=null and #{0} !=''">
and article_id = #{0}
</if>
</where>
limit #{1},#{2}
</select>
<select id="selAllComment_1ByArticle_id" resultMap="myMap">
select * from comment_1
<where>
<if test="article_id !=null and article_id !=''">
and article_id = #{article_id}
</if>
</where>
</select>
<insert id="insertComment_1">
insert into comment_1 values(null,#{0},#{1},#{2},#{3},#{4})
</insert>
<select id="selectAllComments" resultMap="myMap">
select * from comment_1
</select>
<delete id="deleteComment">
delete from comment_1 where comment_1_id=#{0}
</delete>
<delete id="deleteComment2">
delete from comment_2 where comment_2_id=#{0}
</delete>
</mapper>
@Controller(value = "mvcDispatcherServlet")
public class MvcDispatcherServlet {
@Resource(name = "userServiceImpl")
private UserService userService;
@Resource(name = "commentServiceImpl")
private CommentService commentService;
@Resource(name = "chatServiceImpl")
private ChatService chatService;
@Resource(name = "InfoServiceImpl")
private InfoService infoService;
@RequestMapping("getArticle")
public String getArticleById(@RequestParam(defaultValue = "1") int article_id,
@RequestParam(defaultValue = "1") int page_num, Model model) throws IOException {
int size = 3;// 每页最大的评论数
// 获得请求参数的page_num(当前页面)
PageInfo_Article pageInfo_article = new PageInfo_Article();
// 存放page_num当前页面
pageInfo_article.setPage_num(page_num);
List<Comment_1> allComments = commentService.getAllRecords(article_id);
pageInfo_article.setList_comment_1(allComments);
int allRecords_1 = 0;
int allRecords_2 = 0;
for (int i = 0; i < allComments.size(); i++) {
allRecords_1++;
for (int j = 0; j < allComments.get(i).getList_comment_2().size(); j++) {
allRecords_2++;
}
}
// 存放最大记录数(评论数)
pageInfo_article.setAllRecords(allRecords_1 + allRecords_2);
int allPages = (int) Math.ceil(allRecords_1 / size);
// 存放最多的页数
pageInfo_article.setAllPages(allPages);
List<Comment_1> list_comment_1 = commentService.getComment_1ByArticle_id(article_id, page_num, size);
// 存放所有的评论
pageInfo_article.setList_comment_1(list_comment_1);
// 页面信息存放在 HttpRequest的作用域中
model.addAttribute("pageInfo_article", pageInfo_article);
return "single" + article_id;
// 为了安全将全部的JSP放在了WEB-INF的文件夹下
// 但是必须要自定义视图解析器
}
@Resource(name = "remarkServiceImpl")
private RemarkService remarkService;
@RequestMapping(value = "addRemark")
public String addRemark(Integer article_id, @RequestParam(defaultValue = "0") int comment_1_id,
String comment_content, String user_name, String photo_address, HttpServletRequest req) {
Object obj = req.getSession().getAttribute("user");
if (obj != null) {
if (article_id == null || comment_content == null || comment_content.equals("") || user_name == null
|| user_name.equals("") || photo_address.equals("") || photo_address == null) {
req.setAttribute("message", "请输入具体内容");
return "forward:getArticle.do?article_id=" + article_id;
} else {
if (comment_1_id == 0) {
// 增加的是一级评论
// 获取当前时间
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowDate = formatter.format(currentTime);
remarkService.insertComment_1(comment_content, article_id, user_name, nowDate, photo_address);
} else {
// 获取当前时间
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowDate = formatter.format(currentTime);
// 首相将这条记录增加到那个二级评论表中,并且加id
remarkService.insertComment_2(comment_content, comment_1_id, user_name, nowDate, photo_address);
}
return "forward:getArticle.do?article_id=" + article_id;
}
} else {
req.setAttribute("message", "请登录后再评论");
return "forward:login.jsp";
}
}
@RequestMapping(value = "register") // 无法解决
public String registerUser(MultipartFile photo_file, User user, HttpServletRequest req) throws IOException {
String user_name = user.getUser_name();
int count = userService.selectUserByName(user_name);
if (count <= 0) {
String originalName = photo_file.getOriginalFilename();// 获得原始的文件名
// 文件名.类型(cute.jpg)
String fileType = originalName.substring(originalName.indexOf(".") + 1);
String uuid = UUID.randomUUID().toString().replace("-", "");
String photo_address = uuid + "." + fileType;
user.setPhoto_address(photo_address);
// 上传到服务器
InputStream is = photo_file.getInputStream();
String absolutePath = req.getServletContext().getRealPath("photo");
String newFile = absolutePath + "\\" + photo_address;
File file = new File(newFile);
FileUtils.copyInputStreamToFile(is, file);
userService.registerUser(user);
req.setAttribute("message", "注册成功");
} else {
req.setAttribute("message", "用户名重复,请更换用户名");
}
return "forward:login.jsp";
}
@RequestMapping(value = "getPhoto")
@ResponseBody
public User getPhoto(String user_name) {
String photo_address = userService.getPhotoAddress(user_name);
User user = new User();
user.setPhoto_address(photo_address);
return user;
}
@RequestMapping(value = "login")
public String login(@RequestParam(defaultValue = "") String user_name,
@RequestParam(defaultValue = "") String password, @RequestParam(defaultValue = "0") int role,
HttpServletRequest req) {
if (password == null || password == "" || user_name == null || user_name == "") {
HttpSession session = req.getSession();
Object obj = session.getAttribute("user");
if(obj==null){
req.setAttribute("message", "请输入用户名/密码");
return "forward:login.jsp";
}else{
User user = (User)obj;
if (role == 0) {
// 查询是否有这个User 普通用户
User selUser = userService.selectUserByNamePwd(user.getUser_name(), user.getPassword(), role);
if (selUser != null) {
// 将查询出来的东西 放在Session中
req.getSession().setAttribute("user", selUser);
return "index";
} else {
req.setAttribute("message", "用户密码错误");
return "forward:login.jsp";
}
} else {
// 查询是否有这个User 管理员用户
User admin = userService.selectUserByNamePwd(user.getUser_name(), user.getPassword(), role);
if (admin != null) {
// 将查询出来的东西 放在Session中
req.getSession().setAttribute("user", admin);
return "admin";
} else {
req.setAttribute("message", "用户密码错误");
return "forward:login.jsp";
}
}
}
} else {
if (role == 0) {
// 查询是否有这个User 普通用户
User user = userService.selectUserByNamePwd(user_name, password, role);
if (user != null) {
// 将查询出来的东西 放在Session中
req.getSession().setAttribute("user", user);
return "index";
} else {
req.setAttribute("message", "用户密码错误");
return "forward:login.jsp";
}
} else {
// 查询是否有这个User 管理员用户
User admin = userService.selectUserByNamePwd(user_name, password, role);
if (admin != null) {
// 将查询出来的东西 放在Session中
req.getSession().setAttribute("user", admin);
return "admin";
} else {
req.setAttribute("message", "用户密码错误");
return "forward:login.jsp";
}
}
}
}