广告
返回顶部
首页 > 资讯 > 精选 >SpringMVC+myBatis如何结合使用
  • 411
分享到

SpringMVC+myBatis如何结合使用

2023-06-03 00:06:42 411人浏览 泡泡鱼
摘要

这篇文章给大家分享的是有关springMVC+mybatis如何结合使用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。1. [代码]控制器片段     

这篇文章给大家分享的是有关springMVC+mybatis如何结合使用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

1. [代码]控制器片段     

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

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

package com.wg.test;

 

import javax.servlet.Http.httpservletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.WEB.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;

import com.wg.bean.User;

import com.wg.service.UserService;

 

@Controller

public class UserController {

 

    @Autowired

    private UserService userService;

 

    @RequestMapping(value = "reGISt", method = RequestMethod.POST)

    public ModelAndView regist(HttpServletRequest request, User user) {

        try {

            userService.saveUser(user);

        } catch (Exception e) {

            e.printStackTrace();

        }

        request.setAttribute("username", user.getUsername());

        request.setAttribute("passWord", user.getPassword());

        System.out.println(user.toString());

        return new ModelAndView("succ");

    }

 

    

    @RequestMapping(value = "login", method = RequestMethod.POST)

    public ModelAndView login(String username, String password) {

        // 验证传递过来的参数是否正确,否则返回到登陆页面。

        if (this.checkParams(new String[] { username, password })) {

            // 指定要返回的页面为succ.jsp

            ModelAndView mav = new ModelAndView("succ");

            // 将参数返回给页面

            mav.addObject("username", username);

            mav.addObject("password", password);

            System.out

                    .println("username=" + username + " password=" + password);

            return mav;

        }

        return new ModelAndView("home");

    }

 

    

    private boolean checkParams(String[] params) {

        for (String param : params) {

            if (param == "" || param == null || param.isEmpty()) {

                return false;

            }

        }

        return true;

    }

}

2. [代码]web.xml配置     

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

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

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 

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

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  

    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 

       

    <!-- 监听spring上下文容器 --> 

    <listener> 

        <listener-class> 

            org.springframework.web.context.ContextLoaderListener 

        </listener-class> 

    </listener> 

       

    <!-- 加载spring的xml配置文件到 spring的上下文容器中 --> 

    <context-param> 

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

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

    </context-param> 

       

    <!-- 配置Spring mvc DispatcherServlet --> 

    <servlet> 

        <servlet-name>MVC</servlet-name> 

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 

        <!-- 初始化参数 --> 

        <init-param> 

            <!-- 加载SpringMVC的xml到 spring的上下文容器中 --> 

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

            <param-value> 

                /WEB-INF/classes/mvc-context.xml 

            </param-value> 

        </init-param> 

        <load-on-startup>1</load-on-startup> 

    </servlet> 

   

    <!-- 配置DispatcherServlet所需要拦截的 url --> 

    <servlet-mapping> 

        <servlet-name>MVC</servlet-name> 

        <url-pattern>*.do</url-pattern> 

    </servlet-mapping> 

   

    <welcome-file-list> 

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

    </welcome-file-list> 

   

   

</web-app>

3. [代码]spring-mvc配置     

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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

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

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

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

  http://www.springframework.org/schema/context

  http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 

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

 

    <bean id="viewResolver"

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

        <property name="viewClass"

            value="org.springframework.web.servlet.view.JstlView" />

        <property name="prefix" value="/page/" />

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

    </bean>

</beans>

4. [代码]userMapper配置     

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

<?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.wg.dao.UserDao">

    <!-- 取得用户列表 -->

    <select id="getUser" resultType="User" parameterType="User">

        select

        id,

        username,

        password

        From user

        <where>

            <if test="username != null and password != null">

                username =#{username} and password =#{password}

            </if>

            <if test="id!=null">

                and id=#{id}

            </if>

        </where>

    </select>

    <!-- 新增用户 -->

    <insert id="insertUser" parameterType="User">

        insert into user(id,username,password) values(#{id},#{username},#{password})

        <selecTKEy keyProperty="id" resultType="Long">

            select last_insert_id() as id

        </selectKey>

    </insert>

    <!-- 修改用户 -->

    <update id="updateUser" parameterType="User">

        update user

        <set>

            <if test="username != null">username=#{username},</if>

            <if test="password != null">password=#{password},</if>

        </set>

        where id=#{id}

    </update>

    <!-- 删除用户 -->

    <delete id="deleteUser" parameterType="Long">

        delete from user where id=#{id}

    </delete>

 

</mapper>

感谢各位的阅读!关于“SpringMVC+myBatis如何结合使用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

--结束END--

本文标题: SpringMVC+myBatis如何结合使用

本文链接: https://www.lsjlt.com/news/231915.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringMVC+myBatis如何结合使用
    这篇文章给大家分享的是有关SpringMVC+myBatis如何结合使用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。1. [代码]控制器片段     ...
    99+
    2023-06-03
  • Spring整合mybatis、springMVC总结
    目录1.mybatis配置流程2.spring配置流程3.spring 整合Dao层4.spring整合Service层5.spring整合MVC层6. spring整合dao-se...
    99+
    2023-05-18
    Spring整合mybatis springMVC Spring整合mybatis Spring整合springMVC
  • 使用SpringMVC如何实现整合mybatis
    这篇文章给大家介绍使用SpringMVC如何实现整合mybatis,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache softw...
    99+
    2023-05-31
    springmvc mybatis
  • 如何在AngularJs中结合SpringMVC使用
    本篇文章给大家分享的是有关如何在AngularJs中结合SpringMVC使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。首先我们把springmvc的环境搭好,先来web....
    99+
    2023-05-31
    angularjs springmvc
  • 使用spring如何实现springmvc与mybatis进行整合
    使用spring如何实现springmvc与mybatis进行整合?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。jar包 引入web.xml文件<conte...
    99+
    2023-05-31
    spring springmvc mybatis
  • Mybatis怎么与Spring结合使用
    Mybatis怎么与Spring结合使用?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。所需要用到的其他工具或技术:项目管理工具 : Maven前台WEB展示:JSP其他框架:S...
    99+
    2023-05-31
    mybatis spring
  • 如何使用Maven搭建SpringMVC+Spring+MyBatis框架
    这篇文章主要介绍如何使用Maven搭建SpringMVC+Spring+MyBatis框架,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!SSM(Spring+SpringMVC+Mybatis),目前较为主流的企业级...
    99+
    2023-05-30
    springmvc spring mybatis
  • springmvc 结合ajax如何实现批量增加
    这篇文章主要讲解了“springmvc 结合ajax如何实现批量增加”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“springmvc 结合ajax如何实现批量增加”吧! 需要注意的问题mvc...
    99+
    2023-06-08
  • SpringMVC Web项目如何使用SpringBoot和Mybatis实现
    SpringMVC Web项目如何使用SpringBoot和Mybatis实现?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。一、热身一个现实的场景是:当我们开发...
    99+
    2023-05-31
    springmvc web springboot mybatis
  • Idea SpringMVC+Spring+MyBatis+Maven怎么使用
    本篇内容介绍了“Idea SpringMVC+Spring+MyBatis+Maven怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧...
    99+
    2022-10-19
  • 使用Spring如何搭建一个SpringMVC与MyBatis环境
    这篇文章将为大家详细讲解有关使用Spring如何搭建一个SpringMVC与MyBatis环境,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。1.对原生态jdbc程序中问题总结1 jdbc程序...
    99+
    2023-05-31
    mybatis spring springmvc
  • 如何使用Oracle结合Mybatis实现取表的10条数据
    如何使用Oracle结合Mybatis实现取表的10条数据?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。最原始版本:selec...
    99+
    2022-10-18
  • 如何将mybatis配置到springmvc中
    MyBatis简介MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原...
    99+
    2023-05-30
    mybatis springmvc
  • 使用spring如何实现整合mybatis
    今天就跟大家聊聊有关使用spring如何实现整合mybatis,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。1、采用MapperScannerConfigurer,它将会查找类路径下...
    99+
    2023-05-31
    spring mybatis 整合
  • 如何在springmvc与mybatis中使用sql 语句实现分页
    如何在springmvc与mybatis中使用sql 语句实现分页?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。具体代码如下所示:<xml version=&q...
    99+
    2023-05-31
    springmvc sql 语句 mybatis
  • Mybatis如何使用动态语句实现批量删除(delete结合foreach)
    目录下面我将演示如何使用动态语句批量删除数据库数据下面是项目的结构1、IEmpDAO.java为接口提供批量删除数据方法2、EmpDAOImpl.java为接口的实现类3、Mybat...
    99+
    2022-11-13
  • SpringMVC整合Kaptcha的具体使用
    目录验证码的作用Kaptcha 简介Kaptcha 详细配置表Spring MVC 整合 KaptchaPOM创建 Spring 配置控制器关键代码JSP 关键代码验证码的作用 防止...
    99+
    2022-11-13
  • 如何使用springmvc
    小编给大家分享一下如何使用springmvc,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!1.配置阶段根据web.xml ,先定义DispatcherServle...
    99+
    2023-06-25
  • 使用SpringMVC和MyBatis框架如何搭建一个开发环境
    本篇文章给大家分享的是有关使用SpringMVC和MyBatis框架如何搭建一个开发环境,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。下载SpringMVC框架架包,点击打开地...
    99+
    2023-05-31
    springmvc sprinmybatis 环境搭建
  • 微信开发中如何使用springmvc 搭建一个mybatis项目
    本篇文章为大家展示了微信开发中如何使用springmvc 搭建一个mybatis项目,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。①在建立好的maven项目中的pom.xml文件引入依赖,代码如下:...
    99+
    2023-05-31
    springmvc mybatis
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作