测试环境:本章系统使用SSM+layui实现各个模块,Web服务器使用Tomcat 8.5.75,数据库采用的是MySQL8.0,集成开发环境为IntelliJ IDEA 2022.1,导入项目后需要先更新maven,然后在项目结构中添加tomcat依赖,并在mysql数据库中创建7张与系统相关的数据表。
先看成品。
公告标题可以点击,点击进去后是这个样子。
再看看项目结构
别忘了还有数据库。
好的,正片开始。
- pom.xml
pom池,这里怕缺少jar包导致各种奇怪的问题就直接全部放出来了。
<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>org.example</groupId>
<artifactId>login-register</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>login-register Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>5.3.23</spring.version>
</properties>
<dependencies>
<!--前提:先导入数据库驱动依赖-->
<!--dbcp数据库连接池-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring核心ioc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--做spring事务用到的-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjtools -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.9.9.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.9.1</version>
</dependency>
<!-- Mybatis核心 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.11</version>
</dependency>
<!-- junit测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<!-- log4j日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--mybatis和spring集成的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.19</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>1.2</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.14.0</version>
</dependency>
<!-- 上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5-20081211</version>
</dependency>
</dependencies>
<build>
<finalName>login-register</finalName>
</build>
</project>
- IndexController.java
控制层,注意注解别忘记了。
package com.controller;
import com.service.IndexService;
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.RequestMethod;
import javax.servlet.http.HttpSession;
@Controller("indexController")
@RequestMapping("/index")
public class IndexController {
@RequestMapping("/Notice")
public String Notice(Model model) {
return indexService.Notice(model);
}
@RequestMapping("/selectANotice")
public String selectANotice(Model model,Integer id) {
return indexService.selectANotice(model, id);
}
}
- NoticeDao.java
package com.dao;
import com.po.Notice;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Repository("NoticeDao")
@Mapper
public interface NoticeDao {
List<Map<String, Object>> Notice();
Notice selectANotice(Integer id);
}
- Notice.java
package com.po;
public class Notice {
private Integer id;
private String ntitle;
private String ncontent;
private String ntime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNtitle() {
return ntitle;
}
public void setNtitle(String ntitle) {
this.ntitle = ntitle;
}
public String getNcontent() {
return ncontent;
}
public void setNcontent(String ncontent) {
this.ncontent = ncontent;
}
public String getNtime() {
return ntime;
}
public void setNtime(String ntime) {
this.ntime = ntime;
}
}
- IndexService.java
package com.service;
import org.springframework.ui.Model;
import javax.servlet.http.HttpSession;
public interface IndexService {
String Notice(Model model);
String selectANotice(Model model,Integer id);
String Scenic(Model model);
}
- IndexServiceImpl.java
package com.service;
import com.dao.ScenicDao;
import com.dao.NoticeDao;
import com.po.Scenic;
import com.po.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
@Service("indexService")
@Transactional
public class IndexServiceImpl implements IndexService{
@Autowired
private NoticeDao NoticeDao;
private int x;
private int x1;
@Override
public String Notice(Model model) {
List<Map<String, Object>> notice = NoticeDao.Notice();
model.addAttribute("notice", notice);
return "index";
}
public String selectANotice(Model model, Integer id) {
Notice notice = NoticeDao.selectANotice(id);
model.addAttribute("notice", notice);
return "noticeDetail";
}
}
- NoticeMapper.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.dao.NoticeDao">
<select id="Notice" resultType="com.po.Notice">
select * from noticetable order by ntime desc
</select>
<select id="selectANotice" resultType="Notice" parameterType="Integer">
select * from noticetable where id = #{id}
</select>
</mapper>
文件就是这么多,让我们来看看能不能运行。
好的,完美!