7: Spring+SpringMVC+Maven+Mybatis+MySQL項(xiàng)目搭建

開發(fā)環(huán)境介紹

IDE: MyEclipse 10.7
jdk: 1.6
maven:apache-maven-3.2.5
基本環(huán)境只要上面這些就行.
Maven的安裝

1、創(chuàng)建表源请,并插入數(shù)據(jù):

----------------------------------------------創(chuàng)建表
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

---------------------------------------插入數(shù)據(jù)
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工程創(chuàng)建,選擇快速框架

Paste_Image.png
Paste_Image.png

3 輸出項(xiàng)目名卸耘,包(Packaging,如果只是普通的項(xiàng)目刊侯,選jar就好了声离,如果是web項(xiàng)目就選war芒炼,這里是web項(xiàng)目,所以選擇war)

Paste_Image.png

4术徊、創(chuàng)建好的目錄如下:

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版本號(hào) -->
        <spring.version>3.2.8.RELEASE</spring.version>
        <!-- log4j日志文件管理包版本 -->
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <!-- junit版本號(hào) -->
        <junit.version>4.10</junit.version>
        <!-- mybatis版本號(hào) -->
        <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>

        <!--單元測(cè)試依賴 -->
        <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單元測(cè)試依賴 -->
        <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驅(qū)動(dòng)包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.29</version>
        </dependency>
    </dependencies>

</project>

6、配置文件:

6子寓、1 創(chuàng)建 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暗挑、 創(chuàng)建 properties包下添加jdbc.properties文件(數(shù)據(jù)源配置文件):
Paste_Image.png
jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/study
jdbc_username=root
jdbc_password=123456
6. 3、 創(chuàng)建 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">

<!--設(shè)置domain類和數(shù)據(jù)庫(kù)中表的字段一一對(duì)應(yīng)斜友,注意數(shù)據(jù)庫(kù)字段和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鲜屏、 創(chuàng)建 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>
                <!--要是有多個(gè)配置文件烹看,只需在這里繼續(xù)添加即可 -->
            </list>
        </property>
    </bean>

    <!-- 配置數(shù)據(jù)源 -->
    <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>

    <!-- 自動(dòng)掃描了所有的XxxxMapper.xml對(duì)應(yīng)的mapper接口文件,這樣就不用一個(gè)一個(gè)手動(dòng)配置Mpper的映射了洛史,只要Mapper接口類和Mapper映射文件對(duì)應(yīng)起來就可以了惯殊。 -->
    <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在同一個(gè)包下且同名土思,spring掃描Mapper.class的同時(shí)會(huì)自動(dòng)掃描同名的Mapper.xml并裝配到Mapper.class。
        如果Mapper.xml與Mapper.class不在同一個(gè)包下或者不同名忆嗜,就必須使用配置mapperLocations指定mapper.xml的位置己儒。
        此時(shí)spring是通過識(shí)別mapper.xml中的 <mapper namespace="com.XXX.XXX"> namespace的值來確定對(duì)應(yīng)的Mapper.class的。 -->
        <!-- <property name="mapperLocations" value="classpath:mapper/*.xml"/>   -->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
    </bean>

    <!-- 自動(dòng)掃描注解的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實(shí)現(xiàn)類:
  • 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 單元測(cè)試

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" })  
//使用標(biāo)準(zhǔn)的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 運(yùn)行單元測(cè)試冻璃,UserServiceTest右鍵Run As –>Junit Test,運(yùn)行結(jié)果:
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损合,并轉(zhuǎn)換maven工程為web項(xiàng)目

9 省艳、轉(zhuǎn)換成web項(xiàng)目:

  • Eclipse


    Paste_Image.png
  • MyEclipse

Paste_Image.png

將紅框里面的勾去掉,確定(OK),然后重新打開剛剛那個(gè)頁(yè)面嫁审,把Dynamic web Module勾上跋炕,就會(huì)看到紅框的內(nèi)容,點(diǎn)擊:

Paste_Image.png

配置如下,然后點(diǎn)擊OK

Paste_Image.png

至此,項(xiàng)目已轉(zhuǎn)為WEB項(xiàng)目律适!

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版本號(hào) -->
        <spring.version>3.2.8.RELEASE</spring.version>
        <!-- log4j日志文件管理包版本 -->
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <!-- junit版本號(hào) -->
        <junit.version>4.11</junit.version>
        <!-- mybatis版本號(hào) -->
        <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單元測(cè)試依賴 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        <!-- mysql驅(qū)動(dòng)包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.29</version>
        </dependency>
        
        <!-- javaee-api包 注意和項(xiàng)目使用的JDK版本對(duì)應(yīng) -->  
        <dependency>  
            <groupId>javax</groupId>  
            <artifactId>javaee-api</artifactId>  
            <version>6.0</version>  
            <scope>provided</scope>  
        </dependency>  

        <!-- javaee-web-api包 注意和項(xiàng)目使用的JDK版本對(duì)應(yīng) -->  
        <dependency>  
            <groupId>javax</groupId>  
            <artifactId>javaee-web-api</artifactId>  
            <version>6.0</version>  
            <scope>provided</scope>  
        </dependency>  
        
    </dependencies>
  
</project>

其實(shí)也就增加了下面兩個(gè)

<!-- javaee-api包 注意和項(xiàng)目使用的JDK版本對(duì)應(yīng) -->  
        <dependency>  
            <groupId>javax</groupId>  
            <artifactId>javaee-api</artifactId>  
            <version>6.0</version>  
            <scope>provided</scope>  
        </dependency>  

        <!-- javaee-web-api包 注意和項(xiàng)目使用的JDK版本對(duì)應(yīng) -->  
        <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,內(nèi)容如下:
<?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"/>  

   <!-- 對(duì)模型視圖添加前后綴 -->
   <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>
  <!-- /*時(shí),匹配不到j(luò)sp頁(yè)面 -->
        <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>
    
    <!-- 起始?xì)g迎界面 -->
    <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>
    
    <!-- 設(shè)計(jì)路徑變量值 -->
    <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>
    
    <!-- 監(jiān)聽 -->
    <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>
    
    <!-- 攔截設(shè)置 -->
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <!-- 錯(cuò)誤跳轉(zhuǎn)頁(yè)面 -->
    <error-page>
        <!-- 路徑不正確 -->
        <error-code>404</error-code>
        <location>/WEB-INF/errorpage/404.jsp</location>
    </error-page>
    <error-page>
        <!-- 沒有訪問權(quán)限扣草,訪問被禁止 -->
        <error-code>405</error-code>
        <location>/WEB-INF/errorpage/405.jsp</location>
    </error-page>
    <error-page>
        <!-- 內(nèi)部錯(cuò)誤 -->
        <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下新建一個(gè)文件夾jsp,添加一個(gè)index.jsp辰妙,內(nèi)容如下:
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下新建一個(gè)包c(diǎn)om.gp6.controller.然后新建一個(gè)類UserController.java,其內(nèi)容如下

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 運(yùn)行!C芑搿M苡ぁ!完成尔破!
Paste_Image.png

11街图、源碼下載

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市呆瞻,隨后出現(xiàn)的幾起案子台夺,更是在濱河造成了極大的恐慌,老刑警劉巖痴脾,帶你破解...
    沈念sama閱讀 206,013評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件颤介,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡赞赖,警方通過查閱死者的電腦和手機(jī)滚朵,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,205評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來前域,“玉大人辕近,你說我怎么就攤上這事∧渎ⅲ” “怎么了移宅?”我有些...
    開封第一講書人閱讀 152,370評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)椿疗。 經(jīng)常有香客問我漏峰,道長(zhǎng),這世上最難降的妖魔是什么届榄? 我笑而不...
    開封第一講書人閱讀 55,168評(píng)論 1 278
  • 正文 為了忘掉前任浅乔,我火速辦了婚禮,結(jié)果婚禮上铝条,老公的妹妹穿的比我還像新娘靖苇。我一直安慰自己,他們只是感情好班缰,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,153評(píng)論 5 371
  • 文/花漫 我一把揭開白布贤壁。 她就那樣靜靜地躺著,像睡著了一般埠忘。 火紅的嫁衣襯著肌膚如雪芯砸。 梳的紋絲不亂的頭發(fā)上萧芙,一...
    開封第一講書人閱讀 48,954評(píng)論 1 283
  • 那天,我揣著相機(jī)與錄音假丧,去河邊找鬼双揪。 笑死,一個(gè)胖子當(dāng)著我的面吹牛包帚,可吹牛的內(nèi)容都是我干的渔期。 我是一名探鬼主播,決...
    沈念sama閱讀 38,271評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼渴邦,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼疯趟!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起谋梭,我...
    開封第一講書人閱讀 36,916評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤信峻,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后瓮床,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體盹舞,經(jīng)...
    沈念sama閱讀 43,382評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,877評(píng)論 2 323
  • 正文 我和宋清朗相戀三年隘庄,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了踢步。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 37,989評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡丑掺,死狀恐怖获印,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情兼丰,我是刑警寧澤地粪,帶...
    沈念sama閱讀 33,624評(píng)論 4 322
  • 正文 年R本政府宣布斗忌,位于F島的核電站织阳,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏弄痹。R本人自食惡果不足惜肛真,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,209評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蹄葱,春花似錦、人聲如沸琼稻。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,199評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)睬塌。三九已至揩晴,卻和暖如春硫兰,著一層夾襖步出監(jiān)牢的瞬間违孝,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,418評(píng)論 1 260
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留撒踪,地道東北人制妄。 一個(gè)月前我還...
    沈念sama閱讀 45,401評(píng)論 2 352
  • 正文 我出身青樓俺抽,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親弛饭。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,700評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理侣颂,服務(wù)發(fā)現(xiàn),斷路器枪孩,智...
    卡卡羅2017閱讀 134,599評(píng)論 18 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,748評(píng)論 6 342
  • 最近比較閑憔晒,復(fù)習(xí)搭建一下項(xiàng)目,這次主要使用Spring+SpringMVC+Mybatis蔑舞。項(xiàng)目持久層使用Myba...
    mingli_jianshu1閱讀 1,591評(píng)論 0 10
  • 坐上火車的那一刻拒担,我哭了,哭的特別厲害斗幼,以至于車上周圍的人紛紛向我這邊望來。我看著窗外抚垄,想著心事蜕窿,在火車開動(dòng)之前想...
    深沉的狼閱讀 181評(píng)論 0 0
  • 今天是個(gè)霧霾天谋逻。 太陽(yáng)不知道躲到哪里發(fā)呆了,就跟我喜歡一個(gè)人聽著羅威的鋼琴曲胡思亂想一樣桐经,不想被人打擾毁兆。 想想過去...
    嫩寒閱讀 254評(píng)論 0 0