91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringMVC+myBatis如何結合使用

發布時間:2021-12-16 10:17:26 來源:億速云 閱讀:147 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關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");

    }

 

    /***

     * 用戶登陸

     * <p>

     * 注解配置,只允許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

<?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如何結合使用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

林甸县| 隆化县| 阳东县| 五台县| 德江县| 寿光市| 许昌县| 兴和县| 郁南县| 扎赉特旗| 怀柔区| 仁化县| 稷山县| 临沭县| 东乡族自治县| 江山市| 开平市| 金川县| 阜城县| 饶阳县| 达拉特旗| 涞源县| 辛集市| 临沂市| 日照市| 响水县| 武安市| 且末县| 内黄县| 东海县| 达孜县| 都昌县| 庄浪县| 都匀市| 宝山区| 陇西县| 景德镇市| 肥乡县| 齐齐哈尔市| 潼关县| 南澳县|