maven搭建Spring+SpringMVC的应用

一(what)什么是maven
maven:是项目管理的一个构建工具,通过一些【简单的描述信息】来管理项目的构建,报告,文档等的软件项目管理工具
二(why)为什么使用maven
按照传统的开发方式,在一开始,我们往往得找到一些jar包,难免不了jar包的冲突,而采用maven直不在考虑jar包的问题,只需要加入一些【简单的描述信息】即可
三(how)怎么使用用maven开发

1,File->New->Project…->Maven->(选择SDK)New->Next->填写GroupID(项目的包名),ArtifactId(模块名),版本默认即可->next->Project name(项目名字),路径->finish
结果如图所示:

maven搭建Spring+SpringMVC的应用_第1张图片,
2,view->Tool Windows->maven Project

maven搭建Spring+SpringMVC的应用_第2张图片

3选中(如图所示,再运行,刷新)
maven就会到远程中心仓库下载所有依赖的jar包

maven搭建Spring+SpringMVC的应用_第3张图片

这样就可以了,接着就是建包撸码了。。。
*采用Spring+springmvc框架,简单的验证用户登录问题

工程目录如下所示:

maven搭建Spring+SpringMVC的应用_第4张图片

第一步建数据库

drop database springmvc;
create database springmvc;
use springmvc;
create table userTable(
id int primary key auto_increment,
username varchar(10),
password varchar(10)
);
insert into  userTable values(1,"wby","123456");

第二步建实体类
(属性注意和数据表字段相对应)

package com.wby.entity;

/**
 * Created by Administrator on 2017/11/20.
 */
public class User {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

第三步编写dao层
采用spring jdbcTemplate操作数据库

package com.wby.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * Created by Administrator on 2017/11/20.
 */

@Repository
public class UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public  int getCount(String username,String password){
        String sql="select count(*) from userTable "+"where username=? and password=?";
        return jdbcTemplate.queryForObject(sql, new Object[]{username, password}, Integer.class);
    }

}

第四步编写service层

package com.wby.service;
import com.wby.entity.User;
import com.wby.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2017/11/20.
 */
@Service
public class UserService {
    @Autowired
    UserDao userDao;
   public int getCount(User user){
      return  userDao.getCount(user.getUsername(),user.getPassword() );

   }
}

第五部编写controller层

```
package com.wby.controller;
import com.wby.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.wby.entity.User;
//import com.wby.service.UserService;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2017/11/20.
 */
@Controller
public class UserController {
    @Autowired
    UserService userService;
@RequestMapping("/index.html")
    public ModelAndView Check(HttpServletRequest request,User user){

       int a= userService.getCount(user);
       if(a>0) {
           request.getSession().setAttribute("user",user);
           return new ModelAndView("main");

       }
       else

           return new ModelAndView("index","error","用户名或者密码有误");

    }
}

六spring框架的配置文件applicationContext.xml
配置数据源,jdbcTemplate,,数据管理器,事物增强…


<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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    
    <context:component-scan base-package="com.wby.dao"/>
    <context:component-scan base-package="com.wby.service"/>

    
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" 
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/springmvc"
p:username="root"
p:password="root" />


<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
p:dataSource-ref="dataSource" />


<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />


<aop:config proxy-target-class="true">
<aop:pointcut id="serviceMethod"
expression="(execution(* com.wby.service..*(..))) and (@annotation(org.springframework.transaction.annotation.Transactional))" />
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" />
tx:attributes>
tx:advice>
beans>

七springmvc框架的配置文件springmvc.xml

配置视图解析器,前缀后缀的设定
springmvc的默认配置文件是放在WEB-INF下的,叫action-servlet.xml
想改在resources下,那么就得修改web.xml下的分发器的servlet


<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-4.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="com.wby.controller"/>


<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"

p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />

beans>

web.xml


<web-app version="2.5" 
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_2_5.xsd">
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
listener-class>
listener>



<servlet>
<servlet-name>actionservlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
servlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc.xmlparam-value>
init-param>
servlet>

<servlet-mapping>
<servlet-name>actionservlet-name>
<url-pattern>*.htmlurl-pattern>
servlet-mapping>


web-app>

pom.xm(在pom.xnl文件中配上坐标,maven就会去远程仓库中下载相应的jar包,这就是前面所提及的一小段配置信息)


<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.0modelVersion>

    <groupId>com.wbygroupId>
    <artifactId>demo1artifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>warpackaging>

    <properties>
        <file.encoding>UTF-8file.encoding>
        <spring.version>4.2.2.RELEASEspring.version>
        <mysql.version>5.1.29mysql.version>
        <servlet.version>3.0-alpha-1servlet.version>
        <aspectj.version>1.8.1aspectj.version>
        <commons-codec.version>1.9commons-codec.version>
        <commons-dbcp.version>1.4commons-dbcp.version>
        <hibernate.validator.version>5.0.2.Finalhibernate.validator.version>
        <jetty.version>8.1.8.v20121106jetty.version>
        <slf4j.version>1.7.5slf4j.version>
        <testng.version>6.8.7testng.version>
    properties>
    <dependencies>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-context-supportartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>commons-dbcpgroupId>
            <artifactId>commons-dbcpartifactId>
            <version>${commons-dbcp.version}version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>${mysql.version}version>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>${servlet.version}version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>${aspectj.version}version>
        dependency>


        <dependency>
            <groupId>org.testnggroupId>
            <artifactId>testngartifactId>
            <version>${testng.version}version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>${spring.version}version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.codehaus.cargogroupId>
            <artifactId>cargo-core-api-containerartifactId>
            <version>1.2.3version>
        dependency>
    dependencies>

    <build>
        <plugins>
            
            <plugin>
                <groupId>org.mortbay.jettygroupId>
                <artifactId>maven-jetty-pluginartifactId>
                <version>6.1.25version>
                <configuration>
                    <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>8000port>
                            <maxIdleTime>60000maxIdleTime>
                        connector>
                    connectors>
                    <contextPath>/bbscontextPath>
                    <scanIntervalSeconds>0scanIntervalSeconds>
                configuration>
            plugin>

            <plugin>             <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-surefire-pluginartifactId>
                <version>2.17version>
                <configuration>
                    <parallel>methodsparallel>
                    <threadCount>10threadCount>
                configuration>
            plugin>
        plugins>
    build>
project>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Titletitle>
head>
<body>
  <form action="/index.html" method="post">
     用户名 <input type="text" name="username">
      密码<input type="text" name="password">
      <input type="submit" value="提交">
  form>
  body>
html>

main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>title>
head>
<body>
当前用户为${user.username};
body>
html>

运行测试
输入
1
这里写图片描述
这里写图片描述
2
这里写图片描述

这里写图片描述

你可能感兴趣的:(maven,spring,maven)