计算机系统应用教程网站

网站首页 > 技术文章 正文

Spring+SpringMVC+Mybatis通过注解实现文章管理

btikc 2024-09-25 15:01:40 技术文章 18 ℃ 0 评论

在Java开发工程中,注解和XML混合使用已成为很多人和公司的首选了。

今天,我们来看一个功能模块,是本人小长假时,亲自动手实验的。当时想使用Spring+SpringMVC+Mybatis以注解的形式做一个人博客的,后来做了两个模块,实在是没有时间,就搁置了。目前实现了,对文章类型的管理和对文章的管理,这两个模块前段使用的时layui。现在,我们来看一下,这个项目的基本架构如图1。

图1

看完架构了,我来看一下具体的配置吧,首先看一下web.xml的配置。

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://xmlns.jcp.org/xml/ns/javaee"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

id="WebApp_ID" version="3.1">

<display-name>springmvc</display-name>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:application*.xml</param-value>

</context-param>

<servlet>

<description>springMVC核心控制器</description>

<servlet-name>servletDispatcher</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>servletDispatcher</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<filter>

<filter-name>encodingFilter</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>

<init-param>

<param-name>forceEncoding</param-name>

<param-value>true</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

这里面主要配置的是Spring的监听,SpringMvc的核心控制器,目的就是项目启动时就能启动这两个。还有配置的就是防止项目出现乱码的过滤器,统一编码。

接着看一下applicationContext.xml。

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.3.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd

http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">

<!--扫描这个包下所有接口当作mapper的配置,之后可以自动引入mapper类 -->

<mybatis:scan base-package="com.rw.mapper"/>

<!--扫描有spring相关注解的类,把这些类注册spring的bean -->

<context:component-scan base-package="com.rw"></context:component-scan>

<!--使用 propertyOverrideConfigurer后处理器加载数据源参数 -->

<context:property-override location="classpath:db.properties"/>

<!-- 配置c3p0 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"></bean>

<!--配置session工厂 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource"></bean>

<!--jdbc事物管理器 -->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"

p:dataSource-ref="dataSource"></bean>

<!-- 启用支持annotation注解方式事务管理 -->

<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

再看一下springmvc.xml的详情。这个主要时配置SpringMvc的。

<?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:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

<!--扫描controller -->

<context:component-scan base-package="com.rw"></context:component-scan>

<!--默认配置方案 -->

<mvc:annotation-driven></mvc:annotation-driven>

<bean id="viewResolver"

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/"></property>

<property name="suffix" value=".jsp"></property>

</bean>

<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>

<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>

<mvc:resources location="/img/" mapping="/img/**"></mvc:resources>

<bean id="multipartResolver"

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<!--设置上传最大尺寸为5MB -->

<property name="maxUploadSizePerFile" value="5242880" />

<property name="resolveLazily" value="true" />

</bean>

</beans>

看完这些配置,我们具体看一个方法是怎么用注解实现的。先看一下接口如图2

图2

再看一下具体的方法如图3

图3

具体的数据库交互如图4

图4

实现的效果如下图:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表