Spring+SpringMVC+Mybatis+Maven+Eclipse整合--SSM

原创 2016年08月30日 16:37:42

LZ最近在阿里巴巴实习,做java Web开发,因此不可避免对一些主流的web开发框架进行接触,那么我在进行相应的开发前,又重新研习了一遍SSM框架的搭建,以下是我的一些步骤,与大家分享:

一、首先是pom.xml文件,此文件用于部署一些相关的jar包,我们可以利用这个文件来加载所需要的jar包,而通过配置相关的SSM,我们可以看到需要配置SSM框架,需要用到的jar包。当然,这里我主要用到的几个包是

Spring3.2.3 RELAEASE

SpringMVC 3.2.3 RELEASE

Mybatis 3.1.1

<span style="font-size:14px;"><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.springstudy</groupId>
  <artifactId>hello</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>hello Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-webmvc</artifactId>
    	<version>3.2.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.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>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.34</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.1.41</version>
    </dependency>
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.10</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.9</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>hello</finalName>
  </build>
</project>
</span>

因为我配置的eclipse中没有自动更新此pom.xml文件引入的jar包,所以我需要手动在eclipse中,在项目的maven中用update projects来进行自动更新所需要的jar包。

二、接下来是jdbc属性文件

jdbc.properties   此文件主要用来放置连接的数据库的密码和姓名

<span style="font-size:14px;">jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/test
jdbc_username=root
jdbc_password=123456</span>

三、mybatis-spring.xml文件,此文件是用来spring和mybatis整合用的,主要的作用就是用来扫描、自动注入、配置数据库。

<span style="font-size:14px;"><?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:jee="http://www.springframework.org/schema/jee"
     xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/jee
       http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.1.xsd">
       
       <!-- 开启扫描和自动注解功能-->
       <context:annotation-config/>
       <context:component-scan base-package="com.sprintstudy.DAO"/>
       <context:component-scan base-package="com.srpingstudy.DAOImpl"/>
       <context:component-scan base-package="com.springstudy.DO"/>
       <context:component-scan base-package="com.springstudy.service"/>
       <context:component-scan base-package="com.springstudy.serviceImpl"/>
       <mvc:annotation-driven/>
       <!-- 配置相关的jdbc的配置文件,用于将jdbc的信息放置在文件中-->
       <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
           <property name="location" value="classpath:configs/jdbc.properties"/>
       </bean>
       <!-- 配置数据库-->
       <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
           <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>
       
       <!-- spring和mybatis的完美结合,不需要mybatis的配置映射文件-->
       <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
           <property name="dataSource" ref="dataSource"/>
           <property name="mapperLocations" value="classpath:configs/userMapper.xml"></property>
       </bean>
       <!-- DAO接口所在的包,spring会自动扫描其下面的类,在后序的代码里就可以直接调用这个包与sqlSessionFactory进行整合,用于连接相关的-->
       <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
           <property name="basePackage" value="com.springstudy.DAO"/>
           <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
       </bean>
       <!-- 事务管理-->
       <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
           <property name="dataSource" ref="dataSource"/>
       </bean>
</beans>
       </span>

当然,我需要建立的数据库在我本地的mysql中,
create table tuser
(
     user_id int(11) NOT NULL auto_increment,
     user_name  varchar(30) NOT NULL,
     user_password varchar(10) NOT NULL
     primary key(user_id)
);

那么在项目中也需要一个pojo类,将这个表转化成一个User对象。

package com.springstudy.DO;

import org.springframework.stereotype.Repository;

@Repository("User")
public class User 
{
	private Integer user_id;
	private String user_name;
	private String user_password;
	public Integer getUser_id() 
	{
		return user_id;
	}
	public void setUser_id(Integer user_id) 
	{
		this.user_id = user_id;
	}
	public String getUser_name() 
	{
		return user_name;
	}
	public void setUser_name(String user_name) 
	{
		this.user_name = user_name;
	}
	public String getUser_password() 
	{
		return user_password;
	}
	public void setUser_password(String user_password) 
	{
		this.user_password = user_password;
	}
	
}

相关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="com.springstudy.DAO.user">
     
     <resultMap id="BaseResultMap" type="com.springstudy.DO.User">
        <id column = "user_id" property="user_id" jdbcType="INTEGER" />
        <result column = "user_name" property="user_name" jdbcType="VARCHAR" />
        <result column = "user_password" property="user_password" jdbcType="VARCHAR"/>
     </resultMap>
     
     <select id="selectUserById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        SELECT * FROM tuser WHERE user_id = #{user_id}
     </select>
</mapper>



四、是SpringMVC的整合,也就是spring-mvc.xml文件,此文件是springMVC的配置文件,我们可以通过配置这个,来给Dispatcher来进行分配。

<?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:jee="http://www.springframework.org/schema/jee"
     xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/jee
       http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.1.xsd">
       
       <context:annotation-config/>
       <context:component-scan base-package="com.springstudy.controller"/>   <!-- springMVC配置文件中存放controller层的扫描文件 -->
       <mvc:annotation-driven/>
       
       <mvc:resources mapping="/styles/**" location="/styles/"/>
       <mvc:resources mapping="/scripts/**" location="/scripts/" />
       <mvc:resources mapping="/images/**" location="/images/" />
       
       <bean
          class ="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/WEB-INF/views/" />
          <property name="suffix" value=".jsp" />
       </bean>
      
</beans>


此配置文件中,我们可以看到,有一些前缀和后缀的相关信息,我们可以通过添加相关的前缀和后缀,来分配给具体的vm页面渲染。

五、web.xml

此文件是spring框架的配置文件,任何的类和对象从加载开始都是从这个文件开始,我们可以直接从这里读取到相关的内容。

<?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="hello" version="2.5">
    <display-name>Archetype Created Web Application</display-name>
    <description>springMVC</description>
    <!-- spring的配置文件-->
    <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:configs/mybatis-spring.xml</param-value>
    </context-param>
    
    <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- springMVC配置 -->
    <servlet>
        <servlet-name>Dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:configs/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <!-- springMVC请求后缀 -->
    <servlet-mapping>
         <servlet-name>Dispatcher</servlet-name>
         <url-pattern>/</url-pattern>
    </servlet-mapping>

    <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>
</web-app>

六、我的controller层代码

package com.springstudy.controller;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.springstudy.DO.User;
import com.springstudy.service.UserService;

@Controller
public class UserInfoController
{
	@Resource
	private UserService userservice;
	
	@RequestMapping(value = "/user",method = RequestMethod.POST)
	@ResponseBody
	public User selectById(@RequestHeader("user_id") int user_id)
	{
		return userservice.selectById(user_id);
	}
}


七、Service层代码

package com.springstudy.service;

import javax.annotation.Resource;

import com.springstudy.DO.User;
import com.springstudy.serviceImpl.UserServiceImpl;


public interface UserService 
{
	public User selectById(int id);
}


具体的实现类

package com.springstudy.serviceImpl;


import javax.annotation.Resource;

import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.springstudy.DAO.user;
import com.springstudy.DO.User;
import com.springstudy.service.UserService;

@Service("UserServiceImpl")
public class UserServiceImpl implements UserService
{
	@Resource 
	private user u;
	
	public User selectById(int user_id)
	{
		return this.u.selectUserById(user_id);
	}
	
}


八、DAO层的代码

package com.springstudy.DAO;

import org.springframework.stereotype.Repository;

import com.springstudy.DO.User;


public interface user 
{
	public User selectUserById(Integer user_id);
}

这个DAO层的代码在mybatis-spring.xml中,已经和相关的UserMapper.xml文件进行了匹配,所以我们可以直接拿来用即可。


九、这里我没有写相关的页面来具体展现,而是用了接口的形式,通过Postman来测试。






版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

SSM框架——使用MyBatis Generator自动创建代码

这两天需要用到MyBatis的代码自动生成的功能,由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生...

【SSM】Eclipse使用Maven创建Web项目+整合SSM框架

自己接触ssm框架有一段时间了,从最早的接触新版ITOO项目的(SSM/H+Dobbu zk),再到自己近期来学习到的《淘淘商城》一个ssm框架的电商项目。用过,但是还真的没有自己搭建过,一直都是用别...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

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

使用SSM(Spring、SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方。之前没有记录SSM整合...

Log4j配置详解

来自: http://www.blogjava.net/zJun/archive/2006/06/28/55511.html Log4J的配置文件(Configuration File)就是用来...

Eclipse Maven建web项目 与SSM三大框架整合教程(Spring+SpringMVC+MyBatis)

Eclipse Maven建web项目 地址:http://dead-knight.iteye.com/blog/1841675 1.创建Maven项目: 点击“File”菜单,或者通过工具栏...

Spring+SpringMVC+MyBatis(SSM)框架整合

本博客主要整理了Spring+SpringMVC+MyBatis+c3p0+ehcache在整合过程的一些细节。 框架简介:      Spring是一个轻量级的控制反转(IoC)和面向切面(ao...

Spring,SpringMVC,Mybatis (SSM)框架的搭建

转载请注明出处:   搭建SSM框架参照一下步骤: 1.提供ssm所需jar包     这些jar包包括三个框架所需要的,就不一一列举所属了 aopalliance-1.0.jar asm-3.3.1...

[框架]eclipse搭建ssm框架 一

虽然现在也做过一些项目,但是自己从头搭起来的框架几乎没有,所以这两天自己搭了一下ssm的框架,下面写一下框架的搭建过程。并且给出增删改查四条线来方便大家熟悉代码。环境准备maven3.2.3 ecl...

eclipse + maven搭建SSM框架

0、系统环境 1)Windows 10 企业版 2)JDK 1.8.0_131 3)Eclipse Java EE IDE for Web Developers  Version: Neon.3...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)