Maven搭建SSM框架(Spring+SpringMVC+MyBatis)

一、概述:

  Spring是一个轻量级开发框架,好比一个大工厂(容器),可以将所有对象的创建和依赖关系交给spring管理。

  SpringMVC是一个基于mvc的web框架。spring mvc是spring框架的一个模块,springmvc和spring无需通过中间整合层进行整合。

  MyBatis是一个持久层的框架,在使用上相比Hibernate更加灵活,可以控制sql的编写,使用 XML或注解进行相关的配置.

二、搭建好项目的运行环境(java环境,Maven,Mysql,Tomcat),所使用工具Eclipse。具体的环境搭建请自行搜一下资料。jdk1.8+tomcat8.5

三、开始搭建

  首先看一下项目结构。controller,mapper,pojo,service

  

  1.创建数据库表user,添加一条测试数据

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(40) NOT NULL,
  `password` varchar(255) NOT NULL,
  `age` int(4) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
insert  into `user_t`(`id`,`user_name`,`password`,`age`) values (1,‘admin‘,‘123456‘,21)

  2. 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.iris</groupId>
  <artifactId>ssm</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>ssm Maven Webapp</name>
  <url>http://maven.apache.org</url>
   <properties>
        <spring.version>4.0.5.RELEASE</spring.version>
        <mybatis.version>3.2.1</mybatis.version>
        <log4j.version>1.2.12</log4j.version>
        <mysql.version>5.1.35</mysql.version>
        <shiro.version>1.2.4</shiro.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-beans</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>
    <!--spring单元测试依赖 -->
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-test</artifactId>
         <version>${spring.version}</version>
         <scope>test</scope>
     </dependency>

      <!-- spring webmvc相关jar -->
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-webmvc</artifactId>
         <version>${spring.version}</version>
     </dependency>
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-web</artifactId>
         <version>${spring.version}</version>
     </dependency>

    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>

      <!-- mysql驱动包 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>

    <!-- alibaba data source 相关jar包-->
     <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>druid</artifactId>
         <version>0.2.23</version>
     </dependency>

     <!-- alibaba fastjson 格式化对 -->
     <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.1.41</version>
     </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.5</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>

     <!-- logback start -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
    </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>
    <!-- 添加servlet3.0核心包 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.2-b01</version>
    </dependency>
    <!-- jstl -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <!--单元测试依赖 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>ssm</finalName>
  </build>
</project>

  3.配置文件包括

    jdbc,properties   

jdbc.driver=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456

    log4j.properties

#\u5B9A\u4E49LOG\u8F93\u51FA\u7EA7\u522B
log4j.rootLogger=INFO,Console,File
#\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u5730\u4E3A\u63A7\u5236\u53F0
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=system.out
#\u53EF\u4EE5\u7075\u6D3B\u5730\u6307\u5B9A\u65E5\u5FD7\u8F93\u51FA\u683C\u5F0F\uFF0C\u4E0B\u9762\u4E00\u884C\u662F\u6307\u5B9A\u5177\u4F53\u7684\u683C\u5F0F %d [%t]%-5p [%c] - %m%n
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

#\u6587\u4EF6\u5927\u5C0F\u5230\u8FBE\u6307\u5B9A\u5C3A\u5BF8\u7684\u65F6\u5019\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6
log4j.appender.File = org.apache.log4j.RollingFileAppender
#\u6307\u5B9A\u8F93\u51FA\u76EE\u5F55
log4j.appender.File.File = f:/logs/ssms.log
#\u5B9A\u4E49\u6587\u4EF6\u6700\u5927\u5927\u5C0F
log4j.appender.File.MaxFileSize = 10MB
# \u8F93\u51FA\u6240\u4EE5\u65E5\u5FD7\uFF0C\u5982\u679C\u6362\u6210DEBUG\u8868\u793A\u8F93\u51FADEBUG\u4EE5\u4E0A\u7EA7\u522B\u65E5\u5FD7
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

    spring-mvc.xml

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

    <!-- 扫描所有的 controller -->
    <context:component-scan base-package="com.iris.controller" />

    <!-- 启动注解驱动 SpringMVC 功能 -->
    <mvc:annotation-driven />

    <!-- 定义跳转的文件的前后缀,视图模式配置 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
         <property name="prefix" value="/WEB-INF/jsp/" />
         <property name="suffix" value=".jsp" />
    </bean>

    <!-- 静态资源访问(不拦截此目录下的东西的访问) -->
    <mvc:resources location="/js/" mapping="/js/**" />

</beans>

    spring-mybatis.xml

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!-- 自动扫描 将标注Spring注解的类自动转化Bean -->
    <context:component-scan base-package="com.iris" />
    <!-- 导入数据库配置文件 -->
    <context:property-placeholder location="classpath:config/jdbc.properties"/>
    <!-- druid 数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url"  value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 扫描mybatis 配置文件-->
    <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:com/iris/mapper/*.xml" />
    </bean>
    <!-- 扫描Mapper 配置文件-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.iris.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>
</beans>

    web.xml

<?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"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>ssm</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
      <!-- 读取Spring配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:config/spring-*.xml
        </param-value>
    </context-param>

    <!-- Spring MVC配置 -->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:config/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
      <!-- post乱码处理 -->
      <filter>
        <filter-name>CharacterEncodingFilter</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>
      </filter>
      <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
</web-app>

  4.pojo实体

package com.iris.pojo;

public class User {
    private Integer id;
    private String user_name;
    private String password;
    private Integer age;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUser_name() {
        return user_name;
    }
    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }

}

  5.dao层mapper接口

package com.iris.mapper;
import java.util.List;
import com.iris.pojo.User;
public interface UserMapper {
    Boolean  addUser(User user);
    Boolean  delUser(Integer id);
    Boolean  modUser(User user);
    List<User>  getUserList();
    User  getUserById(Integer id);
}
<?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.iris.mapper.UserMapper">
      <select id="getUserList" resultType="com.iris.pojo.User">
          select * from user
      </select>
    <select id="getUserById" resultType="com.iris.pojo.User" parameterType="int">
          select * from user t where t.id=#{id}
      </select>
</mapper>

  6.controller控制层,这里将返回数据类型统一封装到ResultBean

package com.iris.controller;

import java.io.Serializable;

import org.springframework.web.bind.annotation.RestController;
@RestController
public class ResultBean<T>  implements Serializable{
    private static final long serialVersionUID=1L;
    private static final int NO_LOGIN=-1;
    private static final int SUCCESS=0;
    private static final int FAIL=1;
    private static final int NO_PERMISSION=2;
    private String msg="success";
    private int code=SUCCESS;
    private T data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public ResultBean() {
        super();
    }

    public ResultBean(T data) {
        super();
        this.data=data;
    }

    public ResultBean(Throwable e) {
        super();
        this.msg=e.toString();
        this.code=FAIL;
    }

}
package com.iris.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.iris.pojo.User;
import com.iris.service.UserService;
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/add")
    public ResultBean<Boolean> addUser(User user){
        return new ResultBean<Boolean>(userService.addUser(user));
    }
    @RequestMapping("/del")
    public ResultBean<Boolean> delUser(Integer id){
        return new ResultBean<Boolean>(userService.delUser(id));
    }
    @RequestMapping("/mod")
    public ResultBean<Boolean> modUser(User user){
        return new ResultBean<Boolean>(userService.modUser(user));
    }
    @RequestMapping("/getAll")
    public ResultBean<List<User>> getUserList(){
        return new ResultBean<List<User>>(userService.getUserList());
    }
    @RequestMapping("/getUser")
    public ResultBean<User> getUserById(Integer id){
        return new ResultBean<User>(userService.getUserById(id));
    }
}

  7.service服务接口以及实现类

package com.iris.service;
import java.util.List;
import com.iris.pojo.User;
public interface UserService {
    Boolean  addUser(User user);
    Boolean  delUser(Integer id);
    Boolean  modUser(User user);
    List<User>  getUserList();
    User  getUserById(Integer id);
}
package com.iris.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iris.mapper.UserMapper;
import com.iris.pojo.User;
import com.iris.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    public Boolean addUser(User user) {
        return userMapper.addUser(user);
    }

    public Boolean delUser(Integer id) {
        return userMapper.delUser(id);
    }

    public Boolean modUser(User user) {
        return userMapper.modUser(user);
    }

    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    public List<User> getUserList() {
        return userMapper.getUserList();
    }

}

以上只是一个小白查阅了相关资料做的一个总结,如有问题敬请指正。

原文地址:https://www.cnblogs.com/JonlyZhang/p/9866608.html

时间: 2024-11-05 12:23:03

Maven搭建SSM框架(Spring+SpringMVC+MyBatis)的相关文章

Maven 搭建 SSM框架——Spring+SpringMVC+Mybatis的搭建教程

一:概述 SSM框架在项目开发中经常使用到,相比于SSH框架,它在仅几年的开发中运用的更加广泛. Spring作为一个轻量级的框架,有很多的拓展功能,最主要的我们一般项目使用的就是IOC和AOP.SpringMVC是Spring实现的一个Web层,相当于Struts的框架,但是比Struts更加灵活和强大!Mybatis是 一个持久层的框架,在使用上相比Hibernate更加灵活,可以控制sql的编写,使用 XML或注解进行相关的配置!根据上面的描述,学习SSM框架就非常的重要了! 二:搭建一个

Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World(转发)

[JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World 来源:http://blog.csdn.net/zhshulin/article/details/37956105?utm_source=tuicool&utm_medium=referral(以下博文对原博文有改动和补充) 博文里红底白字的,为注意修改的地方,在这里先声明 源码:Maven&SSM框架 - Hello World 开发环境: Eclipse Java EE I

[JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis)

来源:http://blog.csdn.net/zhshulin/article/details/37956105?utm_source=tuicool&utm_medium=referral(以下博文对原博文有改动和补充) 开发环境: Eclipse Java EE IDE for Web Developers. Version: Mars.2 Release (4.5.2) apache-tomcat-8.0.33 jdk1.8.0_77 MySQL 5.0.11-dev(官网下载需要账号登

SSM框架——Spring+SpringMVC+Mybatis的搭建教程

一:概述 SSM框架在项目开发中经常使用到,相比于SSH框架,它在仅几年的开发中运用的更加广泛. Spring作为一个轻量级的框架,有很多的拓展功能,最主要的我们一般项目使用的就是IOC和AOP. SpringMVC是Spring实现的一个Web层,相当于Struts的框架,但是比Struts更加灵活和强大! Mybatis是 一个持久层的框架,在使用上相比Hibernate更加灵活,可以控制sql的编写,使用 XML或注解进行相关的配置! 根据上面的描述,学习SSM框架就非常的重要了! 二:搭

eclipse 搭建ssm (Spring+SpringMVC+MyBatis)

eclipse 开发软件搭建简单 ssm 开源框架并连接MySQL查询 前提:准备好搭建框架的jar.连接数据库的jar.日志架包jar和tomcat 不多说 我们开始 首先 创建项目File ->New-> Dynamic Web Project (名字随便起 我这里就起 ssm ) 在src下创建基本的包 导入jar的包 将jar包放进 WebContent ->web-INF ->lib里 (?? 一大堆 不知道哪个没有用  放进去就对了) jar包 链接:https://p

SSM框架Spring+SpringMVC+MyBatis——详细整合教程

摘要: 包括SQL Maps和Data Access ObjectsDAOMyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的... 摘要:   spring MVC属于SpringFrameWork的后续产品已经融合在Spring Web Flow里面.Spring MVC 分离了控制器.模型对... 1.基本概念 1.1.Spring Spring是一个开源框架Spring是于2003 年兴起的一个轻量级的Java 开发框架由Rod Johnson 在其著作Expert 

多工程:基于Maven的SSM(Spring,SpringMvc,Mybatis)整合的web工程(中)

上篇用了单工程创建了SSM整合的web工程(http://www.cnblogs.com/yuanjava/p/6748956.html),这次我们把上篇的单工程改造成为多模块工程 一:创建对应的多工程 首先原工程有对应的包如下 因为原单工程是 contoller 调用 service ,service 调用 mapper ,mapper 调用pojo 因此把对应的 service  mapper  pojo 分别拆分出去当做模块  然后把utils公用的抽出去当做common模块 最后创建出来

详解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)(上)

SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架. 其中spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架. SpringMVC分离了控制器.模型对象.分派器以及处理程序对象的角色,这种分离让它们更容易进行定制. MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架. 本文旨在快速且详细的介绍intellij idea 搭建SS

使用maven搭建ssm框架的javaweb项目

目前主流的javawe项目,常会用到ssm(Spring+Spring MVC+Mybatis)框架来搭建项目的主体框架,本篇介绍搭建SSM矿建的maven项目的实施流程.记之共享! 一.SSM框架介绍 SSM框架是指:Spring+Spring MVC+Mybatis. [Spring] 轻量--从大小与开销两方面而言Spring都是轻量的,此外,Spring是非侵入式的:典型地,Spring应用中的对象不依赖于Spring的特定类.简单说就是自己需要什么就导入对应的jar的即可,而不是侵入式