使用Spring+SpringMVC+Mybatis实现留言板的增删改查

1 篇文章 0 订阅

目的

1)掌握使用maven来创建SSM项目

2)掌握web.xml和pom.xml的使用

3)掌握Spring、SpringMVC和Mybatis,学会整合SSM

4)掌握的controller层注解@Autowired,@RequestMapping和@RequestParam的使用

5)用学会配置SpringMVC.xml、applicationContext.xml和mybatis-config.xml等配置文件

6)掌握spring+springMVC+mybatis 对于留言板的开发

方案:

(1)本次实验主要来学习如何使用spring+springMVC+mybatis 实现留言板增删改查, 基本功能就是实现增删改查功能,其他方面自己拓展及显示,并对验证结果进行提示。

(2)设计数据库:

创建数据库hg,并创建两个表,note表是留言管理处理;person表是登录处理;

person表:

 note表:

 步骤:

  1. 创建项目的步骤:

(1)先创建一个Maven项目(本人的项目名称:SSM_lyb),并配置环境,在pom.xml中引入相关的spring-context、spring-jdbc、spring-tx、spring-test、spring-webmvc、aspectjweaver、druid、jstl、standard、mybatis、mybatis-spring、mysql-connector-java、logback-classic、junit和javax.servlet-api。

(2)先创建数据表(person表和note表)。

(3)创建两个表对应的实体类(Person.java和Note.java)。

(4)配置mybatis的mybatis-config.xml配置文件。

(5)配置spring的applicationContext.xml配置文件。

(6)创建jdbc.properties和log4j.properties。

(7)创建Mapper.java(NoteMapper.java和PersonMapper.java)以及对应的映射文件(NoteMapper.xml和PersonMapper.xml)。

(8)配置springMVC的SpringMVC.xml配置文件

(9)再创建对应的Controller并进行编写。

(10)运行项目。


2.配置文件:

(1)配置mybatis-config.xml

(2)配置applicationContext.xml 

 (3)配置jdbc.properties

 (4)配置SpringMVC.xml

 (5)配置web.xml


3.操作步骤

  • 用户进行登录,若密码或用户名正确,可以进入登录成功的页面;若错误显示“用户或密码错误提示”(没做注册部分);
  • 进入登录成功的页面;
  • 点击进入,进入管理留言板页面,进行相关的增删改查操作;若添加留言时某一些为空,则提示“添加留言板失败”,返回管理留言板页面。

项目结构:

结果:

登录页面如下:(用户名:ah,密码:123)

想要源码   发:  Q1

  • 27
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 92
    评论
好的,以下是一个简单的 SpringMVC+Spring+MyBatis 留言板实现过程: 1. 创建数据库表 首先,我们需要创建一个数据库表,用于存储留言信息,可以创建一个名为 `message` 的表,包含以下字段: ``` id: 留言id,自增长 name: 留言人姓名 content: 留言内容 create_time: 留言时间 ``` 2. 配置数据库连接信息 在 `application.properties` 配置文件中配置数据库连接信息,例如: ``` spring.datasource.url=jdbc:mysql://localhost:3306/message_board?useSSL=false&useUnicode=true&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 3. 创建实体类 创建一个 `Message` 实体类,对应数据库表中的字段。 ```java public class Message { private Integer id; private String name; private String content; private Date createTime; // getter、setter方法省略 } ``` 4. 创建 DAO 层接口和映射文件 创建一个 `MessageMapper` 接口,并在其中定义需要实现的 DAO 层方法,例如: ```java public interface MessageMapper { List<Message> getAllMessages(); void addMessage(Message message); void deleteMessage(Integer id); } ``` 同时,在 `resources` 目录下创建一个 `mapper` 文件夹,并创建一个名为 `MessageMapper.xml` 的映射文件,实现 DAO 层方法与 SQL 语句的映射关系,例如: ```xml <mapper namespace="com.example.mapper.MessageMapper"> <resultMap id="messageMap" type="com.example.entity.Message"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="content" column="content"/> <result property="createTime" column="create_time"/> </resultMap> <select id="getAllMessages" resultMap="messageMap"> select * from message order by create_time desc </select> <insert id="addMessage"> insert into message (name, content, create_time) values (#{name}, #{content}, #{createTime}) </insert> <delete id="deleteMessage"> delete from message where id = #{id} </delete> </mapper> ``` 5. 配置 Spring Bean 在 `applicationContext.xml` 配置文件中,配置 Spring Bean,包括数据源、MyBatis 的 SqlSessionFactory、MapperScannerConfigurer 等,例如: ```xml <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${spring.datasource.driver-class-name}"/> <property name="url" value="${spring.datasource.url}"/> <property name="username" value="${spring.datasource.username}"/> <property name="password" value="${spring.datasource.password}"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="typeAliasesPackage" value="com.example.entity"/> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper"/> </bean> <bean id="messageMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.example.mapper.MessageMapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> ``` 6. 创建 Service 层接口和实现类 创建一个 `MessageService` 接口,并在其中定义需要实现的 Service 层方法,例如: ```java public interface MessageService { List<Message> getAllMessages(); void addMessage(Message message); void deleteMessage(Integer id); } ``` 同时,在 `service` 包下创建一个名为 `MessageServiceImpl` 的实现类,实现 Service 层方法,例如: ```java @Service public class MessageServiceImpl implements MessageService { @Autowired private MessageMapper messageMapper; @Override public List<Message> getAllMessages() { return messageMapper.getAllMessages(); } @Override public void addMessage(Message message) { message.setCreateTime(new Date()); messageMapper.addMessage(message); } @Override public void deleteMessage(Integer id) { messageMapper.deleteMessage(id); } } ``` 7. 创建 Controller 层 创建一个 `MessageController` 类,用于处理请求,例如: ```java @Controller @RequestMapping("/message") public class MessageController { @Autowired private MessageService messageService; @RequestMapping(value = "/list", method = RequestMethod.GET) public ModelAndView list() { ModelAndView mv = new ModelAndView(); mv.setViewName("message/list"); List<Message> messages = messageService.getAllMessages(); mv.addObject("messages", messages); return mv; } @RequestMapping(value = "/add", method = RequestMethod.GET) public ModelAndView add() { ModelAndView mv = new ModelAndView(); mv.setViewName("message/add"); mv.addObject("message", new Message()); return mv; } @RequestMapping(value = "/add", method = RequestMethod.POST) public String add(@ModelAttribute("message") Message message) { messageService.addMessage(message); return "redirect:/message/list"; } @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET) public String delete(@PathVariable("id") Integer id) { messageService.deleteMessage(id); return "redirect:/message/list"; } } ``` 8. 创建前端页面 在 `resources/templates` 目录下创建 `message` 文件夹,并创建 `list.html` 和 `add.html` 页面,例如: `list.html`: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>留言板</title> </head> <body> <h1>留言板</h1> <table> <thead> <tr> <th>姓名</th> <th>留言内容</th> <th>时间</th> <th>操作</th> </tr> </thead> <tbody> <tr th:each="message : ${messages}"> <td th:text="${message.name}"></td> <td th:text="${message.content}"></td> <td th:text="${#dates.format(message.createTime, 'yyyy-MM-dd HH:mm:ss')}"></td> <td> <a th:href="@{/message/delete/{id}(id=${message.id})}">删除</a> </td> </tr> </tbody> </table> <a th:href="@{/message/add}">添加留言</a> </body> </html> ``` `add.html`: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>添加留言</title> </head> <body> <h1>添加留言</h1> <form action="#" th:action="@{/message/add}" th:object="${message}" method="post"> <p> <label>姓名:</label> <input type="text" th:field="*{name}"> </p> <p> <label>留言内容:</label> <textarea th:field="*{content}"></textarea> </p> <button type="submit">提交</button> </form> <a href="#" onclick="javascript:history.back(-1);">返回</a> </body> </html> ``` 以上就是一个简单的 SpringMVC+Spring+MyBatis 留言板实现过程,可以根据实际需求进行修改和优化。
评论 92
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

简若*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值