ssm_springmvc_spring

pom文件

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

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.lky</groupId>
  <artifactId>BZ17201</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>BZ17201 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
    <!-- 作用域使用provided -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.2</version>
    </dependency>

    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.4</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>BZ17201</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

web文件

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
  </context-param>
  <filter>
    <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>DispatcherServlet</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>
  </servlet>
  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.lky" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

spring文件

<?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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.lky">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
        <property name="user" value="root"/>
        <property name="password" value="1234"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

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

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:com/lky/mapper/*.xml"/>
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>

</beans>

mapper.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.lky.mapper.DoctorMapper">
    <resultMap id="doctorMapper" type="com.lky.entity.Doctor">
        <id column="doctor_id" property="doctorId"/>
        <result column="doctor_name" property="doctorName"/>
        <result column="doctor_password" property="doctorPassword"/>
    </resultMap>
    <select id="getDoctor" resultType="com.lky.entity.Doctor" resultMap="doctorMapper">
        select * from doctor where doctor_name = #{doctorName} and doctor_password = #{doctorPassword}
    </select>
    <insert id="saveDoctor" >
        insert into doctor(doctor_id,doctor_name,doctor_password) values (#{doctorId},#{doctorName},#{doctorPassword})
    </insert>
</mapper>


entity文件

package com.lky.entity;

public class Doctor {
   int doctorId;
   String doctorName;
   String doctorPassword;

    public int getDoctorId() {
        return doctorId;
    }

    public void setDoctorId(int doctorId) {
        this.doctorId = doctorId;
    }

    public String getDoctorName() {
        return doctorName;
    }

    public void setDoctorName(String doctorName) {
        this.doctorName = doctorName;
    }

    public String getDoctorPassword() {
        return doctorPassword;
    }

    public void setDoctorPassword(String doctorPassword) {
        this.doctorPassword = doctorPassword;
    }
}

mapper.java文件

package com.lky.mapper;

import com.lky.entity.Doctor;
import org.apache.ibatis.annotations.Param;

public interface DoctorMapper {
    Doctor getDoctor(@Param("doctorName") String doctorName,@Param("doctorPassword") String doctorPassword);
    void saveDoctor(@Param("doctorId") int doctorId,@Param("doctorName") String doctorName,@Param("doctorPassword") String doctorPassword);
}

service.java文件

package com.lky.service;

import com.lky.entity.Doctor;

public interface DoctorService {
    Doctor getDoctor(String doctorName,  String doctorPassword);
    void saveDoctor( int doctorId, String doctorName, String doctorPassword);
}

serviceImpl.java文件

package com.lky.service.impl;

import com.lky.entity.Doctor;
import com.lky.mapper.DoctorMapper;
import com.lky.service.DoctorService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DoctorServiceImpl implements DoctorService {
    @Autowired
    SqlSession sqlSession;

    @Override
    @Transactional(readOnly = true)  // 自读事务
    public Doctor getDoctor(String doctorName, String doctorPassword) {
        DoctorMapper mapper = sqlSession.getMapper(DoctorMapper.class);
        Doctor doctor = mapper.getDoctor(doctorName, doctorPassword);
        return doctor;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED) // 需要事务支持
    public void saveDoctor(int doctorId,String doctorName, String doctorPassword) {
        DoctorMapper mapper = sqlSession.getMapper(DoctorMapper.class);
        mapper.saveDoctor(doctorId,doctorName, doctorPassword);
    }
}

controller.java文件

package com.lky.controller;
import com.lky.entity.Doctor;
import com.lky.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("login")
public class DoctorController {

    @Autowired
    DoctorService doctorService;
    @RequestMapping("/dologin")
    public String login(String doctorName, String doctorPassword, Map<String, Object> map, HttpSession session) {
        if (doctorName == null || "".equals(doctorName)) {
            map.put("error","医生名为空");
            return "index";
        }

        if (doctorPassword == null || "".equals(doctorPassword)) {
            map.put("error","密码为空");
            return "index";
        }

        Doctor doctor = doctorService.getDoctor(doctorName, doctorPassword);
        if (doctor == null){
            map.put("error", "医生名或密码错误");
            return "index";
        }

        session.setAttribute("doctorName", doctorName);
        return "redirect:../loginsuccess.jsp";
    }

    @RequestMapping("/register")
    public String register() {
        return "register";
    }

    @RequestMapping("/doRegister")
    public String doRegister(int doctorId,String doctorName, String doctorPassword, Map<String, Object> map, HttpSession session) {

        if ("".equals(doctorId)) {
            map.put("error","编号为空");
            return "register";
        }

        if (doctorName == null || "".equals(doctorName)) {
            map.put("error","医生名为空");
            return "register";
        }

        if (doctorPassword == null || "".equals(doctorPassword)) {
            map.put("error","密码为空");
            return "register";
        }


        doctorService.saveDoctor(doctorId, doctorName, doctorPassword);
        return "redirect:../index.jsp";
    }
}

index.jsp文件

<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录页面</title>
<style>
    * { font: 13px/1.5 '微软雅黑'; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; -box-sizing: border-box; padding:0; margin:0; list-style:none; box-sizing: border-box; }
    body, html { height:100%; overflow:hidden; }
    body { background:#93defe; background-size: cover; }
    a { color:#27A9E3; text-decoration:none; cursor:pointer; }
    img{ border:none;}
    .login_box{ width:1100px; margin:120px auto 0;}
    .login_box .login_l_img{ float:left; width:432px; height:440px; margin-left:50px;}
    .login_box .login_l_img img{width:500px; height:440px; }
    .login {height:460px; width:400px; padding:50px; background-color: #ffffff;border-radius:6px;box-sizing: border-box; float:right; margin-right:50px; position:relative; margin-top:50px;}
    .login_logo{ width:120px; height:120px; border:5px solid #93defe;border-radius:100px; background:#fff; text-align:center; line-height:110px; position:absolute; top:-60px; right:140px;}
    .login_name{ width:100%; float:left; text-align:center; margin-top:20px;}
    .login_name p{ width:100%; text-align:center; font-size:18px; color:#444; padding:10px 0 20px;}
    .login_logo img{ width:60px; height:60px;display: inline-block; vertical-align: middle;}
    input[type=text], input[type=file], input[type=password], input[type=email], select { border: 1px solid #DCDEE0; vertical-align: middle; border-radius: 3px; height: 50px; padding: 0px 16px; font-size: 14px; color: #555555; outline:none; width:100%;margin-bottom: 15px;line-height:50px; color:#888;}
    input[type=text]:focus, input[type=file]:focus, input[type=password]:focus, input[type=email]:focus, select:focus { border: 1px solid #27A9E3; }
    input[type=submit], input[type=button] { display: inline-block; vertical-align: middle; padding: 12px 24px; margin: 0px; font-size:16px; line-height: 24px; text-align: center; white-space: nowrap; vertical-align: middle; cursor: pointer; color: #ffffff; background-color: #27A9E3; border-radius: 3px; border: none; -webkit-appearance: none; outline:none; width:100%; }
    #password_text{border: 1px solid #DCDEE0; vertical-align: middle; border-radius: 3px; height: 50px; padding: 0px 16px; font-size: 14px; color: #888; outline:none; width:100%;margin-bottom: 15px;display: block; line-height:50px;}
</style>
</head>
<body>
<div class="login_box">
    <%--<div class="login_l_img"><img src="images/login-img.png" /></div>--%>
    <div class="login">
        <div class="login_logo">
            <%--<a href="#"><img src="images/login_logo.png" /></a>--%>
        </div>
        <div class="login_name">
            <p>后台管理系统</p>
        </div>
        <form action="/login/dologin" method="post">
            用户名<input name="doctorName" type="text"  />
            密码<input name="doctorPassword" type="password" id="password" />
            <input value="登录" style="width:100%;" type="submit">
            <br><br>
            <input type="button" value="注册" onclick="register();">
        </form>
        <div class="error" style="padding-left: 25px;color: red">${error}</div>
    </div>
</div>

<script type="text/javascript">
    function register() {
        window.location.href="/login/register";
    }
</script>
</body>
</html>

register.jsp文件

<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录页面</title>
    <style>
        * { font: 13px/1.5 '微软雅黑'; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; -box-sizing: border-box; padding:0; margin:0; list-style:none; box-sizing: border-box; }
        body, html { height:100%; overflow:hidden; }
        body { background:#93defe; background-size: cover; }
        a { color:#27A9E3; text-decoration:none; cursor:pointer; }
        img{ border:none;}
        .login_box{ width:1100px; margin:120px auto 0;}
        .login_box .login_l_img{ float:left; width:432px; height:440px; margin-left:50px;}
        .login_box .login_l_img img{width:500px; height:440px; }
        .login {height:460px; width:400px; padding:50px; background-color: #ffffff;border-radius:6px;box-sizing: border-box; float:right; margin-right:50px; position:relative; margin-top:50px;}
        .login_logo{ width:120px; height:120px; border:5px solid #93defe;border-radius:100px; background:#fff; text-align:center; line-height:110px; position:absolute; top:-60px; right:140px;}
        .login_name{ width:100%; float:left; text-align:center; margin-top:20px;}
        .login_name p{ width:100%; text-align:center; font-size:18px; color:#444; padding:10px 0 20px;}
        .login_logo img{ width:60px; height:60px;display: inline-block; vertical-align: middle;}
        input[type=text], input[type=file], input[type=password], input[type=email], select { border: 1px solid #DCDEE0; vertical-align: middle; border-radius: 3px; height: 50px; padding: 0px 16px; font-size: 14px; color: #555555; outline:none; width:100%;margin-bottom: 15px;line-height:50px; color:#888;}
        input[type=text]:focus, input[type=file]:focus, input[type=password]:focus, input[type=email]:focus, select:focus { border: 1px solid #27A9E3; }
        input[type=submit], input[type=button] { display: inline-block; vertical-align: middle; padding: 12px 24px; margin: 0px; font-size:16px; line-height: 24px; text-align: center; white-space: nowrap; vertical-align: middle; cursor: pointer; color: #ffffff; background-color: #27A9E3; border-radius: 3px; border: none; -webkit-appearance: none; outline:none; width:100%; }
        #password_text{border: 1px solid #DCDEE0; vertical-align: middle; border-radius: 3px; height: 50px; padding: 0px 16px; font-size: 14px; color: #888; outline:none; width:100%;margin-bottom: 15px;display: block; line-height:50px;}
    </style>
</head>
<body>
<div class="login_box">
    <%--<div class="login_l_img"><img src="images/login-img.png" /></div>--%>
    <div class="login">
        <div class="login_logo">
            <%--<a href="#"><img src="images/login_logo.png" /></a>--%>
        </div>
        <div class="login_name">
            <p>后台管理系统</p>
        </div>
        <form action="/login/dologin" method="post">
            编号:<input type="text" name="doctor_id" >
            医生名:<input name="doctorName" type="text"   >
            密码:<input name="doctorPassword" type="password" />
            <input value="注册" style="width:100%;" type="submit">
        </form>
        <div class="error" style="padding-left: 25px;color: red">${error}</div>
    </div>
</div>
</body>
</html>

longinsuccess.jsp文件

<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2019/12/13
  Time: 11:05
  To change this template use File | Settings | File Templates.
--%>
<%@  page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
欢迎${doctor_name}回来
</body>
</html>

SSM项目是一种基于Spring+SpringMVC+MyBatis框架的软件开发模式。这种模式集合了Spring的IOC和AOP特性,SpringMVC的Web开发能力以及MyBatis的ORM技术,使得项目在开发过程中更加高效和简便。 在SSM项目中,Spring作为核心容器,负责管理各个Bean的生命周期,并提供了强大的依赖注入功能,降低了代码耦合性,增加了代码的可维护性。Spring还通过AOP技术提供了事务管理、日志记录、权限控制等一系列的切面功能,使得开发过程更加灵活和可控。 SpringMVC是基于MVC设计模式的Web框架,它提供了统一的请求处理流程和各个层级之间的数据交互机制。通过注解和配置文件的方式,开发者可以定义Controller、视图解析器、拦截器等组件,实现对HTTP请求的响应和处理。 MyBatis是一种轻量级的持久层框架,它提供了ORM(对象关系映射)功能,通过简单的XML或注解配置,将Java对象和数据库表之间建立映射关系。MyBatis还提供了丰富的SQL映射、分页查询、缓存机制等功能,能够极大地简化数据库操作的代码量。 SSM项目开发主要包括以下几个步骤:首先,根据项目需求设计数据库表结构,并创建相应的实体类。然后,编写MyBatis的Mapper接口和SQL语句,实现对数据库的操作。接着,编写Service接口和实现类,在其中调用Mapper接口完成业务逻辑的处理。最后,编写Controller类,处理用户请求,并调用Service层方法返回结果给前端页面。 总之,SSM项目结合了SpringSpringMVC和MyBatis的优点,可以有效地提高开发效率和代码质量。它在企业级应用开发中得到了广泛应用,是一种成熟和稳定的开发模式。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值