7: Spring+SpringMVC+Maven+Mybati

作者: 笑Skr人啊 | 来源:发表于2017-03-17 14:26 被阅读31次

    开发环境介绍

    IDE: MyEclipse 10.7
    jdk: 1.6
    maven:apache-maven-3.2.5
    基本环境只要上面这些就行.
    Maven的安装

    1、创建表,并插入数据:

    ----------------------------------------------创建表
    CREATE TABLE `t_user` (
      `USER_ID` int(11) NOT NULL AUTO_INCREMENT,
      `USER_NAME` char(30) NOT NULL,
      `USER_PASSWORD` char(10) NOT NULL,
      PRIMARY KEY (`USER_ID`),
      KEY `IDX_NAME` (`USER_NAME`)
    ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8
    
    ---------------------------------------插入数据
    INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD) VALUES (1, 'luoguohui', '123456');
    INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD) VALUES (2, 'zhangsan', '123456');
    

    2、Maven工程创建,选择快速框架

    Paste_Image.png Paste_Image.png

    3 输出项目名,包(Packaging,如果只是普通的项目,选jar就好了,如果是web项目就选war,这里是web项目,所以选择war)

    Paste_Image.png

    4、创建好的目录如下:

    Paste_Image.png

    5 、添加包的依赖,编辑pom.xml文件添加如下依赖:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.gp6</groupId>
      <artifactId>MyMavenProject</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      
      <properties>
            <!-- spring版本号 -->
            <spring.version>3.2.8.RELEASE</spring.version>
            <!-- log4j日志文件管理包版本 -->
            <slf4j.version>1.6.6</slf4j.version>
            <log4j.version>1.2.12</log4j.version>
            <!-- junit版本号 -->
            <junit.version>4.10</junit.version>
            <!-- mybatis版本号 -->
            <mybatis.version>3.2.1</mybatis.version>
      </properties>
    
      <dependencies>
            <!-- 添加Spring依赖 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
    
            <!--单元测试依赖 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
    
            <!-- 日志文件管理包 -->
            <!-- log start -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <!-- log end -->
    
            <!--spring单元测试依赖 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
                <scope>test</scope>
            </dependency>
    
            <!--mybatis依赖 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
    
            <!-- mybatis/spring包 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.2.0</version>
            </dependency>
    
            <!-- mysql驱动包 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.29</version>
            </dependency>
        </dependencies>
    
    </project>
    
    

    6、配置文件:

    6、1 创建 mybatis包下添加mybatis-config.xml文件(mybatis配置文件):
    Paste_Image.png
    <?xml version="1.0" encoding="UTF-8"?>  
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
    "http://mybatis.org/dtd/mybatis-3-config.dtd">  
    
    <configuration> 
       
    </configuration>
    
    6. 2、 创建 properties包下添加jdbc.properties文件(数据源配置文件):
    Paste_Image.png
    jdbc_driverClassName=com.mysql.jdbc.Driver
    jdbc_url=jdbc:mysql://localhost:3306/study
    jdbc_username=root
    jdbc_password=123456
    
    6. 3、 创建 mapper包下userMapper.xml配置文件(表映射文件):
    Paste_Image.png
    一定要注意<mapper namespace="com.gp6.mapper.UserMapper">这一行的值是否正确
    <?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.gp6.mapper.UserMapper">
    
    <!--设置domain类和数据库中表的字段一一对应,注意数据库字段和Entity类中的字段名称不致,此处一定要!-->
        <resultMap id="BaseResultMap" type="com.gp6.entity.User">
            <id column="USER_ID" property="userId" jdbcType="INTEGER" />
            <result column="USER_NAME" property="userName" jdbcType="CHAR" />
            <result column="USER_PASSWORD" property="userPassword" jdbcType="CHAR" />
        </resultMap>
        
        
        <!-- 查询单条记录 -->
        <select id="selectUserById" parameterType="int" resultMap="BaseResultMap">
            SELECT * FROM t_user WHERE USER_ID = #{userId}
        </select>
    </mapper>
    
    6. 4、 创建 spring配置文件application.xml:
    Paste_Image.png
    <?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:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        
        xsi:schemaLocation=
               "http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
               http://www.springframework.org/schema/aop  
               http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
               http://www.springframework.org/schema/context  
               http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
         <!-- 引入jdbc配置文件 -->  
         <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                   <value>classpath:properties/*.properties</value>
                    <!--要是有多个配置文件,只需在这里继续添加即可 -->
                </list>
            </property>
        </bean>
    
        <!-- 配置数据源 -->
        <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <!-- 不使用properties来配置 -->
            <!-- <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
                <property name="url" value="jdbc:mysql://localhost:3306/study" /> 
                <property name="username" value="root" /> 
                <property name="password" value="123456" /> -->
                
                
           <!-- 使用properties来配置 ,建议使用-->
            <property name="driverClassName">
                <value>${jdbc_driverClassName}</value>
            </property>
            <property name="url">
                <value>${jdbc_url}</value>
            </property>
            <property name="username">
                <value>${jdbc_username}</value>
            </property>
            <property name="password">
                <value>${jdbc_password}</value>
            </property>
        </bean>
    
        <!-- 自动扫描了所有的XxxxMapper.xml对应的mapper接口文件,这样就不用一个一个手动配置Mpper的映射了,只要Mapper接口类和Mapper映射文件对应起来就可以了。 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.gp6.dao" />
        </bean>
    
        <!-- 配置Mybatis的文件 ,mapperLocations配置**Mapper.xml文件位置,configLocation配置mybatis-config文件位置-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <!-- 如果Mapper.xml与Mapper.class在同一个包下且同名,spring扫描Mapper.class的同时会自动扫描同名的Mapper.xml并装配到Mapper.class。
            如果Mapper.xml与Mapper.class不在同一个包下或者不同名,就必须使用配置mapperLocations指定mapper.xml的位置。
            此时spring是通过识别mapper.xml中的 <mapper namespace="com.XXX.XXX"> namespace的值来确定对应的Mapper.class的。 -->
            <!-- <property name="mapperLocations" value="classpath:mapper/*.xml"/>   -->
            <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
        </bean>
    
        <!-- 自动扫描注解的bean -->
        <context:component-scan base-package="com.gp6.service" />
    
    </beans>
    

    7 接口和类的配置:

    Paste_Image.png
    7.1 、com.gp6.entity 下添加User.Java文件:
    package com.gp6.entity;
    
    public class User {
        private Integer userId;  
        private String userName;  
        private String userPassword;
        
        public Integer getUserId() {
            return userId;
        }
        public void setUserId(Integer userId) {
            this.userId = userId;
        }
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
        public String getUserPassword() {
            return userPassword;
        }
        public void setUserPassword(String userPassword) {
            this.userPassword = userPassword;
        }  
    }
    
    
    7.2、com.gp6.mapper下添加Usermapper.java文件:
    package com.gp6.mapper;
    
    import com.gp6.entity.User;
    
    public interface UserMapper {
        
        /**
         * @param userId
         * @return User
         */
        public User selectUserById(Integer userId);  
    }
    
    
    7.3、com.gp6.service下添加UserService.java接口和UserServiceImpl实现类:
    • UserService.java
    package com.gp6.service;
    
    import com.gp6.entity.User;
    
    public interface UserService {
        public User selectUserById(Integer userId); 
    }
    
    
    • UserServiceImpl.java
    package com.gp6.service.impl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.gp6.entity.User;
    import com.gp6.mapper.UserMapper;
    import com.gp6.service.UserService;
    
    @Service
    public class UserServiceImpl implements UserService {
    
        @Autowired
        UserMapper userMapper;
        
        @Override
        public User selectUserById(Integer userId) {
             return userMapper.selectUserById(userId);  
        }
    
    }
    
    

    8 单元测试

    Paste_Image.png
    8.1 BaseTest
    package com.gp6.baseTest;
    
    import org.junit.runner.RunWith;  
    import org.springframework.test.context.ContextConfiguration;  
    import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;  
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
    
    //指定bean注入的配置文件  
    @ContextConfiguration(locations = { "classpath:application.xml" })  
    //使用标准的JUnit @RunWith注释来告诉JUnit使用Spring TestRunner  
    @RunWith(SpringJUnit4ClassRunner.class) 
    public class BaseTest extends AbstractJUnit4SpringContextTests{
    
    }
    
    
    8.2 UserServiceTest
    package com.gp6.serviceTest;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.junit.Test;  
    import com.gp6.baseTest.BaseTest;  
    import com.gp6.entity.User;
    import com.gp6.service.UserService;
    
    public class UserServiceTest extends BaseTest {
        @Autowired  
        private UserService userService; 
    
        @Test  
        public void selectUserByIdTest(){  
            User user = userService.selectUserById(1);  
            System.out.println(user.getUserName() + ":" + user.getUserPassword());
        }  
    }
    
    
    8.3 运行单元测试,UserServiceTest右键Run As –>Junit Test,运行结果:
    Paste_Image.png
    8.4 添加日志配置
    Paste_Image.png
    • log4j.properties
    log4j.rootLogger=DEBUG,Console,Stdout  
      
    #Console  
    log4j.appender.Console=org.apache.log4j.ConsoleAppender  
    log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
    log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n  
      
    log4j.logger.java.sql.ResultSet=INFO  
    log4j.logger.org.apache=INFO  
    log4j.logger.java.sql.Connection=DEBUG  
    log4j.logger.java.sql.Statement=DEBUG  
    log4j.logger.java.sql.PreparedStatement=DEBUG   
      
    log4j.appender.Stdout = org.apache.log4j.DailyRollingFileAppender    
    log4j.appender.Stdout.File = E://WorkSpace/Study/logs/MyMavenProject/log.log   
    log4j.appender.Stdout.Append = true    
    log4j.appender.Stdout.Threshold = DEBUG     
    log4j.appender.Stdout.layout = org.apache.log4j.PatternLayout    
    log4j.appender.Stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n    
    

    到这里配置好spring+mybatis+mysql!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    这里开始添加spirngMVC,并转换maven工程为web项目

    9 、转换成web项目:

    • Eclipse


      Paste_Image.png
    • MyEclipse

    Paste_Image.png

    将红框里面的勾去掉,确定(OK),然后重新打开刚刚那个页面,把Dynamic web Module勾上,就会看到红框的内容,点击:

    Paste_Image.png

    配置如下,然后点击OK

    Paste_Image.png

    至此,项目已转为WEB项目!

    10 配置springmvc

    10.1、pom.xml文件添加依赖,修改后配置如下:
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.gp6</groupId>
      <artifactId>MyMavenProject</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      
      <properties>
            <!-- spring版本号 -->
            <spring.version>3.2.8.RELEASE</spring.version>
            <!-- log4j日志文件管理包版本 -->
            <slf4j.version>1.6.6</slf4j.version>
            <log4j.version>1.2.12</log4j.version>
            <!-- junit版本号 -->
            <junit.version>4.11</junit.version>
            <!-- mybatis版本号 -->
            <mybatis.version>3.2.1</mybatis.version>
      </properties>
    
      <dependencies>
            <!-- 添加Spring依赖 -->
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
    
            <!-- 日志文件管理包 -->
            <!-- log start -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <!-- log end -->
    
            <!--spring单元测试依赖 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
                <scope>test</scope>
            <!-- mysql驱动包 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.29</version>
            </dependency>
            
            <!-- javaee-api包 注意和项目使用的JDK版本对应 -->  
            <dependency>  
                <groupId>javax</groupId>  
                <artifactId>javaee-api</artifactId>  
                <version>6.0</version>  
                <scope>provided</scope>  
            </dependency>  
    
            <!-- javaee-web-api包 注意和项目使用的JDK版本对应 -->  
            <dependency>  
                <groupId>javax</groupId>  
                <artifactId>javaee-web-api</artifactId>  
                <version>6.0</version>  
                <scope>provided</scope>  
            </dependency>  
            
        </dependencies>
      
    </project>
    
    

    其实也就增加了下面两个

    <!-- javaee-api包 注意和项目使用的JDK版本对应 -->  
            <dependency>  
                <groupId>javax</groupId>  
                <artifactId>javaee-api</artifactId>  
                <version>6.0</version>  
                <scope>provided</scope>  
            </dependency>  
    
            <!-- javaee-web-api包 注意和项目使用的JDK版本对应 -->  
            <dependency>  
                <groupId>javax</groupId>  
                <artifactId>javaee-web-api</artifactId>  
                <version>6.0</version>  
                <scope>provided</scope>  
            </dependency> 
    
    10.2、在src/main/resource中添加springmvc文件夹,然后添加文件spring-mvc.xml,内容如下:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    
       <mvc:annotation-driven /> 
       <!-- 扫描controller(controller层注入) -->
       <context:component-scan base-package="com.gp6.controller"/>  
    
       <!-- 对模型视图添加前后缀 -->
       <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
    </beans>
    
    10.3、配置web.xml
    <servlet-mapping>
            <servlet-name>dispatcherServlet</servlet-name>
      <!-- /*时,匹配不到jsp页面 -->
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    
    
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        id="WebApp_ID" version="2.5">
        
        <display-name>Archetype Created Web Application</display-name>
        
        <!-- 起始欢迎界面 -->
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    
        <!-- 读取spring配置文件 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:application.xml</param-value>
        </context-param>
        
        <!-- 设计路径变量值 -->
        <context-param>
            <param-name>webAppRootKey</param-name>
            <param-value>springmvc.root</param-value>
        </context-param>
    
        <!-- Spring字符集过滤器 -->
        <filter>
            <filter-name>SpringEncodingFilter</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>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>SpringEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        
        <!-- 监听 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <!-- springMVC核心配置 -->
        <servlet>
            <servlet-name>dispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <!--spingMVC的配置路径  -->
                <param-value>classpath:springmvc/spring-mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        
        <!-- 拦截设置 -->
        <servlet-mapping>
            <servlet-name>dispatcherServlet</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    
        <!-- 错误跳转页面 -->
        <error-page>
            <!-- 路径不正确 -->
            <error-code>404</error-code>
            <location>/WEB-INF/errorpage/404.jsp</location>
        </error-page>
        <error-page>
            <!-- 没有访问权限,访问被禁止 -->
            <error-code>405</error-code>
            <location>/WEB-INF/errorpage/405.jsp</location>
        </error-page>
        <error-page>
            <!-- 内部错误 -->
            <error-code>500</error-code>
            <location>/WEB-INF/errorpage/500.jsp</location>
        </error-page>
    </web-app>
    
    10.4、添加index.jsp,在src/main/webapp/WEB-INF下新建一个文件夹jsp,添加一个index.jsp,内容如下:
    Paste_Image.png
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <html>
    <body>
    <h2>Hello World!</h2>
    用户名: ${user.userName}<br>
     密码:${user.userPassword}<br>
    </body>
    </html>
    
    
    10.5、写controller
    Paste_Image.png

    在src/main/java下新建一个包com.gp6.controller.然后新建一个类UserController.java,其内容如下

    package com.gp6.controller;
    
    import javax.annotation.Resource;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.gp6.entity.User;
    import com.gp6.service.UserService;
    
    @Controller
    public class UserController {
        @Resource  
        private UserService userService;  
    
        @RequestMapping("/")    
        public ModelAndView getIndex(){      
            ModelAndView modelAndView = new ModelAndView("index");   
            User user = userService.selectUserById(1);  
            modelAndView.addObject("user", user);   
            return modelAndView;    
        }    
    }
    
    
    10.6 运行!!!!完成!
    Paste_Image.png

    11、源码下载

    相关文章

      网友评论

        本文标题:7: Spring+SpringMVC+Maven+Mybati

        本文链接:https://www.haomeiwen.com/subject/lzcenttx.html