intellij idea 使用maven构建SSM项目

环境条件

Tomcat、maven、jdk1.8、intellij idea、mysql

源码下载:

http://download.csdn.net/download/m0_37681914/10112551

最终的项目:

这里写图片描述
这是一个简单的登录系统。
这是演示结果:
这里写图片描述

这里写图片描述

这里写图片描述

主要步骤

1.创建一个Maven、web项目

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

稍等进度完成
这里写图片描述
要是太慢了,也可以自己创建,主要步骤如下:
这里写图片描述

这里写图片描述

这里写图片描述

好了,再创建controller包、mapper包、service包、service.imp包、entity包。
经过上面的步骤后,项目的样子应该是下面的样子:

这里写图片描述

2. 创建配置文件

不管三七二十一,先把基本的.xml都创建了吧
这里写图片描述

配置pom.xml

这是配置我们的依赖jar包,大概需要以下:

javax.servlet-api
spring-webmvc
spring-tx
mybatis
mybatis-spring
commons-dbcp
spring-jdbc
mysql-connector-java
Junit4

pom配置文件,请参考源码。

配置mybatisConfig.xml

这是mybatis核心配置文件

<?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>
配置applicationContext.xml

这是spring核心配置

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       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
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
       default-autowire="byName" default-lazy-init="false">

    <context:annotation-config />


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver"
          p:url="jdbc:mysql://127.0.0.1:3306/ssm?characterEncoding=utf8"
          p:username="root" p:password="123456" p:maxActive="10" p:maxIdle="10">
    </bean>

    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--dataSource属性指定要用到的连接池-->
        <property name="dataSource" ref="dataSource" />
        <!--configLocation属性指定mybatis的核心配置文件-->
        <property name="configLocation" value="classpath:mybatisConfig.xml" />
        <!-- 所有配置的mapper文件 -->
        <property name="mapperLocations" value="classpath*:mapper/*.xml" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="mapper" />
    </bean>

    <bean class="service.imp.UserServiceImp" id="userService" autowire="byName"/>
</beans>
配置spring-mvc-servlet.xml

这是springMVC核心文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

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

    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>
配置web.xml

目的是为了将ssm整合到web中

<?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>ssm</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>encodingFilter</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>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>
      org.springframework.web.context.ContextCleanupListener
    </listener-class>
  </listener>
  <servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

3. 开始写代码罗!

sql
create database ssm;
use ssm;
Create table user( id int primary key auto_increment,name varchar(10) not null unique,password varchar(10) not null);
#创建一个测试的账号
Insert into user value(10001,'admin','admin');
entity层

User类

package entity;

public class User {
    private int id;
    private String name;
    private String password;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

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

UserMapper接口

package mapper;

import entity.User;

public interface UserMapper {
    User findUser(User user) throws Exception;
}

UserMapper.xml

这里写图片描述

<?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="mapper.UserMapper">
    <select id="findUser" parameterType="entity.User" resultType="entity.User">
        <if test="name != null and name !='' and password != null and password !=''">
            SELECT * FROM user WHERE name='${name}' and password='${password}'
        </if>
    </select>
</mapper>
service层

UserService接口

package service;

import entity.User;

public interface UserService {
    User findUser(String name, String password) throws Exception;
}

UserServiceImp类,注意@Autowired注解哦,spring能自动帮我们注入userMapper

package service.imp;

import entity.User;
import mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import service.UserService;

public class UserServiceImp  implements UserService{
    @Autowired
    UserMapper userMapper;

    public User findUser(String name, String password) throws Exception {
        boolean flag = checkString(name,3,10)&&checkString(password,4,10);
        if (!flag)
            return null;
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        return userMapper.findUser(user);
    }
    private boolean checkString(String str,int minL,int maxL){
        return str!=null&&str.length()>=minL&&str.length()<=maxL;
    }
}
controller层,springMVC中的视图控制类

UserLogin类

package controller;

import entity.User;
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 service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

@Controller
@RequestMapping("/user")
public class UserLogin {

    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public ModelAndView login(HttpServletRequest request,HttpServletResponse response){
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        ModelAndView modelAndView;
        try {
            User user = userService.findUser(name,password);
            Objects.requireNonNull(user);
            modelAndView = new ModelAndView("hello");
            modelAndView.addObject("id",user.getId());
            modelAndView.addObject("name",user.getName());
        } catch (Exception e) {
            modelAndView = new ModelAndView("login");
            modelAndView.addObject("info","您的账号或密码错误,请输入正确的账号和密码");
        }
        return modelAndView;
    }
}

4. 最后的jsp

index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<title>主页</title>
<body>
<h2>请先登录</h2>
<form action="/user/login" method="post">
    <input type="text" name="name" value="admin"><br>
    <input type="password" name="password" value="admin"><br>
    <input type="submit" value="登录">
</form>
</body>
</html>
hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<title>欢迎${name}</title>
<body>
    <table>
        <tr>
            <th>id</th>
            <th>name</th>
        </tr>
        <tr>
            <td>${id}</td>
            <td>${name}</td>
        </tr>
    </table>
</body>
</html>
login.jsp
<%--
  Created by IntelliJ IDEA.
  User: weijun.zou
  Date: 2017/11/9
  Time: 13:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
    <form action="/user/login" method="post">
        <h2>${info}</h2>
       <input type="text" name="name"><br>
        <input type="password" name="password"><br>
        <input type="submit" value="登录">
    </form>
</body>
</html>

使用maven构建简单的ssm项目就完成了!
中间还写了一个测试类:
这里写图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值