SSMフレームワーク--(Spring+SpringMVC+Mybatis統合)付ソースコード


フレーム構築手順:
ソースのダウンロードアドレス:
http://download.csdn.net/download/tt741472287/10110611

ディレクトリの作成
  • Dynamic Web Projectプロジェクトの作成、プロジェクトディレクトリの作成、jarパッケージのインポート、jdbcの作成properties,log4j.propertiesプロパティファイル.SSM框架--(Spring+SpringMVC+Mybatis整合)附源码_第1张图片

  • ファイル構造:SSM框架--(Spring+SpringMVC+Mybatis整合)附源码_第2张图片
    com.ssm.configパッケージ
    SSM框架--(Spring+SpringMVC+Mybatis整合)附源码_第3张图片 - mybatis-config.xmlプロファイル
    
    <configuration>
        <mappers>
            <mapper resource="com/ssm/mapper/StudentMapper.xml" />
        mappers>
    configuration>
  • spring-dao.xmlプロファイル
  • 
    <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:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        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.xsd">
    
        <context:component-scan base-package="com.ssm.dao">context:component-scan>
    
    
    
        <context:property-placeholder location="classpath:jdbc.properties" />
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="${mysql.driver}">property>
            <property name="url" value="${mysql.url}">property>
            <property name="username" value="${mysql.username}">property>
            <property name="password" value="${mysql.password}">property>
        bean>
    
        
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource">property>
            <property name="configLocation" value="classpath:com/ssm/config/mybatis-config.xml">property>
        bean>
    
    
        
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.ssm.dao">property>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
        bean>
    
    beans>
  • spring-service.xmlプロファイル
  • 
    <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:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        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.xsd">
    
    
          <context:component-scan base-package="com.ssm.service">context:component-scan>        
    
    beans>
  • springmvc.xmlプロファイル
  • 
    <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:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        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.xsd">
    
    
            <context:component-scan base-package="com.ssm">context:component-scan>
    
            
            <mvc:annotation-driven>mvc:annotation-driven>
    
            
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
            p:prefix="/WEB-INF/jsp/" p:suffix=".jsp">bean>
    
    beans>

    com.ssm.コントロールパッケージ
    package com.ssm.controller;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.ssm.entity.Student;
    import com.ssm.service.StudentService;
    import com.ssm.service.impl.StudentServiceImpl;
    
    @Controller
    @RequestMapping("/authuser")
    public class AuthUserController {
    
        @Autowired
        private StudentService studentService;
    
        @RequestMapping("/login")
        public String login(){
            System.out.println(studentService.hashCode());
            List list=studentService.getStudent();
            for(Student student:list){
                System.out.println(student.toString());
            }
            return "login";
        }
    }
    

    com.ssm.daoバッグ
    package com.ssm.dao;
    
    import java.util.List;
    
    
    import com.ssm.entity.Student;
    
    
    
    
    public interface StudentDao {
    
        public List getStudent();
    
    
    }

    com.ssm.entityパッケージ
    package com.ssm.entity;
    
    /**
     *    
     * @author caleb
     *
     */
    public class Student {
        /**
         * This field was generated by MyBatis Generator.
         * This field corresponds to the database column mk_cmm_area.id
         *
         * @mbggenerated Mon Oct 30 17:51:08 CST 2017
         */
        private Long id;
    
        /**
         * This field was generated by MyBatis Generator.
         * This field corresponds to the database column mk_cmm_area.parent_id
         *
         * @mbggenerated Mon Oct 30 17:51:08 CST 2017
         */
        private Long parentId;
    
        /**
         * This field was generated by MyBatis Generator.
         * This field corresponds to the database column mk_cmm_area.depth
         *
         * @mbggenerated Mon Oct 30 17:51:08 CST 2017
         */
        private Integer depth;
    
        /**
         * This field was generated by MyBatis Generator.
         * This field corresponds to the database column mk_cmm_area.name
         *
         * @mbggenerated Mon Oct 30 17:51:08 CST 2017
         */
        private String name;
    
        /**
         * This field was generated by MyBatis Generator.
         * This field corresponds to the database column mk_cmm_area.full_name
         *
         * @mbggenerated Mon Oct 30 17:51:08 CST 2017
         */
        private String fullName;
    
        /**
         * This field was generated by MyBatis Generator.
         * This field corresponds to the database column mk_cmm_area.longitude
         *
         * @mbggenerated Mon Oct 30 17:51:08 CST 2017
         */
        private Double longitude;
    
        /**
         * This field was generated by MyBatis Generator.
         * This field corresponds to the database column mk_cmm_area.latitude
         *
         * @mbggenerated Mon Oct 30 17:51:08 CST 2017
         */
        private Double latitude;
    
        /**
         * This field was generated by MyBatis Generator.
         * This field corresponds to the database column mk_cmm_area.rank
         *
         * @mbggenerated Mon Oct 30 17:51:08 CST 2017
         */
        private Integer rank;
    
        /**
         * This field was generated by MyBatis Generator.
         * This field corresponds to the database column mk_cmm_area.flag_enable
         *
         * @mbggenerated Mon Oct 30 17:51:08 CST 2017
         */
        private Integer flagEnable;
    
        /**
         * This field was generated by MyBatis Generator.
         * This field corresponds to the database column mk_cmm_area.flag_visible
         *
         * @mbggenerated Mon Oct 30 17:51:08 CST 2017
         */
        private Integer flagVisible;
    
        /**
         * This field was generated by MyBatis Generator.
         * This field corresponds to the database column mk_cmm_area.initals
         *
         * @mbggenerated Mon Oct 30 17:51:08 CST 2017
         */
        private String initals;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public Long getParentId() {
            return parentId;
        }
    
        public void setParentId(Long parentId) {
            this.parentId = parentId;
        }
    
        public Integer getDepth() {
            return depth;
        }
    
        public void setDepth(Integer depth) {
            this.depth = depth;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getFullName() {
            return fullName;
        }
    
        public void setFullName(String fullName) {
            this.fullName = fullName;
        }
    
        public Double getLongitude() {
            return longitude;
        }
    
        public void setLongitude(Double longitude) {
            this.longitude = longitude;
        }
    
        public Double getLatitude() {
            return latitude;
        }
    
        public void setLatitude(Double latitude) {
            this.latitude = latitude;
        }
    
        public Integer getRank() {
            return rank;
        }
    
        public void setRank(Integer rank) {
            this.rank = rank;
        }
    
        public Integer getFlagEnable() {
            return flagEnable;
        }
    
        public void setFlagEnable(Integer flagEnable) {
            this.flagEnable = flagEnable;
        }
    
        public Integer getFlagVisible() {
            return flagVisible;
        }
    
        public void setFlagVisible(Integer flagVisible) {
            this.flagVisible = flagVisible;
        }
    
        public String getInitals() {
            return initals;
        }
    
        public void setInitals(String initals) {
            this.initals = initals;
        }
    
        @Override
        public String toString() {
            return "Student [id=" + id + ", parentId=" + parentId + ", depth=" + depth + ", name=" + name + ", fullName="
                    + fullName + ", longitude=" + longitude + ", latitude=" + latitude + ", rank=" + rank + ", flagEnable="
                    + flagEnable + ", flagVisible=" + flagVisible + ", initals=" + initals + "]";
        }
    
    
    
    }
    

    com.ssm.mapperパッケージ
    
    
    <mapper namespace="com.ssm.dao.StudentDao">
        <select id="getStudent" resultType="com.ssm.entity.Student">
            select * from mk_cmm_area
        select>
    mapper>

    com.ssm.サービスパッケージ
    package com.ssm.service;
    
    import java.util.List;
    
    
    import com.ssm.entity.Student;
    
    
    
    public interface StudentService {
        List getStudent();
    }
    

    com.ssm.service.implパッケージ
    package com.ssm.service.impl;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.ssm.dao.StudentDao;
    import com.ssm.entity.Student;
    import com.ssm.service.StudentService;
    
    
    @Service
    public class StudentServiceImpl implements StudentService{
        @Autowired
        private StudentDao studentDao;
    
        public StudentServiceImpl() {
    
            System.out.println("StudentServiceImpl====================construction");
    
        }
    
        @Override
        public List getStudent() {
            // TODO Auto-generated method stub
            return studentDao.getStudent();
    //      return null;
        }
    
    }
    

    jdbc.propertiesファイル
    mysql.driver=com.mysql.jdbc.Driver
    mysql.url=jdbc:mysql://localhost:3306/test
    mysql.username=root
    mysql.password=root

    log4j.propertiesファイル
    log4j.rootLogger=info, Console
    #Console
    log4j.appender.Console=org.apache.log4j.ConsoleAppender
    log4j.appender.Console.layout=org.apache.log4j.PatternLayout
    log4j.appender.Console.layout.ConversionPattern=%d  %p  - %m%n
    log4j.logger.java.sql.ResultSet=INFO
    log4j.logger.org.apache=INFO
    log4j.logger.java.sql.Connection=DEBUG
    log4j.logger.java.sql.Statement=DEBUG
    log4j.logger.java.sql.PreparedStatement=DEBUG
    

    web.xmlプロファイル
    
    <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>project0_ssmdisplay-name>
      <welcome-file-list>
        <welcome-file>index.htmlwelcome-file>
        <welcome-file>index.htmwelcome-file>
        <welcome-file>index.jspwelcome-file>
        <welcome-file>default.htmlwelcome-file>
        <welcome-file>default.htmwelcome-file>
        <welcome-file>default.jspwelcome-file>
      welcome-file-list>
    
    
    
      <filter>
            <filter-name>CharacterEncodingFilterfilter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
            <init-param>
                <param-name>encodingparam-name>
                <param-value>UTF-8param-value>
            init-param>
            <init-param>
                <param-name>forceEncodingparam-name>
                <param-value>trueparam-value>
            init-param>
        filter>
        <filter-mapping>
            <filter-name>CharacterEncodingFilterfilter-name>
            <url-pattern>/*url-pattern>
        filter-mapping>
    
    
    
    
    
    
        
        <context-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:com/ssm/config/spring-*.xmlparam-value>
        context-param>
    
    
        
        <listener>
          <listener-class>
            org.springframework.web.util.IntrospectorCleanupListener
          listener-class>
        listener> 
    
    
        
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    
        listener>
    
        
        <servlet>
            <servlet-name>springmvcservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            <init-param>
                <param-name>contextConfigLocationparam-name>
                <param-value>classpath:com/ssm/config/springmvc.xmlparam-value>
            init-param>
            <load-on-startup>1load-on-startup>
        servlet>
    
        <servlet-mapping>
            <servlet-name>springmvcservlet-name>
            <url-pattern>*.dourl-pattern>
        servlet-mapping>
    
    
    web-app>

    データベース・スクリプト
    /*
    Navicat MySQL Data Transfer
    
    Source Server         : localhost_3306
    Source Server Version : 50546
    Source Host           : localhost:3306
    Source Database       : test
    
    Target Server Type    : MYSQL
    Target Server Version : 50546
    File Encoding         : 65001
    
    Date: 2017-11-08 23:00:27
    */
    
    SET FOREIGN_KEY_CHECKS=0;
    
    -- ----------------------------
    -- Table structure for mk_cmm_area
    -- ----------------------------
    DROP TABLE IF EXISTS `mk_cmm_area`;
    CREATE TABLE `mk_cmm_area` (
      `id` bigint(20) NOT NULL,
      `parent_id` bigint(20) DEFAULT NULL,
      `depth` int(11) DEFAULT NULL COMMENT '  ',
      `name` varchar(32) DEFAULT NULL,
      `full_name` varchar(64) DEFAULT NULL,
      `longitude` double(12,8) DEFAULT NULL,
      `latitude` double(12,8) DEFAULT NULL,
      `rank` int(11) DEFAULT '0' COMMENT '    (      )',
      `flag_enable` int(11) DEFAULT '1' COMMENT '    ,0 ,1 ',
      `flag_visible` int(11) DEFAULT '1' COMMENT '    ,0 ,1 ',
      `initals` char(1) DEFAULT NULL COMMENT '   ',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    -- ----------------------------
    -- Records of mk_cmm_area
    -- ----------------------------
    INSERT INTO `mk_cmm_area` VALUES ('110105', '110100', '2', '  ', '   ', '116.48641000', '39.92149000', '2', '1', '1', null);
    INSERT INTO `mk_cmm_area` VALUES ('110106', '110100', '2', '  ', '   ', '116.28696400', '39.86364400', '6', '1', '1', null);