【编程不良人】快速入门SpringMVC学习笔记02---Spring+SpringMVC+MyBatis整合编程

【编程不良人】快速入门SpringMVC学习笔记02---Spring+SpringMVC+MyBatis整合编程,第1张

1.新建maven-webapp工程,添加java、resources目录及test目录下的java、resources

2.引入依赖
  
  
    org.springframework
    spring-core
    4.3.2.RELEASE
  
  
    org.springframework
    spring-context
    4.3.2.RELEASE
  
  
    org.springframework
    spring-context-support
    4.3.2.RELEASE
  
  
    org.springframework
    spring-jdbc
    4.3.2.RELEASE
  
  
    org.springframework
    spring-aop
    4.3.2.RELEASE
  
  
    org.springframework
    spring-beans
    4.3.2.RELEASE
  
  
    org.springframework
    spring-expression
    4.3.2.RELEASE
  
  
    org.springframework
    spring-aspects
    4.3.2.RELEASE
  
  
    org.springframework
    spring-tx
    4.3.2.RELEASE
  
  
    org.springframework
    spring-web
    4.3.2.RELEASE
  
  
    org.springframework
    spring-webmvc
    4.3.2.RELEASE
  
  
    javax.servlet
    servlet-api
    2.5
    provided
  
  
    jstl
    jstl
    1.2
  
  
    org.mybatis
    mybatis
    3.2.8
  
  
    org.mybatis
    mybatis-spring
    1.3.3
  
  
    mysql
    mysql-connector-java
    5.1.38
  
  
    com.alibaba
    druid
    1.1.17
  
  
    org.apache.logging.log4j
    log4j-core
    2.10.0
  
  
    org.slf4j
    slf4j-log4j12
    1.7.25
  
  
    com.alibaba
    fastjson
    1.2.62
  

3.Spring+Mybatis整合 3.1 新建表

3.2 新建实体类
  
  package com.study.entity;
  ​
  import java.util.Date;
  ​
  /**
   * @ClassName User
   * @Description TODO
   * @Author Jiangnan Cui
   * @Date 2022/4/23 10:46
   * @Version 1.0
   */
  public class User {
      private String id;
      private String name;
      private Integer age;
      private Date bir;
  ​
      @Override
      public String toString() {
          return "User{" +
                  "id='" + id + '\'' +
                  ", name='" + name + '\'' +
                  ", age=" + age +
                  ", bir=" + bir +
                  '}';
      }
  ​
      public User() {
      }
  ​
      public User(String id, String name, Integer age, Date bir) {
          this.id = id;
          this.name = name;
          this.age = age;
          this.bir = bir;
      }
  ​
      public String getId() {
          return id;
      }
  ​
      public void setId(String 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;
      }
  ​
      public Date getBir() {
          return bir;
      }
  ​
      public void setBir(Date bir) {
          this.bir = bir;
      }
  }
  ​

3.3 新建DAO接口
  
  package com.study.dao;
  ​
  import com.study.entity.User;
  ​
  import java.util.List;
  ​
  /**
   * @ClassName UserDAO
   * @Description TODO
   * @Author Jiangnan Cui
   * @Date 2022/4/23 10:45
   * @Version 1.0
   */
  public interface UserDAO {
  ​
      /**
       * @MethodName selectAllUsers
       * @Description 查询所有用户
       * @return: java.util.List
       * @Author Jiangnan Cui
       * @Date 2022/4/23 10:50
       */
      List selectAllUsers();
  ​
      /**
       * @MethodName insertUser
       * @Description 添加用户
       * @param: user
       * @Author Jiangnan Cui
       * @Date 2022/4/23 10:49
       */
      void insertUser(User user);
  ​
  }
  ​

3.4 新建Mapper配置文件
  
  
      
      
  ​
      
      
          insert into t_user values(#{id},#{name},#{age},#{bir})
      ​
  ​
  

3.5 新建Service接口
  
  package com.study.service;
  ​
  import com.study.entity.User;
  ​
  import java.util.List;
  ​
  /**
   * @ClassName UserService
   * @Description TODO
   * @Author Jiangnan Cui
   * @Date 2022/4/23 10:55
   * @Version 1.0
   */
  public interface UserService {
      /**
       * @MethodName selectAllUsers
       * @Description 查询所有用户
       * @return: java.util.List
       * @Author Jiangnan Cui
       * @Date 2022/4/23 10:50
       */
      List selectAllUsers();
  ​
      /**
       * @MethodName insertUser
       * @Description 添加用户
       * @param: user
       * @Author Jiangnan Cui
       * @Date 2022/4/23 10:49
       */
      void insertUser(User user);
  ​
  }
  ​

3.6 新建ServiceImpl实现类
  
  package com.study.service;
  ​
  import com.study.dao.UserDAO;
  import com.study.entity.User;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Propagation;
  import org.springframework.transaction.annotation.Transactional;
  ​
  import java.util.List;
  import java.util.UUID;
  ​
  /**
   * @ClassName UserServiceImpl
   * @Description TODO
   * @Author Jiangnan Cui
   * @Date 2022/4/23 10:55
   * @Version 1.0
   */
  @Service("userService")
  @Transactional
  public class UserServiceImpl implements UserService{
      @Autowired
      private UserDAO userDAO;
  ​
      @Override
      @Transactional(propagation = Propagation.SUPPORTS)
      public List selectAllUsers() {
          return userDAO.selectAllUsers();
      }
  ​
      @Override
      public void insertUser(User user) {
          user.setId(UUID.randomUUID().toString());
          userDAO.insertUser(user);
      }
  }
  ​

3.7 引入spring.xml配置文件
  
  
  
      
      
  ​
      
      
          
          
          
          
      
  ​
      
      
          
          
          
      
  ​
      
      
          
          
      
  ​
      
      
          
      
  ​
      
      

3.8 引入log4j.properties
  
  log4j.rootLogger=ERROR,stdout
  log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  log4j.appender.stdout.layout.conversionPattern=[%p] %d{yyyy-MM-dd} %m%n
  •
  log4j.logger.com.study.dao=DEBUG
  log4j.logger.org.springframework=ERROR

3.9 测试Service方法是否可以成功调用
  
  package com.study.test;
  ​
  import com.study.entity.User;
  import com.study.service.UserService;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  ​
  import java.util.List;
  ​
  /**
   * @ClassName TestUserService
   * @Description TODO
   * @Author Jiangnan Cui
   * @Date 2022/4/23 11:11
   * @Version 1.0
   */
  public class TestUserService {
      public static void main(String[] args) {
          ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
          UserService userService = (UserService) context.getBean("userService");
          List users = userService.selectAllUsers();
          for (User user : users) {
              System.out.println("user = " + user);
          }
      }
  }
  ​

测试结果:

  [DEBUG] 2022-04-23 ==>  Preparing: select id,name,age,bir from t_user 
  [DEBUG] 2022-04-23 ==> Parameters: 
  [DEBUG] 2022-04-23 <==      Total: 18
  user = User{id='1', name='张三', age=10, bir=Fri Apr 01 00:00:00 CST 2022}
  user = User{id='2', name='李四', age=20, bir=Sat Apr 02 00:00:00 CST 2022}
  user = User{id='2d237778-835d-477b-9721-fe6f575064b9', name='波比', age=18, bir=Sun Feb 02 00:00:00 CST 2020}
  user = User{id='2f278828-6422-432d-ab1b-a727b33c23bc', name='小胖墩', age=3, bir=Mon Apr 11 21:43:57 CST 2022}
  user = User{id='3', name='王五', age=30, bir=Sun Apr 03 00:00:00 CST 2022}
  user = User{id='4', name='灰太狼', age=60, bir=Sun Apr 10 09:56:17 CST 2022}
  user = User{id='5', name='小超超', age=36, bir=Sun Apr 10 09:55:41 CST 2022}
  user = User{id='586a5f71-836c-4171-a038-4620fe0f9354', name='葫芦娃', age=5, bir=Tue Apr 12 09:45:17 CST 2022}
  user = User{id='6', name='小呆呆', age=60, bir=Sun Apr 10 09:55:43 CST 2022}
  user = User{id='7', name='小超超', age=36, bir=Sun Apr 10 09:55:44 CST 2022}
  user = User{id='7a1a00aa-1507-461a-95c0-50110d6f4333', name='唐三', age=20, bir=Fri Apr 15 00:00:00 CST 2022}
  user = User{id='8', name='胖嘟嘟', age=50, bir=Sun Apr 10 09:55:39 CST 2022}
  user = User{id='8a3e8ab8-5abc-44e1-9aec-31f3978d97f2', name='小胖墩', age=3, bir=Tue Apr 12 10:23:33 CST 2022}
  user = User{id='9', name='小崔', age=25, bir=Sun Apr 10 09:55:52 CST 2022}
  user = User{id='985cd035-dedc-4e84-973a-a5fdaa2b8475', name='喜洋洋', age=8, bir=Tue Oct 10 00:00:00 CST 2000}
  user = User{id='a6be9263-f05f-415f-855e-33843b22e71d', name='皮卡丘', age=10, bir=Thu Apr 14 00:00:00 CST 2022}
  user = User{id='cd280176-68b2-4ceb-b781-ba2f5e1fbeff', name='小胖墩', age=3, bir=Tue Apr 12 10:21:18 CST 2022}
  user = User{id='e3863fd3-173c-46f4-a753-c72f7c92ad56', name='小猫咪', age=3, bir=Mon Apr 11 21:36:31 CST 2022}

4.Spring+SpringMVC整合 4.1 配置web.xml

    Archetype Created Web Application
    
    
      org.springframework.web.context.ContextLoaderListener
    
  ​
    
    
      contextConfigLocation
      classpath:spring.xml
    
  ​
    
    
      springmvc
      org.springframework.web.servlet.DispatcherServlet
      
        contextConfigLocation
        classpath:springmvc.xml
      
    
    
      springmvc
      /
    
  ​
    
    
      charset
      org.springframework.web.filter.CharacterEncodingFilter
      
        encoding
        UTF-8
      
    
    
      charset
      /*
    
  

4.2 引入springmvc.xml
  
  
  

      
      
  ​
      
      
  ​
      
      
          
          
      

      
      

4.3 新建Controller
  
  package com.study.controller;
  ​
  import com.study.entity.User;
  import com.study.service.UserService;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  ​
  import javax.servlet.http.HttpServletRequest;
  import java.util.List;
  ​
  /**
   * @ClassName UserController
   * @Description TODO
   * @Author Jiangnan Cui
   * @Date 2022/4/23 12:12
   * @Version 1.0
   */
  @Controller
  @RequestMapping("user")
  public class UserController {
      @Autowired
      private UserService userService;
  ​
      @RequestMapping("selectAllUsers")
      public String selectAllUsers(HttpServletRequest request){
          //1.收集数据
          //2.调用业务对象
          List users = userService.selectAllUsers();
          request.setAttribute("users",users);
          return "selectAllUsers";
      }
  ​
      @RequestMapping("insertUser")
      public String insertUser(User user){
          //1.收集参数
          //2.调用业务方法
          try {
              userService.insertUser(user);
              return "redirect:/user/selectAllUsers";
          } catch (Exception e) {
              e.printStackTrace();
              return "redirect:/insertUser.jsp";
          }
      }​​
  }
  ​

4.4 添加jsp文件

selectAllUsers.jsp

  
  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  <%@page contentType="text/html; UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
  
  
  
      
      
      
      用来展示所有用户信息
      
      
  
  
      展示用户列表
      
          ${user.id} ==== ${user.name} ==== ${user.age} ==== ${user.bir} 
   
​    添加用户信息

insertUser.jsp

  
  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  <%@page contentType="text/html; UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
  
  
  
      
      
      
      添加用户信息
  
  
      添加用户信息
      
      姓名:
      年龄:
      生日:
           

5. 部署tomcat服务器测试

 测试结果:

6.总结

附项目目录结构:

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/726769.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存