springmvc基础(3)


SSM整合开发思路

  • SpringMvc

    • 视图层、界面层、负责接收请求,显示处理结果的
    • 为Spring的子容器,可以访问Service对象
  • Spring

    • 业务层、管理service、dao,工具类对象的
  • MyBatis

  • 持久层,访问数据库的

  • 加入下列依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    </dependency>
    <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.2</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.5.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>5.2.5.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.5.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.8</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
    </dependency>
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.6</version>
    </dependency>
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.6</version>
    </dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.13</version>
    </dependency>
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.5</version>
    </dependency>
  • web.xml

    • 注册DispatcherServlet

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:conf/dispatcherServlet.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>*.do</url-pattern>
      </servlet-mapping>
    • 注册ContextLoaderListener

      1
      2
      3
      4
      5
      6
      7
      <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:conf/applicationContext.xml</param-value>
      </context-param>
      <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    • 注册字符集过滤器

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      <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>forceRequestEncoding</param-name>
      <param-value>true</param-value>
      </init-param>
      <init-param>
      <param-name>forceResponseEncoding</param-name>
      <param-value>true</param-value>
      </init-param>
      </filter>
      <filter-mapping>
      <filter-name>characterEncodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
      </filter-mapping>
  • spring配置文件applicationContext.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
    init-method="init" destroy-method="close">
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation" value="classpath:conf/mybatis.xml"></property>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    <property name="basePackage" value="com.nayuki.dao"></property>
    </bean>
    <context:component-scan base-package="com.nayuki.service"></context:component-scan>
  • jdbc.properties

    1
    2
    3
    4
    jdbc.driverClassName=com.mysql.cj.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/user?serverTimezone=UTC&useSSL=false
    jdbc.password=mysql
    jdbc.username=root
  • springmvc配置文件dispatcherServlet.xml

    1
    2
    3
    4
    5
    6
    <context:component-scan base-package="com.nayuki.controller"></context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
    </bean>
    <mvc:annotation-driven/>
  • mybatis配置文件mybatis.xml

    1
    2
    3
    4
    5
    6
    7
    8
    <configuration>
    <typeAliases>
    <package name="com.nayuki.domain"/>
    </typeAliases>
    <mappers>
    <package name="com.nayuki.dao"/>
    </mappers>
    </configuration>
  • 创建实体类Student

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    public class Student {
    private Integer id;
    private String name;
    private Integer age;
    public Integer getId() {
    return id;
    }
    public void setId(Integer id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public Integer getAge() {
    return age;
    }
    public void setAge(Integer age) {
    this.age = age;
    }
    }
  • 创建接口StudentDao

    1
    2
    3
    4
    public interface StudentDao {
    int insertStudent(Student student);
    List<Student> selectStudents();
    }
  • 创建StudentDao的mapper配置

    1
    2
    3
    4
    5
    6
    7
    8
    <mapper namespace="com.nayuki.dao.StudentDao">
    <select id="selectStudents" resultType="Student">
    select id,name,age from user order by id desc
    </select>
    <insert id="insertStudent">
    insert into user(name,age) values(#{name},#{age})
    </insert>
    </mapper>
  • 创建接口StudentService

    1
    2
    3
    4
    public interface StudentService {
    int addStudent(Student student);
    List<Student> findStudents();
    }
  • 创建类StudentServiceImpl

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    @Service
    public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentDao studentDao;
    @Override
    public int addStudent(Student student) {
    int nums=studentDao.insertStudent(student);
    return nums;
    }
    @Override
    public List<Student> findStudents() {
    return studentDao.selectStudents();
    }
    }
  • 创建类StudentController

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    @Controller
    @RequestMapping("/student")
    public class StudentController {
    @Resource
    private StudentService service;
    @RequestMapping("addStudent.do")
    public ModelAndView addStudent(Student student){
    ModelAndView modelAndView=new ModelAndView();
    String tips="注册失败";
    int nums=service.addStudent(student);
    if(nums >0){
    tips="学生["+student.getName()+"]注册成功";
    }
    modelAndView.addObject("tips",tips);
    modelAndView.setViewName("result");
    return modelAndView;
    }
    @RequestMapping("queryStudent.do")
    @ResponseBody
    public List<Student> queryStudent(){
    List<Student> students=service.findStudents();
    return students;
    }
    }
  • 编写index.jsp

    1
    2
    3
    4
    5
    6
    <form action="student/addStudent.do" method="post">
    姓名<input type="text" name="name"/>
    年龄<input type="text" name="age"/>
    <input type="submit" value="注册"/>
    </form>
    <a href="listStudent.jsp">浏览学生</a>
  • 编写listStudent.jsp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    <head>
    <title>Title</title>
    <script type="text/javascript" src="js/jquery-3.5.1.js"></script>
    <script type="text/javascript">
    $(function (){
    loadStudentData();
    $("#btnLoader").click(function (){
    loadStudentData();
    })
    })
    function loadStudentData(){
    $.ajax({
    url:"student/queryStudent.do",
    type:"get",
    dataType:"json",
    success:function (data){
    $("#info").html('');
    $.each(data,function (i,n){
    $("#info").append("<tr>")
    .append("<td>"+n.id+"</td>")
    .append("<td>"+n.name+"</td>")
    .append("<td>"+n.age+"</td>")
    .append("</tr>")
    })
    }
    })
    }
    </script>
    </head>
    <body>
    <div align="center">
    <table>
    <thead>
    <tr>
    <td>学号</td>
    <td>姓名</td>
    <td>年龄</td>
    </tr>
    </thead>
    <tbody id="info">
    </tbody>
    </table>
    <input type="button" id="btnLoader" value="查询数据">
    </div>
    </body>
  • 编写result.jsp

    1
    2
    3
    <body>
    result.jsp 结果页面,注册结果${tips}
    </body>