【Web技术】(实验三)基于 Spring+SpringMVC+Mybaits 的 web 系统设计·二


一. 数据库设计

登录、注册使用数据库mytest中的一个表admin,进行用户管理时使用另一个表student,(其实,在进行用户管理时可以直接获取用户的注册信息,只用一张表就完事了),另外这里的用户密码用的是明文存储,这样会有问题,可以利用一些其他技术进行加密存储,表格结构设计如下:
在这里插入图片描述
在这里插入图片描述

二.后端代码实现

首先搭建的是maven结构的web项目,上篇已介绍,对pom.xml文件里应下的依赖包依次为:

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.3.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>4.3.5.RELEASE</version>
    </dependency>
    <!--Spring transaction-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.3.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.3.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-mock</artifactId>
      <version>2.0.8</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.3.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>4.3.5.RELEASE</version>
    </dependency>
    <!--Spring Web + Spring MVC-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.1.RELEASE</version>
    </dependency>
    <!--mysql jdbc-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.27</version>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.17</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.22</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>
    <!--json serialize and deserialization-->
    <!-- 引入fastjson依赖 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.12</version>
    </dependency>
    <!-- 引入gson依赖 -->
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.6.2</version>
    </dependency> 
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.6.2</version>
    </dependency>
    <dependency>
      <groupId>org.jetbrains</groupId>
      <artifactId>annotations-java5</artifactId>
      <version>RELEASE</version>
    </dependency>
    <!--mybatis-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.3.0</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.2.3</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.13.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.13.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.13.1</version>
    </dependency>
    <dependency>
        <groupId>com.thetransactioncompany</groupId>
        <artifactId>cors-filter</artifactId>
        <version>2.5</version>
    </dependency>

然后是spring-servlet.xml配置文件的主要内容:

<mvc:annotation-driven >
</mvc:annotation-driven>
    <!-- 启动包扫描功能,以便注册带有@Controllers、@service、@repository、@Component等注解的类成为spring的bean -->
<context:component-scan base-package="com.homework.Controller" />

接着是applicationContext.xml配置文件:

    <context:annotation-config />
    <!-- 配置component所在的包,自动加载需要管理的Bean -->
    <context:component-scan base-package="com.homework.Service" />
    <context:component-scan base-package="com.homework.Dao"/>
    <import resource="spring-mybatis.xml" />

对于web.xml文件的关键代码:

<!-- Spring MVC配置 -->
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--Spring-servlet.xml config-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-servlet.xml</param-value>
    </init-param>
    <!-- load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法) -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <filter>
    <filter-name>corsFilter</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>corsFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--spring listener config-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

配置数据库的文件jdbc.properties:

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名?serverTimezone=UTC&characterEncoding=utf-8&useSSL=false
jdbc.username=本人用户名
jdbc.password=本人密码

最后是spring-mybatis.xml配置文件:

<bean id="configProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
            </list>
        </property>
    </bean>

    <!-- 配置数据源 -->
    <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource" destroy-method="close">
        <property value="${jdbc.driver}" name="driverClassName"/>
        <property value="${jdbc.url}" name="url"/>
        <property value="${jdbc.username}" name="username"/>
        <property value="${jdbc.password}" name="password"/>
    </bean>

    <!-- 配置mybatisSqlSessionFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
    <!-- 配置mybatis mapper接口 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.homework.Dao"/>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

用到的Mapper映射文件:

<!--AdminMapper.xml-->
<mapper namespace="com.homework.Dao.AdminMapper">
    <resultMap id="BaseResultMap" type="com.homework.Entity.Admin">
        <id column="admin_id" jdbcType="BIGINT" property="adminId" />
        <result column="admin_name" jdbcType="VARCHAR" property="adminName" />
        <result column="admin_password" jdbcType="VARCHAR" property="adminPassword" />
        <result column="admin_email" jdbcType="VARCHAR" property="adminEmail" />
        <result column="admin_date" jdbcType="VARCHAR" property="adminDate" />
    </resultMap>

    <insert id="addAdmin">
        insert into admin(admin_name, admin_password, admin_email, admin_date)
        VALUES(#{adminName},#{adminPassword},#{adminEmail},#{adminDate})
    </insert>

    <update id="updatePassword">
        update admin set admin_password = #{adminPassword} where admin_id = #{adminId}
    </update>

    <select id="selectByNameAndPwd" resultMap="BaseResultMap">
        select * from admin where admin_name = #{adminName} and admin_password = #{adminPassword}
    </select>

    <select id="selectByAdminName" resultMap="BaseResultMap">
        select * from admin where admin_name = #{adminName}
    </select>
<!--StudentMapper.xml-->
<mapper namespace="com.homework.Dao.StudentMapper">
    <resultMap id="BaseResultMap" type="com.homework.Entity.Student">
        <id column="student_id" jdbcType="BIGINT" property="studentId" />
        <result column="student_name" jdbcType="VARCHAR" property="studentName" />
        <result column="student_password" jdbcType="VARCHAR" property="studentPassword" />
        <result column="student_email" jdbcType="VARCHAR" property="studentEmail" />
        <result column="student_balance" jdbcType="INTEGER" property="studentBalance" />
        <result column="student_birthday" jdbcType="VARCHAR" property="studentBirthday" />
    </resultMap>
    <update id="updateById" parameterType="com.homework.Entity.Student">
        update student set student_name = #{studentName},student_email = #{studentEmail},student_password = #{studentPassword},student_birthday = #{studentBirthday},student_balance = #{studentBalance} where student_id = #{studentId}
    </update>

    <delete id="deleteById">
        delete from student where student_id = #{studentId}
    </delete>

    <select id="list" resultMap="BaseResultMap">
       select * from student
    </select>

    <select id="listStudentLikeInfo" resultMap="BaseResultMap">
        select * from student where student_name like concat('%',#{info},'%')
    </select>

接着步入正题:
编写控制器(Controller)包、实体(Entity)包、服务(service)包、Dao包,这里不再赘述,获取完整代码移步:点我领取

三.结构设计介绍

①pom配置文件加入mybatis相关依赖(mysql驱动、mybatis、Mybatis spring插件)
②在Spring的配置文件中配置mybatis (定义数据源、配置Mybatis)
③增加dao层,采用Mybatis访问数据库(作用为编写实体类、定义接口文件、定义mapper映射文件)
④实体类采用实验二的即可,里面内容与数据库里的列属性名尽量保持一致
⑤接口文件,就是CRUD操作,对于一张表单,有增、删、改查,以及查询列表等5个基本动作,也可以根据实际需求增加新的操作
⑥映射文件的每个sql语句的id一定和接口文件中的方法保持一致,可以配置参数和返回值的类型
⑦在service层包含两个部分,service的接口定义和service的实现,这里再增加更多的业务功能
⑧contrller层内容,进行补充完整,返回类型都设置成JSONObject类型
⑨再对前端文件进行修改:
方法一:使用ajax语句
$.ajax({ url:"http://localhost:8080/SSM_Demo/admin/login, type:“POST”, data:jsonstr, dataType:‘json’, success:function(result){ **************}
方法二:使用vue编写前端
详情请见:点我领取

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值