IDEA进行SSM框架整合(附源码)

IDEA进行SSM框架整合

    • 一、创建Maven项目
    • 二、配置pom文件
    • 三、创建相关的包结构
    • 四、 web.xml配置ServletDispathcer拦截
    • 五、Spring相关配置文件配置
      • spring-dao.xml
      • spring-service配置
      • spring-web
    • 六、数据库配置文件
    • 七、mybatis-config配置
    • 八、测试
      • 数据库测试
      • Service和Controller测试

一、创建Maven项目

IDEA进行SSM框架整合(附源码)_第1张图片
IDEA进行SSM框架整合(附源码)_第2张图片

二、配置pom文件

其中关于引入的各个jar包的作用,大部分都添加了注释,个别的自己项目用不到的内容,可自行取舍。

<dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.12version>
      <scope>testscope>
    dependency>

    
    
    <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-classicartifactId>
      <version>1.2.3version>
    dependency>

    
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-coreartifactId>
      <version>${spring.version}version>
    dependency>

    
    <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-jdbcartifactId>
      <version>${spring.version}version>
    dependency>

    
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-txartifactId>
      <version>${spring.version}version>
    dependency>

    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>${spring.version}version>
    dependency>

    
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>${spring.version}version>
    dependency>

    
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <version>${spring.version}version>
      <scope>testscope>
    dependency>

    
    
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.1.0version>
    dependency>
    
    
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
      <version>2.8.7version>
    dependency>

    
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>3.4.2version>
    dependency>
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>1.3.1version>
    dependency>
    
    
    
      
      
      
    
    
    <dependency>
      <groupId>redis.clientsgroupId>
      <artifactId>jedisartifactId>
      <version>2.9.0version>
    dependency>
    <dependency>
      <groupId>com.dyuproject.protostuffgroupId>
      <artifactId>protostuff-coreartifactId>
      <version>1.0.12version>
    dependency>
    <dependency>
      <groupId>com.dyuproject.protostuffgroupId>
      <artifactId>protostuff-runtimeartifactId>
      <version>1.0.12version>
    dependency>
    
    
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>8.0.15version>
    dependency>
    <dependency>
      <groupId>c3p0groupId>
      <artifactId>c3p0artifactId>
      <version>0.9.1.2version>
    dependency>

    
    
    
    <dependency>
      <groupId>commons-collectionsgroupId>
      <artifactId>commons-collectionsartifactId>
      <version>3.2version>
    dependency>
    
    <dependency>
      <groupId>commons-fileuploadgroupId>
      <artifactId>commons-fileuploadartifactId>
      <version>1.3.2version>
    dependency>
    <dependency>
      <groupId>jstlgroupId>
      <artifactId>jstlartifactId>
      <version>1.2version>
    dependency>
    <dependency>
      <groupId>taglibsgroupId>
      <artifactId>standardartifactId>
      <version>1.1.2version>
    dependency>
  dependencies>

三、创建相关的包结构

这里因为博主为了截图,创建了不止一次项目,所以不必过分纠结于项目名。
IDEA进行SSM框架整合(附源码)_第3张图片

四、 web.xml配置ServletDispathcer拦截

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1" metadata-complete="true">
  <display-name>Archetype Created Web Applicationdisplay-name>
  <servlet>
    <servlet-name>spring-dispatcherservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:spring/spring-*.xmlparam-value>
    init-param>
  servlet>
  <servlet-mapping>
    <servlet-name>spring-dispatcherservlet-name>
    
    <url-pattern>/url-pattern>
  servlet-mapping>
web-app>

五、Spring相关配置文件配置

spring-dao.xml

spring整合mybatis


<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.xsd">

    
    
    <context:property-placeholder location="classpath:jdbc.properties"/>

    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        
        <property name="autoCommitOnClose" value="false"/>
        
        <property name="checkoutTimeout" value="10000"/>
        
        <property name="acquireRetryAttempts" value="2"/>
    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource"/>
        
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        
        <property name="typeAliasesPackage" value="com.qcby.entity"/>
        
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        
        <property name="basePackage" value="com.qcby.dao"/>
    bean>
beans>

spring-service配置


<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">
    
    <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:tx="http://www.springframework.org/schema/tx"
           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.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">
        
        <context:component-scan base-package="com.qcby.service" />

        
        <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            
            <property name="dataSource" ref="dataSource"/>
        bean>
        
        <tx:annotation-driven transaction-manager="transactionManager" />
    beans>
beans>

spring-web

控制层相关内容配置


<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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd">

    
    
    
    

    
    
    
    <mvc:annotation-driven/>

    
    <mvc:resources mapping="/resources/**" location="/resources/"/>
    <mvc:default-servlet-handler/>

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/html/"/>
        <property name="suffix" value=".jsp"/>
    bean>

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

六、数据库配置文件

这里配置的是新版的mysql8.0的连接url,设置不使用ssl连接

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/your_database_name?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
jdbc.username=root
jdbc.password=root

七、mybatis-config配置



<configuration>
    
    <settings>
        
        <setting name="useGeneratedKeys" value="true" />

        
        <setting name="useColumnLabel" value="true" />

        
        <setting name="mapUnderscoreToCamelCase" value="true" />
        
        <setting name="logImpl" value="STDOUT_LOGGING" />
    settings>
configuration>

八、测试

数据库测试

IDEA进行SSM框架整合(附源码)_第4张图片

import org.springframework.stereotype.Repository;

import java.util.Date;

@Repository
public class Area {
    private Integer area_id;

    private String area_name;

    private String area_desc;

    private Integer priority;

    private Date create_time;

    private Date last_edit_time;

    public Integer getArea_id() {
        return area_id;
    }

    public void setArea_id(Integer area_id) {
        this.area_id = area_id;
    }

    public String getArea_name() {
        return area_name;
    }

    public void setArea_name(String area_name) {
        this.area_name = area_name == null ? null : area_name.trim();
    }

    public String getArea_desc() {
        return area_desc;
    }

    public void setArea_desc(String area_desc) {
        this.area_desc = area_desc == null ? null : area_desc.trim();
    }

    public Integer getPriority() {
        return priority;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    public Date getCreate_time() {
        return create_time;
    }

    public void setCreate_time(Date create_time) {
        this.create_time = create_time;
    }

    public Date getLast_edit_time() {
        return last_edit_time;
    }

    public void setLast_edit_time(Date last_edit_time) {
        this.last_edit_time = last_edit_time;
    }

    @Override
    public String toString() {
        return "Area{" +
                "area_id=" + area_id +
                ", area_name='" + area_name + '\'' +
                ", area_desc='" + area_desc + '\'' +
                ", priority=" + priority +
                ", create_time=" + create_time +
                ", last_edit_time=" + last_edit_time +
                '}';
    }
}

import com.qcby.entity.Area;

import java.util.List;

public interface AreaDao {

    List<Area> queryArea();
}
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 *配置spring和junit整合,junit启动时加载springIoc容器
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/spring-dao.xml"})
public class BaseTest {
}
import com.qbcy.BaseTest;
import com.qcby.entity.Area;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

import static junit.framework.TestCase.assertEquals;

public class AreaDaoTest extends BaseTest {

    @Autowired
    private com.qcby.dao.AreaDao areaDao;

    @Test
    public void testQueryArea(){
        List<Area> areaList = areaDao.queryArea();
        System.out.println(areaList);
        Area area = new Area();
        area.setArea_name("zhongguo");
        area.setPriority(3);
        area.setCreate_time(new Date());
        area.setLast_edit_time(new Date());
        areaDao.addArea(area);
        assertEquals(4,areaList.size());
    }

}

出现下面图片的数据库内容证明测试通过:
IDEA进行SSM框架整合(附源码)_第5张图片

Service和Controller测试

import com.qcby.service.Impl.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("test")
public class TestController {

    @Autowired
    TestService testService;

    @RequestMapping("testcon")
    public String testCon(Model model, HttpServletRequest request){
        request.setAttribute("test","testcontent");
        testService.testService();
        return "test";
    }
}
@Service("testService")
public class TestService {
    public void testService(){
        System.out.println("TestService");
    }
}

最后出现我们设置的testcontent就万事大吉啦!
IDEA进行SSM框架整合(附源码)_第6张图片

完整的项目我打包发布了出来,不差分的可以下载来看看。直达链接

你可能感兴趣的:(JavaWeb)