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

Spring Springmvc Mybatis--ssm的整合

一、搭建环境sts工具:

 

根据上图创建springproject

 

上图为项目结构 在此基础上进行搭建ssm项目。

二、添加依赖pom文件添加坐标依赖:

<?xml version="1.0" encoding="UTF-8"?>

<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.cn</groupId>

<artifactId>ssm</artifactId>

<name>SSMProject</name>

<packaging>war</packaging>

<version>1.0.0-BUILD-SNAPSHOT</version>

<properties>

<java-version>1.6</java-version>

<org.springframework-version>3.1.1.RELEASE</org.springframework-version>

<org.aspectj-version>1.6.10</org.aspectj-version>

<org.slf4j-version>1.6.6</org.slf4j-version>

</properties>

<dependencies>

<!--rabbitmq依赖 -->

<dependency>

<groupId>org.springframework.amqp</groupId>

<artifactId>spring-rabbit</artifactId>

<version>1.3.5.RELEASE</version>

</dependency>

<!-- 添加Servlet支持 -->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>3.1.0</version>

</dependency>

<dependency>

<groupId>javax.servlet.jsp</groupId>

<artifactId>javax.servlet.jsp-api</artifactId>

<version>2.3.1</version>

</dependency>

<dependency>

<groupId>org.quartz-scheduler</groupId>

<artifactId>quartz</artifactId>

<version>2.2.2</version>

<exclusions>

<exclusion>

<groupId>c3po</groupId>

<artifactId>c3po</artifactId>

</exclusion>

</exclusions>

</dependency>

<!-- rabbitmq客户端 -->

<dependency>

<groupId>com.rabbitmq</groupId>

<artifactId>amqp-client</artifactId>

<version>3.0.4</version>

</dependency>

<dependency>

<groupId>commons-lang</groupId>

<artifactId>commons-lang</artifactId>

<version>2.6</version>

</dependency>

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-lang3</artifactId>

<version>3.1</version>

</dependency>

<!-- 添加jtl支持 -->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

<version>1.2</version>

</dependency>

<dependency>

<groupId>org.quartz-scheduler</groupId>

<artifactId>quartz</artifactId>

<version>2.2.0</version>

</dependency>

<!-- 添加Spring支持-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>4.1.7.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>4.1.7.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>4.1.7.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-tx</artifactId>

<version>4.1.7.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>4.1.7.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>4.1.7.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>4.1.7.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aop</artifactId>

<version>4.1.7.RELEASE</version>

</dependency>

<!-- <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId>

<version>2.13.0-rc2</version> </dependency> -->

<dependency>

<groupId>org.mongodb</groupId>

<artifactId>mongo-java-driver</artifactId>

<version>3.0.2</version>

</dependency>

<!-- mongodb依赖 -->

<dependency>

<groupId>org.springframework.data</groupId>

<artifactId>spring-data-mongodb</artifactId>

<version>1.8.4.RELEASE</version>

<exclusions>

<exclusion>

<groupId>org.mongodb</groupId>

<artifactId>mongo-java-driver</artifactId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aspects</artifactId>

<version>4.1.7.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>4.1.7.RELEASE</version>

</dependency>

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>1.2.3</version>

</dependency>

<!-- 添加日志支持 -->

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

<version>1.7.12</version>

</dependency>

<!-- 添加mybatis支持 -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>3.3.0</version>

</dependency>

<!-- jdbc驱动包 -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.37</version>

</dependency>

<!-- 添加shiro支持 -->

<dependency>

<groupId>org.apache.shiro</groupId>

<artifactId>shiro-core</artifactId>

<version>1.2.4</version>

</dependency>

<dependency>

<groupId>org.apache.shiro</groupId>

<artifactId>shiro-web</artifactId>

<version>1.2.4</version>

</dependency>

<dependency>

<groupId>org.apache.shiro</groupId>

<artifactId>shiro-spring</artifactId>

<version>1.2.4</version>

</dependency>

<!-- 添加百度编辑器ueditor支持 -->

<dependency>

<groupId>commons-fileupload</groupId>

<artifactId>commons-fileupload</artifactId>

<version>1.3.1</version>

</dependency>

<!-- apache公共包 -->

<dependency>

<groupId>commons-codec</groupId>

<artifactId>commons-codec</artifactId>

<version>1.10</version>

</dependency>

<dependency>

<groupId>commons-lang</groupId>

<artifactId>commons-lang</artifactId>

<version>2.5</version>

</dependency>

<dependency>

<groupId>commons-beanutils</groupId>

<artifactId>commons-beanutils</artifactId>

<version>1.8.0</version>

</dependency>

<dependency>

<groupId>commons-collections</groupId>

<artifactId>commons-collections</artifactId>

<version>3.2.1</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>1.1.1</version>

</dependency>

<dependency>

<groupId>net.sf.ezmorph</groupId>

<artifactId>ezmorph</artifactId>

<version>1.0.6</version>

</dependency>

<!-- 添加lucene支持 -->

<dependency>

<groupId>org.apache.lucene</groupId>

<artifactId>lucene-core</artifactId>

<version>5.3.1</version>

</dependency>

<dependency>

<groupId>org.apache.lucene</groupId>

<artifactId>lucene-analyzers-common</artifactId>

<version>5.3.1</version>

</dependency>

<dependency>

<groupId>org.apache.lucene</groupId>

<artifactId>lucene-analyzers-smartcn</artifactId>

<version>5.3.1</version>

</dependency>

<dependency>

<groupId>org.apache.lucene</groupId>

<artifactId>lucene-queryparser</artifactId>

<version>5.3.1</version>

</dependency>

<dependency>

<groupId>org.apache.lucene</groupId>

<artifactId>lucene-highlighter</artifactId>

<version>5.3.1</version>

</dependency>

 

<!-- 添加连接池druid支持 -->

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid</artifactId>

<version>1.0.16</version>

</dependency>

<!-- 添加jsoup支持 -->

<dependency>

<groupId>org.jsoup</groupId>

<artifactId>jsoup</artifactId>

<version>1.8.3</version>

</dependency>

<!-- redis的坐标 -->

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>2.7.0</version>

</dependency>

<!-- JSON begin 为了配置返回类型为json对象的 -->

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-core</artifactId>

<version>2.1.4</version>

</dependency>

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.1.4</version>

</dependency>

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-annotations</artifactId>

<version>2.1.4</version>

</dependency>

<dependency>

<groupId>com.fasterxml.jackson.module</groupId>

<artifactId>jackson-module-jaxb-annotations</artifactId>

<version>2.1.4</version>

</dependency>

<dependency>

<groupId>com.fasterxml.jackson.jaxrs</groupId>

<artifactId>jackson-jaxrs-json-provider</artifactId>

<version>2.1.4</version>

<!-- Jackson2.2.0 版本对于 CXF2.7.3 不支持-->

<!-- http://www.marshut.com/krrqx/fasterxml-jackson-2-2-provider-no-longer-works-with-cxf-jax-rs.pdf -->

<!-- http://cxf.547215.n5.nabble.com/Creating-input-values-on-WADL-td5728910.html -->

</dependency>

<!--添加poi -->

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>3.9</version>

</dependency>

<!-- gson的解析jar-->

<dependency>

<groupId>com.google.code.gson</groupId>

<artifactId>gson</artifactId>

<version>2.3.1</version>

</dependency>

<!--添加websocket jar -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-websocket</artifactId>

<version>4.0.1.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-messaging</artifactId>

<version>4.0.1.RELEASE</version>

</dependency>

<!-- jsoup用于网络爬虫 -->

<dependency>

<groupId>org.jsoup</groupId>

<artifactId>jsoup</artifactId>

<version>1.8.3</version>

</dependency>

</dependencies>

<build>

<plugins>

<plugin><artifactId>maven-eclipse-plugin</artifactId>

<version>2.9</version>

<configuration>

<additionalProjectnatures><projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>

</additionalProjectnatures>

<additionalBuildcommands><buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>

</additionalBuildcommands>

<downloadSources>true</downloadSources>

<downloadJavadocs>true</downloadJavadocs>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>2.5.1</version>

<configuration>

<source>1.6</source>

<target>1.6</target><compilerArgument>-Xlint:all</compilerArgument>

<showWarnings>true</showWarnings>

<showDeprecation>true</showDeprecation>

</configuration>

</plugin>

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>exec-maven-plugin</artifactId>

<version>1.2.1</version>

<configuration>

<mainClass>org.test.int1.Main</mainClass>

</configuration>

</plugin>

</plugins>

</build>

</project>

以上依赖包含了所有能用到坐标 例如 redismongodbwebsocketpoirabbitmqmybatisspring等依赖坐标。

//会需要很长时间下载相关依赖可根据自身情况选择依赖

三、开始正式搭建项目:

 

Java代码进行分包处理。

四、配置配置文件:

创建applicationContext.xml,mybatis-config.xml

1、根据Spring官网粘贴头文件以及相关连接。

2、配置数据源来连接数据库。

<?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:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"

xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:websocket="http://www.springframework.org/schema/websocket"

xsi:schemaLocation="    

        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  

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

         <!-- 配置MySQL的数据源-->

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

<!--连接驱动-->

<property name="url" value="jdbc:mysql://localhost:3306/tiangou?useUnicode=true&characterEncoding=UTF-8"/>

<property name="username" value="root"/>

<property name="password" value="root"/>

</bean>

<!-- 数据源配置结束 -->

<!-- 需要配置和mybatis要扫描的mapper文件  配置mybatis的开始 -->

<!-- 配置mybatissqlSessionFactory  -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<!-- 自动扫描mappers.xml文件  需要改成自己的dao层的mapper.xml路径-->

<property name="mapperLocations" value="classpath:com/cn/ssm/dao/*.xml"></property>

<!-- mybatis配置文件  需要引进刚刚创建的mybatis-config.xml-->

<property name="configLocation" value="classpath:mybatis-config.xml"></property>

</bean>

<!-- 配置mybatis结束 -->

<!-- 扫描旗下的类来加载-->

<!-- DAO接口所在包名,Spring会自动查找其下的类http://www.cnblogs.com/sharpest/p/6097747.html -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<!--后面的value值换成你自己的dao层目录,扫描所有接口,然后创建各自接口的动态代理类-->

<property name="basePackage" value="com.cn.ssm.dao" />

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>

</bean>

<!-- (事务管理)transaction manager, use JtaTransactionManager for globaltx -->

<bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource" />

</bean>

<!-- 文件长传的配置文件-->

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  

        <property name="defaultEncoding" value="utf-8"></property>   

        <property name="maxUploadSize" value="10485760000000000"></property>  

        <property name="maxInMemorySize" value="40960"></property>  

        <property name="resolveLazily" value="true"></property>

   </bean> 

  

<!-- 配置事务通知属性 -->  

    <tx:advice id="txAdvice" transaction-manager="transactionManager">  

        <!-- 定义事务传播属性-->  

        <tx:attributes>  

            <tx:method name="insert*" propagation="REQUIRED" />  

            <tx:method name="update*" propagation="REQUIRED" />  

            <tx:method name="edit*" propagation="REQUIRED" />  

            <tx:method name="save*" propagation="REQUIRED" />  

            <tx:method name="add*" propagation="REQUIRED" />  

            <tx:method name="new*" propagation="REQUIRED" />  

            <tx:method name="set*" propagation="REQUIRED" />  

            <tx:method name="remove*" propagation="REQUIRED" />  

            <tx:method name="delete*" propagation="REQUIRED" />  

            <tx:method name="change*" propagation="REQUIRED" />  

            <tx:method name="check*" propagation="REQUIRED" />  

            <tx:method name="get*" propagation="REQUIRED" />  

            <tx:method name="find*" propagation="REQUIRED" />  

            <tx:method name="load*" propagation="REQUIRED" />  

            <tx:method name="*" propagation="REQUIRED"  />  

        </tx:attributes>  

    </tx:advice>  

  

    <!-- 配置事务切面  处理事务的地方来设置事务只读还是啥-->  

    <aop:config>  

        <aop:pointcut id="serviceOperation"  

            expression="execution(* com.cn.ssm.service.*.*(..))" />  

        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  

    </aop:config>  

    

   <!-- 自动扫描 service-->

<context:component-scan base-package="com.cn.ssm.service" />

 

</beans>

五、配置mybatis-config.xml

<?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>

<!-- 别名 -->

<typeAliases>

<package name="com.cn.ssm.entity"/>

</typeAliases>

</configuration>

六.配置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:p="http://www.springframework.org/schema/p"  

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:context="http://www.springframework.org/schema/context"  

    xmlns:jee="http://www.springframework.org/schema/jee"  

    xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation=        

"http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  

        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  

        http://www.springframework.org/schema/mvc   http://www.springframework.org/schema/mvc/spring-mvc.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.0.xsd  

        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

        <mvc:annotation-driven/>

 

   <mvc:resources location="/" mapping="/**/*.html"/>

  <mvc:resources location="/" mapping="/**/*.js"/>

  <mvc:resources location="/" mapping="/**/*.css"/>

  <mvc:resources location="/" mapping="/**/*.jpg"/>

  <mvc:resources location="/" mapping="/**/*.gif"/>

  <mvc:resources location="/" mapping="/**/*.gzcss"/>

  <mvc:resources location="/" mapping="/**/*.gzjs"/>

  <mvc:resources location="/" mapping="/**/*.png"/>

  <mvc:resources location="/" mapping="/**/*.eot"/>

  <mvc:resources location="/" mapping="/**/*.svg"/>

  <mvc:resources location="/" mapping="/**/*.ttf"/>

  <mvc:resources location="/" mapping="/**/*.woff"/>

  <mvc:resources location="/" mapping="/**/*.woff2"/>

<!-- 视图解析器 -->

<bean id="viewResolver"

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/views/" />

<property name="suffix" value=".jsp"></property>

</bean>

 

<bean id="multipartResolver"

        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<property name="defaultEncoding" value="UTF-8"/>  

    <property name="maxUploadSize" value="10000000"/>

 

</bean>

<!-- 扫描添加@controller的注解-->

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

</beans>

项目结构:

 

七:业务逻辑:

Controller的编写:

@Controller

public class HomeController {

/**

 * 登录的方法

 * 通过用户名查询数据库的密码来比对输入的密码

 * @return

 */

@Autowired

private CheckSelf checkSelf;

@RequestMapping(value="/login",method=RequestMethod.GET, produces ="application/json; charset=utf-8")

@ResponseBody

public String login(@RequestParam(value ="username", required =false) Stringname,

@RequestParam(value="password",required=false,defaultValue="1234567") String code){

if (checkSelf.checkSelf(name,code)) {

return"suucess";

}

return"登录失败";

}

}

Entity的使用和数据库一一对应

public class Customer {

private String customerId;

private String name;

private int age;

private String mobile;

private String password;

private String url;

public String getCustomerId() {

return customerId;

}

public void setCustomerId(StringcustomerId) {

this.customerId =customerId;

}

public String getName() {

return name;

}

public void setName(Stringname) {

this.name =name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age =age;

}

public String getMobile() {

return mobile;

}

public void setMobile(Stringmobile) {

this.mobile =mobile;

}

public String getPassword() {

return password;

}

public void setPassword(Stringpassword) {

this.password =password;

}

public String getUrl() {

return url;

}

public void setUrl(Stringurl) {

this.url =url;

}

}

Dao层的编写:

一、mapperJava

public interface  CustomerMapper {

public String findByName(Stringname);

}

二、mapper.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.cn.ssm.dao.CustomerMapper">

<resultMap type="Customer" id="CustomResult">

<id property="customerId" column="CUSTOMERID" />

<result property="name" column="NAME" />

<result property="mobile" column="MOBILE" />

<result property="age" column="AGE" />

<result property="password" column="PASSWORD" />

<result property="url" column="URL" />

<!-- <collection property="orderList" resultMap="com.zlyj.dao.OrderMapper.orderResult">

</collection> -->

</resultMap>

<select id="findByName" parameterType="String" resultType="String">

select PASSWORD from customer

<where>

AND NAME=#{name}

</where>

</select>

</mapper> 

Service的编写使用

一、接口的编写:

public interface CheckSelf {

/**

 * 判断用户名密码是否正确

 * @param name

 * @param code

 * @return

 */

public boolean checkSelf(Stringname,Stringcode);

}

二、实现的编写

@Service

public class CheckSelfImplimplements CheckSelf {

@Autowired

private CustomerMappercustomerMapper;

 

@Override

public boolean checkSelf(Stringname, Stringcode) {

// TODO Auto-generated method stub

String password = customerMapper.findByName(name);

if (code.equals(password)) {

return true;

}

return false;

}

 

}

最后项目的结构:

 

 

 

数据库自己创建就简单的一个表:

 

启动项目发布tomcat访问路径:

http://localhost:8080/ssm/login?username=zhangchao&&password=122


 

http://localhost:8080/ssm/login?username=zhangchao&&password=123


 

 

 

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