千家信息网

SpringMVC+myBatis如何结合使用

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章给大家分享的是有关SpringMVC+myBatis如何结合使用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。1. [代码]控制器片段123456789101112
千家信息网最后更新 2026年11月01日SpringMVC+myBatis如何结合使用

这篇文章给大家分享的是有关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");

}

/***

* 用户登陆

*

* 注解配置,只允许POST提交到该方法

*

* @param username

* @param password

* @return

*/

@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");

}

/***

* 验证参数是否为空

*

* @param params

* @return

*/

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

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">

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:*-context.xml

MVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

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

1

MVC

*.do

index.jsp

3. [代码]spring-mvc配置

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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">

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

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

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

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

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

select last_insert_id() as id

update user

username=#{username},

password=#{password},

where id=#{id}

delete from user where id=#{id}

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

代码 配置 参数 页面 内容 更多 篇文章 登陆 验证 不错 实用 控制器 文章 方法 注解 片段 用户 看吧 知识 参考 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 青岛互联网科技公司 狮子座互联网科技 互联网女皇 腾讯科技 四个字互联网科技公司取名 博兴的互联网电子科技公司 深圳国际航运科技和互联网 哈尔滨新华互联网科技学院 中国的互联网科技企业的崛起 晓花互联网科技有限公司网址 新兴科技互联网创新大赛颁奖典礼 互联网科技人工智能实验室 广州新华互联网科技学校电竞方面 威龙国际互联网科技公司 明优互联网科技有限公司 互联网科技圈年度人物 中国互联网有什么科技 抖集互联网科技技术咨询 中国科技和美国互联网 北京军民互联网科技公司 安徽九吞吴互联网科技有限公司 耶加雪菲 互联网科技公司 重庆互联网络科技 西安互联网科技 互联网科技论坛 呼和浩特新华互联网科技学院 领航科技大厦互联网公司 福仕德教育科技互联网 数字科技和互联网金融哪个好 互联网 科技节活动 微视频大赛
0