SSM框架--(Spring+SpringMVC+Mybatis整合)附源码

框架搭建步骤:

源码下载地址:

http://download.csdn.net/download/tt741472287/10110611

创建目录

  • 创建Dynamic Web Project项目,创建工程目录,导入jar包,并创建jdbc.properties,log4j.properties属性文件。
    这里写图片描述

文件结构:
这里写图片描述


com.ssm.config包

这里写图片描述
- mybatis-config.xml配置文件

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <mappers>
        <mapper resource="com/ssm/mapper/StudentMapper.xml" />
    </mappers>
</configuration>
  • spring-dao.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: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>

    <!-- 利用Spring框架来创建和管理MyBatis的SqlSessionFactory -->
    <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>


    <!-- 扫描MyBatis Dao层中接口,同时关联接口中的方法和映射文件的标签id名称 -->
    <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配置文件
<?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: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配置文件
<?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: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>

        <!-- springMVC启用注解 -->
        <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.controller包

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<Student> 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<Student> 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包

<?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.ssm.dao.StudentDao">
    <select id="getStudent" resultType="com.ssm.entity.Student">
        select * from mk_cmm_area
    </select>
</mapper>

com.ssm.service包

package com.ssm.service;

import java.util.List;


import com.ssm.entity.Student;



public interface StudentService {
    List<Student> 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<Student> 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配置文件

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



  <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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>






    <!--Tomcat Web容器上下文变量参数  -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:com/ssm/config/spring-*.xml</param-value>
    </context-param>


    <!-- Spring 刷新Introspector防止内存泄露 -->
    <listener>
      <listener-class>
        org.springframework.web.util.IntrospectorCleanupListener
      </listener-class>
    </listener> 


    <!--整合spring容器  -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

    <!--整合SpringMVC容器,配置SpringMVC的前端控制器  -->
    <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:com/ssm/config/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值