用idea搭建简单ssm框架

使用的IDEA作为IDE


一 创建 Maven Project

记得是 Maven Project



groupId : testSSM

artifactId: testSSM




二 配置Pom.xml


[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <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">  
  4.   
  5.   <modelVersion>4.0.0modelVersion>  
  6.   <packaging>warpackaging>  
  7.   
  8.   <name>testSSMname>  
  9.   <groupId>testSSMgroupId>  
  10.   <artifactId>testSSMartifactId>  
  11.   <version>1.0-SNAPSHOTversion>  
  12.   
  13.   <build>  
  14.     <plugins>  
  15.       <plugin>  
  16.         <groupId>org.mortbay.jettygroupId>  
  17.         <artifactId>maven-jetty-pluginartifactId>  
  18.         <version>6.1.7version>  
  19.         <configuration>  
  20.           <connectors>  
  21.             <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">  
  22.               <port>8888port>  
  23.               <maxIdleTime>30000maxIdleTime>  
  24.             connector>  
  25.           connectors>  
  26.           <webAppSourceDirectory>${project.build.directory}/${pom.artifactId}-${pom.version}webAppSourceDirectory>  
  27.           <contextPath>/contextPath>  
  28.         configuration>  
  29.       plugin>  
  30.     plugins>  
  31.   build>  
  32.   
  33.   <properties>  
  34.     <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>  
  35.     <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>  
  36.   
  37.       
  38.     <spring.version>4.2.5.RELEASEspring.version>  
  39.   
  40.       
  41.     <mybatis.version>3.2.8mybatis.version>  
  42.   
  43.       
  44.     <mysql-driver.version>5.1.29mysql-driver.version>  
  45.   
  46.       
  47.     <slf4j.version>1.7.18slf4j.version>  
  48.     <log4j.version>1.2.17log4j.version>  
  49.   
  50.   properties>  
  51.   
  52.   
  53.   <dependencies>  
  54.       
  55.     <dependency>  
  56.       <groupId>jstlgroupId>  
  57.       <artifactId>jstlartifactId>  
  58.       <version>1.2version>  
  59.     dependency>  
  60.   
  61.     <dependency>  
  62.       <groupId>javaxgroupId>  
  63.       <artifactId>javaee-apiartifactId>  
  64.       <version>7.0version>  
  65.     dependency>  
  66.     <dependency>  
  67.       <groupId>com.alibabagroupId>  
  68.       <artifactId>druidartifactId>  
  69.       <version>1.0.16version>  
  70.     dependency>  
  71.       
  72.     <dependency>  
  73.       <groupId>junitgroupId>  
  74.       <artifactId>junitartifactId>  
  75.       <version>4.11version>  
  76.         
  77.       <scope>testscope>  
  78.     dependency>  
  79.   
  80.       
  81.     <dependency>  
  82.       <groupId>org.springframeworkgroupId>  
  83.       <artifactId>spring-coreartifactId>  
  84.       <version>${spring.version}version>  
  85.     dependency>  
  86.     <dependency>  
  87.       <groupId>org.springframeworkgroupId>  
  88.       <artifactId>spring-webartifactId>  
  89.       <version>${spring.version}version>  
  90.     dependency>  
  91.     <dependency>  
  92.       <groupId>org.springframeworkgroupId>  
  93.       <artifactId>spring-oxmartifactId>  
  94.       <version>${spring.version}version>  
  95.     dependency>  
  96.     <dependency>  
  97.       <groupId>org.springframeworkgroupId>  
  98.       <artifactId>spring-txartifactId>  
  99.       <version>${spring.version}version>  
  100.     dependency>  
  101.     <dependency>  
  102.       <groupId>org.springframeworkgroupId>  
  103.       <artifactId>spring-jdbcartifactId>  
  104.       <version>${spring.version}version>  
  105.     dependency>  
  106.     <dependency>  
  107.       <groupId>org.springframeworkgroupId>  
  108.       <artifactId>spring-webmvcartifactId>  
  109.       <version>${spring.version}version>  
  110.     dependency>  
  111.     <dependency>  
  112.       <groupId>org.springframeworkgroupId>  
  113.       <artifactId>spring-contextartifactId>  
  114.       <version>${spring.version}version>  
  115.     dependency>  
  116.     <dependency>  
  117.       <groupId>org.springframeworkgroupId>  
  118.       <artifactId>spring-context-supportartifactId>  
  119.       <version>${spring.version}version>  
  120.     dependency>  
  121.     <dependency>  
  122.       <groupId>org.springframeworkgroupId>  
  123.       <artifactId>spring-aopartifactId>  
  124.       <version>${spring.version}version>  
  125.     dependency>  
  126.   
  127.     <dependency>  
  128.       <groupId>org.springframeworkgroupId>  
  129.       <artifactId>spring-testartifactId>  
  130.       <version>${spring.version}version>  
  131.     dependency>  
  132.   
  133.       
  134.     <dependency>  
  135.       <groupId>org.mybatisgroupId>  
  136.       <artifactId>mybatisartifactId>  
  137.       <version>${mybatis.version}version>  
  138.     dependency>  
  139.   
  140.       
  141.     <dependency>  
  142.       <groupId>org.mybatisgroupId>  
  143.       <artifactId>mybatis-springartifactId>  
  144.       <version>1.2.2version>  
  145.     dependency>  
  146.   
  147.       
  148.     <dependency>  
  149.       <groupId>mysqlgroupId>  
  150.       <artifactId>mysql-connector-javaartifactId>  
  151.       <version>${mysql-driver.version}version>  
  152.     dependency>  
  153.       
  154.     <dependency>  
  155.       <groupId>commons-dbcpgroupId>  
  156.       <artifactId>commons-dbcpartifactId>  
  157.       <version>1.2.2version>  
  158.     dependency>  
  159.   
  160.       
  161.     <dependency>  
  162.       <groupId>com.alibabagroupId>  
  163.       <artifactId>fastjsonartifactId>  
  164.       <version>1.1.41version>  
  165.     dependency>  
  166.   
  167.       
  168.     <dependency>  
  169.       <groupId>log4jgroupId>  
  170.       <artifactId>log4jartifactId>  
  171.       <version>${log4j.version}version>  
  172.     dependency>  
  173.     <dependency>  
  174.       <groupId>org.slf4jgroupId>  
  175.       <artifactId>slf4j-apiartifactId>  
  176.       <version>${slf4j.version}version>  
  177.     dependency>  
  178.     <dependency>  
  179.       <groupId>org.slf4jgroupId>  
  180.       <artifactId>slf4j-log4j12artifactId>  
  181.       <version>${slf4j.version}version>  
  182.     dependency>  
  183.   
  184.       
  185.       
  186.     <dependency>  
  187.       <groupId>org.codehaus.jacksongroupId>  
  188.       <artifactId>jackson-mapper-aslartifactId>  
  189.       <version>1.9.13version>  
  190.     dependency>  
  191.       
  192.     <dependency>  
  193.       <groupId>com.fasterxml.jackson.coregroupId>  
  194.       <artifactId>jackson-coreartifactId>  
  195.       <version>2.8.0version>  
  196.     dependency>  
  197.       
  198.     <dependency>  
  199.       <groupId>com.fasterxml.jackson.coregroupId>  
  200.       <artifactId>jackson-databindartifactId>  
  201.       <version>2.8.0version>  
  202.     dependency>  
  203.   
  204.     <dependency>  
  205.       <groupId>commons-fileuploadgroupId>  
  206.       <artifactId>commons-fileuploadartifactId>  
  207.       <version>1.3.1version>  
  208.     dependency>  
  209.   
  210.     <dependency>  
  211.       <groupId>commons-iogroupId>  
  212.       <artifactId>commons-ioartifactId>  
  213.       <version>2.4version>  
  214.     dependency>  
  215.   
  216.     <dependency>  
  217.       <groupId>commons-codecgroupId>  
  218.       <artifactId>commons-codecartifactId>  
  219.       <version>1.9version>  
  220.     dependency>  
  221.   dependencies>  
  222.   
  223. project>  


三  创建数据库

创建表
[sql]  view plain  copy
  1. CREATE TABLE `tb_user` (  
  2.   `UserID` int(11) NOT NULL AUTO_INCREMENT,  
  3.   `UserName` varchar(255) DEFAULT NULL,  
  4.   `RealName` varchar(255) DEFAULT NULL,  
  5.   `Passwordvarchar(255) DEFAULT NULL,  
  6.   `IsLock` int(11) DEFAULT NULL,  
  7.   `CreateTime` datetime DEFAULT CURRENT_TIMESTAMP,  
  8.   PRIMARY KEY (`UserID`),  
  9.   UNIQUE KEY `id` (`UserName`)  
  10. ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;  


新增数据



四 搭建SSM分层结构





注意,创建完上面5个包后,需要进行如下操作


选择java,然后点击Sources,java文件夹会变蓝。

同样,选择resources,然后点击 Resources, resources会出现黄色横杠。



五 添加xml配置文件

先看一下配置文件


1 web.xml

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  5.           http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  6.          version="3.0">  
  7.   
  8.   <display-name>testSSMdisplay-name>  
  9.   
  10.   <context-param>  
  11.     <param-name>contextConfigLocationparam-name>  
  12.     <param-value>classpath:spring-mybatis.xmlparam-value>  
  13.   context-param>  
  14.   
  15.   <context-param>  
  16.     <param-name>log4jConfigLocationparam-name>  
  17.     <param-value>classpath:log4j.propertiesparam-value>  
  18.   context-param>  
  19.   
  20.     
  21.   <filter>  
  22.     <filter-name>encodingFilterfilter-name>  
  23.     <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>  
  24.     <init-param>  
  25.       <param-name>encodingparam-name>  
  26.       <param-value>UTF-8param-value>  
  27.     init-param>  
  28.   filter>  
  29.   <filter-mapping>  
  30.     <filter-name>encodingFilterfilter-name>  
  31.     <url-pattern>/*url-pattern>  
  32.   filter-mapping>  
  33.   
  34.     
  35.   <listener>  
  36.     <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>  
  37.   listener>  
  38.   
  39.     
  40.   <listener>  
  41.     <listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>  
  42.   listener>  
  43.   
  44.   
  45.     
  46.   <servlet>  
  47.     <servlet-name>testSSMservlet-name>  
  48.     <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>  
  49.     <init-param>  
  50.       <param-name>contextConfigLocationparam-name>  
  51.       <param-value>classpath:spring-mvc.xmlparam-value>  
  52.     init-param>  
  53.     <load-on-startup>1load-on-startup>  
  54.     <async-supported>trueasync-supported>  
  55.   servlet>  
  56.   <servlet-mapping>  
  57.     <servlet-name>testSSMservlet-name>  
  58.       
  59.     <url-pattern>/url-pattern>  
  60.   servlet-mapping>  
  61.   
  62.   <welcome-file-list>  
  63.     <welcome-file>/index.jspwelcome-file>  
  64.   welcome-file-list>  
  65.   
  66.     
  67.   <session-config>  
  68.     <session-timeout>15session-timeout>  
  69.   session-config>  
  70.   
  71. web-app>  


2 jdbc.properties

[html]  view plain  copy
  1. #driverClasss=com.mysql.jdbc.Driver  
  2. jdbcUrl=jdbc:mysql://xxx.xxx.xxx.xxx:3306/testdb  
  3. #?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&autoReconnectForPools=true&noAccessToProcedureBodies=true  
  4. username=root  
  5. password=root  
  6.   
  7. #定义初始连接数  
  8. initialSize=0    
  9. #定义最大连接数  
  10. maxActive=20    
  11. #定义最大空闲  
  12. maxIdle=20    
  13. #定义最小空闲  
  14. minIdle=1    
  15. #定义最长等待时间  
  16. maxWait=60000    


3 log4j.properties

[html]  view plain  copy
  1. log4j.rootLogger=INFO,Console,File    
  2.   
  3. #控制台日志  
  4. log4j.appender.Console=org.apache.log4j.ConsoleAppender    
  5. log4j.appender.Console.Target=System.out    
  6. log4j.appender.Console.layout=org.apache.log4j.PatternLayout    
  7. log4j.appender.Console.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n    
  8.   
  9. #普通文件日志  
  10. log4j.appender.File=org.apache.log4j.RollingFileAppender    
  11. log4j.appender.File.File=logs/ssm.log    
  12. log4j.appender.File.MaxFileSize=10MB    
  13. #输出日志,如果换成DEBUG表示输出DEBUG以上级别日志  
  14. log4j.appender.File.Threshold=ALL    
  15. log4j.appender.File.layout=org.apache.log4j.PatternLayout    
  16. log4j.appender.File.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n    


4 mybatisConfig.xml

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. >  
  3. <configuration>  
  4.     <settings>  
  5.           
  6.           
  7.     settings>  
  8.   
  9. configuration>  


5 spring-mvc.xml

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.                         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  8.                         http://www.springframework.org/schema/context  
  9.                         http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  10.                         http://www.springframework.org/schema/mvc  
  11.                         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
  12.   
  13.       
  14.     <context:component-scan base-package="controller"/>  
  15.   
  16.       
  17.     <bean id="mappingJacksonHttpMessageConverter"  
  18.           class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
  19.         <property name="supportedMediaTypes">  
  20.             <list>  
  21.                 <value>text/html;charset=UTF-8value>  
  22.             list>  
  23.         property>  
  24.     bean>  
  25.       
  26.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  27.         <property name="messageConverters">  
  28.             <list>  
  29.                 <ref bean="mappingJacksonHttpMessageConverter"/>   
  30.             list>  
  31.         property>  
  32.     bean>  
  33.   
  34.   
  35.       
  36.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  37.         <property name="prefix" value="/WEB-INF/" />  
  38.         <property name="suffix" value=".jsp"/>  
  39.     bean>  
  40.   
  41.       
  42.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  43.           
  44.         <property name="defaultEncoding" value="UTF-8"/>  
  45.           
  46.         <property name="maxUploadSize" value="32505856"/>  
  47.           
  48.         <property name="maxInMemorySize" value="4096"/>  
  49.     bean>  
  50. beans>  


6 spring-mybatis.xml

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  7.                         http://www.springframework.org/schema/context  
  8.                         http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  9.                         http://www.springframework.org/schema/tx  
  10.                         http://www.springframework.org/schema/tx/spring-tx.xsd">  
  11.   
  12.       
  13.     <context:component-scan base-package="controller"/>  
  14.   
  15.       
  16.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  17.         <property name="location" value="classpath:jdbc.properties"/>  
  18.     bean>  
  19.   
  20.   
  21.       
  22.     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"  
  23.           destroy-method="close">  
  24.           
  25.         <property name="url" value="${jdbcUrl}"/>  
  26.         <property name="username" value="${username}"/>  
  27.         <property name="password" value="${password}"/>  
  28.           
  29.         <property name="initialSize" value="${initialSize}">property>  
  30.           
  31.         <property name="maxActive" value="${maxActive}">property>  
  32.           
  33.         <property name="maxIdle" value="${maxIdle}">property>  
  34.           
  35.         <property name="minIdle" value="${minIdle}">property>  
  36.           
  37.         <property name="maxWait" value="${maxWait}">property>  
  38.     bean>  
  39.   
  40.       
  41.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  42.         <property name="dataSource" ref="dataSource"/>  
  43.         <property name="configLocation">  
  44.             <value>classpath:mybatisConfig.xmlvalue>  
  45.         property>  
  46.           
  47.         <property name="mapperLocations" value="classpath:mapping/*.xml">property>  
  48.   
  49.     bean>  
  50.   
  51.       
  52.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  53.         <property name="basePackage" value="dao"/>  
  54.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>  
  55.     bean>  
  56.   
  57.   
  58.       
  59.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  60.         <property name="dataSource" ref="dataSource"/>  
  61.     bean>  
  62.   
  63.       
  64.     <tx:annotation-driven transaction-manager="transactionManager"/>  
  65. beans>  



六 搭建功能和测试

1 UserMapper.xml (位于resources/mapping文件夹下)

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8" ?>  
  2. >  
  3. <mapper namespace="dao.UserMapper" >  
  4.     <resultMap id="BaseResultMap" type="model.User" >  
  5.         <id column="UserID" property="userid" jdbcType="INTEGER" />  
  6.         <result column="UserName" property="username" jdbcType="VARCHAR" />  
  7.         <result column="RealName" property="realname" jdbcType="VARCHAR" />  
  8.         <result column="Password" property="password" jdbcType="VARCHAR" />  
  9.         <result column="IsLock" property="islock" jdbcType="INTEGER" />  
  10.         <result column="CreateTime" property="createtime" jdbcType="TIMESTAMP" />  
  11.     resultMap>  
  12.     <sql id="Base_Column_List" >  
  13.         UserID, UserName, RealName, Password, IsLock, CreateTime  
  14.     sql>  
  15.     <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >  
  16.         select  
  17.         <include refid="Base_Column_List" />  
  18.         from tb_user  
  19.         where UserID = #{userid,jdbcType=INTEGER}  
  20.     select>  
  21.     <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >  
  22.         delete from tb_user  
  23.         where UserID = #{userid,jdbcType=INTEGER}  
  24.     delete>  
  25.     <insert id="insert" parameterType="model.User" >  
  26.         insert into tb_user (UserID, UserName, RealName,  
  27.         Password, IsLock, CreateTime  
  28.         )  
  29.         values (#{userid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{realname,jdbcType=VARCHAR},  
  30.         #{password,jdbcType=VARCHAR}, #{islock,jdbcType=INTEGER}, #{createtime,jdbcType=TIMESTAMP}  
  31.         )  
  32.     insert>  
  33.     <insert id="insertSelective" parameterType="model.User" >  
  34.         insert into tb_user  
  35.         <trim prefix="(" suffix=")" suffixOverrides="," >  
  36.             <if test="userid != null" >  
  37.                 UserID,  
  38.             if>  
  39.             <if test="username != null" >  
  40.                 UserName,  
  41.             if>  
  42.             <if test="realname != null" >  
  43.                 RealName,  
  44.             if>  
  45.             <if test="password != null" >  
  46.                 Password,  
  47.             if>  
  48.             <if test="islock != null" >  
  49.                 IsLock,  
  50.             if>  
  51.             <if test="createtime != null" >  
  52.                 CreateTime,  
  53.             if>  
  54.         trim>  
  55.         <trim prefix="values (" suffix=")" suffixOverrides="," >  
  56.             <if test="userid != null" >  
  57.                 #{userid,jdbcType=INTEGER},  
  58.             if>  
  59.             <if test="username != null" >  
  60.                 #{username,jdbcType=VARCHAR},  
  61.             if>  
  62.             <if test="realname != null" >  
  63.                 #{realname,jdbcType=VARCHAR},  
  64.             if>  
  65.             <if test="password != null" >  
  66.                 #{password,jdbcType=VARCHAR},  
  67.             if>  
  68.             <if test="islock != null" >  
  69.                 #{islock,jdbcType=INTEGER},  
  70.             if>  
  71.             <if test="createtime != null" >  
  72.                 #{createtime,jdbcType=TIMESTAMP},  
  73.             if>  
  74.         trim>  
  75.     insert>  
  76.     <update id="updateByPrimaryKeySelective" parameterType="model.User" >  
  77.         update tb_user  
  78.         <set >  
  79.             <if test="username != null" >  
  80.                 UserName = #{username,jdbcType=VARCHAR},  
  81.             if>  
  82.             <if test="realname != null" >  
  83.                 RealName = #{realname,jdbcType=VARCHAR},  
  84.             if>  
  85.             <if test="password != null" >  
  86.                 Password = #{password,jdbcType=VARCHAR},  
  87.             if>  
  88.             <if test="islock != null" >  
  89.                 IsLock = #{islock,jdbcType=INTEGER},  
  90.             if>  
  91.             <if test="createtime != null" >  
  92.                 CreateTime = #{createtime,jdbcType=TIMESTAMP},  
  93.             if>  
  94.         set>  
  95.         where UserID = #{userid,jdbcType=INTEGER}  
  96.     update>  
  97.     <update id="updateByPrimaryKey" parameterType="model.User" >  
  98.         update tb_user  
  99.         set UserName = #{username,jdbcType=VARCHAR},  
  100.         RealName = #{realname,jdbcType=VARCHAR},  
  101.         Password = #{password,jdbcType=VARCHAR},  
  102.         IsLock = #{islock,jdbcType=INTEGER},  
  103.         CreateTime = #{createtime,jdbcType=TIMESTAMP}  
  104.         where UserID = #{userid,jdbcType=INTEGER}  
  105.     update>  
  106. mapper>  


2 User对象(model)

[java]  view plain  copy
  1. package model;  
  2.   
  3. import java.util.Date;  
  4.   
  5. public class User {  
  6.     private Integer userid;  
  7.   
  8.     private String username;  
  9.   
  10.     private String realname;  
  11.   
  12.     private String password;  
  13.   
  14.     private Integer islock;  
  15.   
  16.     private Date createtime;  
  17.   
  18.     public Integer getUserid() {  
  19.         return userid;  
  20.     }  
  21.   
  22.     public void setUserid(Integer userid) {  
  23.         this.userid = userid;  
  24.     }  
  25.   
  26.     public String getUsername() {  
  27.         return username;  
  28.     }  
  29.   
  30.     public void setUsername(String username) {  
  31.         this.username = username == null ? null : username.trim();  
  32.     }  
  33.   
  34.     public String getRealname() {  
  35.         return realname;  
  36.     }  
  37.   
  38.     public void setRealname(String realname) {  
  39.         this.realname = realname == null ? null : realname.trim();  
  40.     }  
  41.   
  42.     public String getPassword() {  
  43.         return password;  
  44.     }  
  45.   
  46.     public void setPassword(String password) {  
  47.         this.password = password == null ? null : password.trim();  
  48.     }  
  49.   
  50.     public Integer getIslock() {  
  51.         return islock;  
  52.     }  
  53.   
  54.     public void setIslock(Integer islock) {  
  55.         this.islock = islock;  
  56.     }  
  57.   
  58.     public Date getCreatetime() {  
  59.         return createtime;  
  60.     }  
  61.   
  62.     public void setCreatetime(Date createtime) {  
  63.         this.createtime = createtime;  
  64.     }  
  65. }  


3 dao

[java]  view plain  copy
  1. package dao;  
  2.   
  3. import model.User;  
  4.   
  5. public interface UserMapper {  
  6.     int deleteByPrimaryKey(Integer userid);  
  7.   
  8.     int insert(User record);  
  9.   
  10.     int insertSelective(User record);  
  11.   
  12.     User selectByPrimaryKey(Integer userid);  
  13.   
  14.     int updateByPrimaryKeySelective(User record);  
  15.   
  16.     int updateByPrimaryKey(User record);  
  17. }  


4 controller

(由于是测试,就不在创建 service和impl层了。直接调用通过就可以了。)

[java]  view plain  copy
  1. package controller;  
  2.   
  3. import dao.UserMapper;  
  4. import model.User;  
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.stereotype.Controller;  
  7. import org.springframework.ui.ModelMap;  
  8. import org.springframework.web.bind.annotation.RequestMapping;  
  9. import org.springframework.web.servlet.ModelAndView;  
  10.   
  11. import javax.servlet.http.HttpServletRequest;  
  12. import javax.servlet.http.HttpServletResponse;  
  13. import java.util.List;  
  14.   
  15. /** 
  16.  * Created by Tyler on 2017/6/20. 
  17.  */  
  18.   
  19. @Controller  
  20. @RequestMapping("test")  
  21. public class TestController {  
  22.   
  23.     @Autowired  
  24.     protected UserMapper service;  
  25.   
  26.     @RequestMapping(value = "test.do")  
  27.     public ModelAndView test1(HttpServletRequest request, HttpServletResponse response, ModelMap mode) throws Exception {  
  28.         User u =service.selectByPrimaryKey(1);  
  29.   
  30.         return new ModelAndView("test/test");  
  31.     }  
  32. }  

PS: protected UserMapper service; 如果这句报错,可以无视。运行没关系。

5 创建 page

webapp  -->  test  -->  test.jsp


最后运行一下页面就可以了。
http://localhost:8080/testssm/test/test.do

你可能感兴趣的:(用idea搭建简单ssm框架)