IDEA_15简构SSM
博客专区 > AK灬 的博客 > 博客详情
IDEA_15简构SSM
AK灬 发表于2年前
IDEA_15简构SSM
  • 发表于 2年前
  • 阅读 75
  • 收藏 2
  • 点赞 1
  • 评论 0

【腾讯云】买域名送云解析+SSL证书+建站!>>>   

摘要: SSM=Springmvc+Spring+Mybatis
SSM包结构

Controller

package com.ninemax.controller;

import com.ninemax.model.HomeModel;
import com.ninemax.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by AckMan on 2016/8/1.
 */
@Controller
public class HomeController {

    @Autowired
    @Qualifier("serviceImpl")
    private HomeService homeService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response){

        List<HomeModel> modelList = homeService.selectAllById(request.getParameter("id"));

        request.setAttribute("modelList",modelList);

        return "home";
    }
}

Service

package com.ninemax.service;


import com.ninemax.model.HomeModel;

import java.util.List;

/**
 * Created by AckMan on 2016/8/1.
 */

public interface HomeService {

    public List<HomeModel> selectAllById(String modelId);
}

ServiceImpl

package com.ninemax.service.impl;

import com.ninemax.dao.HomeModelMapper;
import com.ninemax.model.HomeModel;
import com.ninemax.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by AckMan on 2016/8/1.
 */
@Service("serviceImpl")
public class HomeServiceImpl implements HomeService {

    @Autowired
    private HomeModelMapper homeModelMapper;

    public HomeModelMapper getHomeModelMapper() {
        return homeModelMapper;
    }

    public void setHomeModelMapper(HomeModelMapper homeModelMapper) {
        this.homeModelMapper = homeModelMapper;
    }

    @Override
    public List<HomeModel> selectAllById(String modelId) {

        return homeModelMapper.selectAllById(Integer.valueOf(modelId));
    }
}

数据访问接口

package com.ninemax.dao;

import com.ninemax.model.HomeModel;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 数据访问接口
 *
 * Created by AckMan on 2016/8/2.
 */
@Repository
public interface HomeModelMapper {

    public List<HomeModel> selectAllById(int modelId) ;
}

数据访问接口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">
<!--这里的namespace必须和是对应的mapper.java的地址-->
<mapper namespace="com.ninemax.dao.HomeModelMapper">

    <resultMap type="HomeModel" id="modelMap">
        <result property="id" column="model_id"/>
        <result property="name" column="model_name"/>
        <result property="age" column="model_age"/>
        <result property="address" column="model_address"/>
    </resultMap>

    <select id="selectAllById" parameterType="int" resultType="list" resultMap="modelMap">
        SELECT * FROM ssm
        WHERE model_id=#{modelId}
    </select>

</mapper>

实体类model

package com.ninemax.model;

/**
 * Created by AckMan on 2016/8/1.
 */
public class HomeModel {

    public int id;

    public String name;

    public String age;

    public String address;

    @Override
    public String toString() {
        return "HomeModel{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", address='" + address + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         version="2.4">
    <display-name>Archetype Created Web Application</display-name>
    <!--Spring container-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- spring mybatis -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mybatis.xml</param-value>
    </context-param>
    <!-- char encoding -->
    <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>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- SpringMVC -->
    <servlet>
        <servlet-name>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:mvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

springmvc配置

<?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: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-3.0.xsd">

    <!--controller-->
    <context:component-scan base-package="com.ninemax.controller"/>

    <!--annotation-->
    <mvc:annotation-driven/>

    <!-- static source -->
    <mvc:resources mapping="/**" location="/static"/>

    <!-- view analyzer -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- file upload analyzer -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="1000000000"/>
    </bean>

</beans>

mybatis配置

<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!-- Spring bean-->
    <context:component-scan base-package="com.ninemax">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!-- data source -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="url" value="${jdbc.url}"/>
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="initialSize" value="${jdbc.initialSize}"/>
        <property name="minIdle" value="${jdbc.minIdle}"/>
        <property name="maxIdle" value="${jdbc.maxIdle}"/>
        <property name="maxActive" value="${jdbc.maxActive}"/>
        <property name="maxWait" value="${jdbc.maxWait}"/>
    </bean>
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- MyBatis SessionFactory -->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.ninemax.model"/>
    </bean>
    <!-- MyBatis auto scannner Mapper -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ninemax.dao"/>
    </bean>
</beans>

数据源配置

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ackman
jdbc.username=root
jdbc.password=root
jdbc.initialSize=0
jdbc.maxActive=20
jdbc.maxIdle=20
jdbc.minIdle=1
jdbc.maxWait=60000

pom.xml配置:

<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.ninemax</groupId>
  <artifactId>ak</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>ak 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>
    <!-- springmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</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-jdbc</artifactId>
      <version>3.0.5.RELEASE</version>
    </dependency>
    <!-- 上传文件 -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.2</version>
    </dependency>
    <!-- servlet -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <!-- mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.2.1</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.2.0</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.26</version>
    </dependency>
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
    <dependency>
      <groupId>commons-pool</groupId>
      <artifactId>commons-pool</artifactId>
      <version>1.6</version>
    </dependency>
    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.2.1</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>ak</finalName>
    <!-- 防止加载不到XML -->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
      </resource>
    </resources>
    <plugins>
      <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>

访问页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <c:forEach items="${modelList}" var="model">
        <table border='1'cellspacing="0" cellpadding="0" align="center">
            <tr>
                <th>ID</th>
                <th>NAME</th>
                <th>AGE</th>
                <th>ADDRESS</th>
           </tr>
            <tr>
                <td>${model.id}</td>
                <td>${model.name}</td>
                <td>${model.age}</td>
                <td>${model.address}</td>
           </tr>
        </table>
    </c:forEach>
</body>
</html>

数据库创建

DROP TABLE IF EXISTS `ssm`;
CREATE TABLE `ssm` (
  `model_id` int(50) NOT NULL AUTO_INCREMENT,
  `model_name` varchar(100) NOT NULL,
  `model_age` varchar(10) DEFAULT NULL,
  `model_address` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`model_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of ssm
-- ----------------------------
INSERT INTO `ssm` VALUES ('1', 'darker', '20', 'HangKong');
INSERT INTO `ssm` VALUES ('2', 'ak', '25', 'ShangHai');
INSERT INTO `ssm` VALUES ('3', 'korol', '30', 'GuangZhou');
INSERT INTO `ssm` VALUES ('4', 'troll', '22', 'WuHan');

效果图

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
AK灬
粉丝 46
博文 322
码字总数 135591
×
AK灬
谢谢观众老爷们的赏赐,小弟我会再接再厉的!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: