用springMVC mybatis thymeleaf在数据库为library表为message中用其中的类型为string的book_id列指定对应message表中的date_time数据延长十天只用message一个表根据输入的book_id将对应书籍date_time延长十天完整代码详细过程 不能用spring boot

首先,需要创建Library和Message两个实体类,分别对应数据库中的library表和message表,代码如下:

Library.java:

public class Library {
    private int id;
    private String bookId;
    private String bookName;
    // 省略getter和setter方法
}

Message.java:

public class Message {
    private int id;
    private String bookId;
    private String dateTime;
    // 省略getter和setter方法
}

然后,在数据库中创建library和message表,并插入一些数据供测试。

接下来,需要创建对应的Mapper接口和Mapper.xml文件,用于实现对数据库的操作,代码如下:

LibraryMapper.java:

public interface LibraryMapper {
    Library selectLibraryById(int id);
    List<Library> selectAllLibrary();
}

LibraryMapper.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.example.mapper.LibraryMapper">
    <select id="selectLibraryById" resultType="com.example.entity.Library">
        select * from library where id=#{id}
    </select>
    <select id="selectAllLibrary" resultType="com.example.entity.Library">
        select * from library
    </select>
</mapper>

MessageMapper.java:

public interface MessageMapper {
    Message selectMessageByBookId(String bookId);
    void updateMessageByBookId(String bookId, String dateTime);
}

MessageMapper.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.example.mapper.MessageMapper">
    <select id="selectMessageByBookId" resultType="com.example.entity.Message">
        select * from message where book_id=#{bookId}
    </select>
    <update id="updateMessageByBookId">
        update message set date_time=#{dateTime} where book_id=#{bookId}
    </update>
</mapper>

接下来,需要创建Controller类,用于处理用户请求并调用相应的Service类进行处理,代码如下:

LibraryController.java:

@Controller
public class LibraryController {
    @Autowired
    private LibraryService libraryService;

    @GetMapping("/library")
    public String library(Model model) {
        List<Library> libraryList = libraryService.getAllLibrary();
        model.addAttribute("libraryList", libraryList);
        return "library";
    }
}

MessageController.java:

@Controller
public class MessageController {
    @Autowired
    private MessageService messageService;

    @GetMapping("/message")
    public String message(Model model) {
        List<Message> messageList = messageService.getAllMessage();
        model.addAttribute("messageList", messageList);
        return "message";
    }

    @PostMapping("/message/update")
    public String updateMessage(@RequestParam("bookId") String bookId) {
        Message message = messageService.getMessageByBookId(bookId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(message.getDateTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 10);
        String dateTime = sdf.format(calendar.getTime());
        messageService.updateMessageByBookId(bookId, dateTime);
        return "redirect:/message";
    }
}

最后,需要创建Service类,用于实现具体的业务逻辑,代码如下:

LibraryService.java:

@Service
public class LibraryService {
    @Autowired
    private LibraryMapper libraryMapper;

    public List<Library> getAllLibrary() {
        return libraryMapper.selectAllLibrary();
    }
}

MessageService.java:

@Service
public class MessageService {
    @Autowired
    private MessageMapper messageMapper;

    public List<Message> getAllMessage() {
        return messageMapper.selectAllMessage();
    }

    public Message getMessageByBookId(String bookId) {
        return messageMapper.selectMessageByBookId(bookId);
    }

    public void updateMessageByBookId(String bookId, String dateTime) {
        messageMapper.updateMessageByBookId(bookId, dateTime);
    }
}

最后,需要在Spring配置文件中配置组件扫描和视图解析器,代码如下:

applicationContext.xml:

<context:component-scan base-package="com.example.controller"/>
<context:component-scan base-package="com.example.service"/>
<context:component-scan base-package="com.example.mapper"/>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/test"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.example.mapper"/>
</bean>

<bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine"/>
    <property name="characterEncoding" value="UTF-8"/>
</bean>

<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
    <property name="templateResolver" ref="templateResolver"/>
    <property name="enableSpringELCompiler" value="true"/>
</bean>

<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
    <property name="prefix" value="classpath:/templates/"/>
    <property name="suffix" value=".html"/>
    <property name="templateMode" value="HTML5"/>
    <property name="characterEncoding" value="UTF-8"/>
</bean>

其中,mybatis-config.xml文件需要放置在classpath下,用于配置Mybatis的相关参数。

最后,需要创建相应的HTML模板文件,用于渲染页面,代码如下:

library.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>图书馆</title>
</head>
<body>
    <table>
        <thead>
        <tr>
            <th>ID</th>
            <th>书号</th>
            <th>书名</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="library : ${libraryList}">
            <td th:text="${library.id}"></td>
            <td th:text="${library.bookId}"></td>
            <td th:text="${library.bookName}"></td>
        </tr>
        </tbody>
    </table>
</body>
</html>

message.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>借阅信息</title>
</head>
<body>
    <table>
        <thead>
        <tr>
            <th>ID</th>
            <th>书号</th>
            <th>借阅时间</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="message : ${messageList}">
            <td th:text="${message.id}"></td>
            <td th:text="${message.bookId}"></td>
            <td th:text="${message.dateTime}"></td>
            <td>
                <form th:action="@{/message/update}" method="post">
                    <input type="hidden" name="bookId" th:value="${message.bookId}"/>
                    <button type="submit">延长10天</button>
                </form>
            </td>
        </tr>
        </tbody>
    </table>
</body>
</html>

完整代码如下

标签: 科技


原文地址: https://gggwd.com/t/topic/hc7G 著作权归作者所有。请勿转载和采集!