自己从事java开发工作也有一年多了,自己却没有亲手搭建一个完整的框架。于是今天自己动手搭建一个,过程中遇到一些问题,倒腾了大半天终于搞定了。
现在给大家分享一下过程,自己也记录下来,以后学习参考使用。
我采用全注解的方式去搭建springMVC,spring,mybatis框架,同时自己也可以去配置xml,但是主要还是通过注解的方式来实现。参考的网上其它大神分享的方法,多多少少有些地方写的不清楚,也并不是完全通过注解去实现的。
springboot可以真正说是零配置吧,后面我将给大家演示springboot搭建的过程。
1.先介绍一下我使用的开发环境:
idea,jdk1.7,tomcat7.0,操作系统就不用介绍了(winddos10)。
2.使用框架:
2.1:前台我使用freemarker模板,通常咋们说的ftl文件。
2.2:后台我使用springMVC,spring,mybatis。
2.3:数据库我使用mysql,也可以使用orcl,更换一下配置就OK。
3.管理工具:
maven
在开始贴出源码之前,我先贴出idea在创建maven项目以及在配置tomcat需要注意的几个问题,我在部署项目的时候,idea提示项目部署成功但是就是没有启动tomcat发布项目,搞了半天,真滴蛋疼。
1.我的架构目录,设置source和resource不要忘记了。
2.最关键的地方。
2.1:web Module Deployment Descriptor 后面的xml文件,是你项目的入口。一般是web.xml文件,检查路径是否正确。
2.2:web Resource Directory 是对应你webapp下部署到tomcat的文件。
2.3:source root 是你的源文件,检查是否正确。
3.有一个更重要的设置,大家务必要检查一下,不然项目就无法启动,也不报什么错误。就是下面截图的几个参数设置,要和你本地的jdk版本一致。
3.2:有个更更更重要的地方一定要检查一下,把你这些jar包都put到lib下面。
4.其他的配置我就不说了,都一样。如果大家要这前面的过程,在评论说出来,我整理出一个word文档分享给大家。
下面我就直接贴出源码了。
pom.xml .===这个地方我没有把版本组件提取出来,不过不影响使用,里面有些其他的jar在后面会使用其他的功能。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>cn.com.MyBigSys</groupId> 8 <artifactId>MyBigSys</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <!-- 定义使用组件的版本,单独提取出来方便以后升级! --> 11 <properties> 12 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 13 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 14 <spring.version>3.2.8.RELEASE</spring.version> 15 <mybatis.version>3.3.0</mybatis.version> 16 <mySql-driver.version>5.1.17</mySql-driver.version> 17 <durid.version>1.0.27</durid.version> 18 <fastjson.version>1.2.7</fastjson.version> 19 <aspectj.version>1.8.5</aspectj.version> 20 <mybatis-pagerHelper.version>4.1.0</mybatis-pagerHelper.version> 21 <mybatis-spring.version>1.2.3</mybatis-spring.version> 22 <mybatis-mapper.version>3.3.4</mybatis-mapper.version> 23 <jsqlparser.version>0.9.4</jsqlparser.version> 24 <log4j.version>1.2.17</log4j.version> 25 <dom4j.version>1.6.1</dom4j.version> 26 <aopalliance.version>1.0</aopalliance.version> 27 <fileupload.version>1.3.1</fileupload.version> 28 <jackson-mapper-asl.version>1.9.13</jackson-mapper-asl.version> 29 <jackson.version>2.5.1</jackson.version> 30 <quartz.version>2.2.2</quartz.version> 31 <commons-lang3.version>3.4</commons-lang3.version> 32 <commons-codec.version>1.10</commons-codec.version> 33 <servlet-api.version>2.5</servlet-api.version> 34 <jsp-api.version>2.2</jsp-api.version> 35 <junit.version>4.10</junit.version> 36 <httpclient.version>4.3.1</httpclient.version> 37 <httpmime.version>4.1.3</httpmime.version> 38 <lombok.version>1.14.8</lombok.version> 39 </properties> 40 41 <!-- 声明项目需要的依赖组件 --> 42 <dependencies> 43 44 <dependency> 45 <groupId>org</groupId> 46 <artifactId>jbarcode</artifactId> 47 <version>1.0.0</version> 48 </dependency> 49 <!--含spring的jar包--> 50 <dependency> 51 <groupId>org.springframework</groupId> 52 <artifactId>spring-beans</artifactId> 53 <version>${spring.version}</version> 54 </dependency> 55 56 <dependency> 57 <groupId>org.springframework</groupId> 58 <artifactId>spring-webmvc</artifactId> 59 <version>${spring.version}</version> 60 </dependency> 61 <dependency> 62 <groupId>org.springframework</groupId> 63 <artifactId>spring-core</artifactId> 64 <version>${spring.version}</version> 65 </dependency> 66 <dependency> 67 <groupId>org.springframework</groupId> 68 <artifactId>spring-aop</artifactId> 69 <version>${spring.version}</version> 70 </dependency> 71 <dependency> 72 <groupId>org.springframework</groupId> 73 <artifactId>spring-jdbc</artifactId> 74 <version>${spring.version}</version> 75 </dependency> 76 <dependency> 77 <groupId>org.springframework</groupId> 78 <artifactId>spring-context</artifactId> 79 <version>${spring.version}</version> 80 </dependency> 81 <dependency> 82 <groupId>org.springframework</groupId> 83 <artifactId>spring-context-support</artifactId> 84 <version>${spring.version}</version> 85 </dependency> 86 <dependency> 87 <groupId>org.springframework</groupId> 88 <artifactId>spring-web</artifactId> 89 <version>${spring.version}</version> 90 </dependency> 91 <dependency> 92 <groupId>org.springframework.data</groupId> 93 <artifactId>spring-data-jpa</artifactId> 94 <version>1.3.2.RELEASE</version> 95 </dependency> 96 97 <dependency> 98 <groupId>org.springframework.data</groupId> 99 <artifactId>spring-data-commons</artifactId> 100 <version>1.5.1.RELEASE</version> 101 </dependency> 102 <!--使用freemarker语法jar--> 103 <dependency> 104 <groupId>org.freemarker</groupId> 105 <artifactId>freemarker</artifactId> 106 <version>2.3.20</version> 107 </dependency> 108 <dependency> 109 <groupId>commons-dbcp</groupId> 110 <artifactId>commons-dbcp</artifactId> 111 <version>1.4</version> 112 </dependency> 113 <!--关于log4j日志jar--> 114 <dependency> 115 <groupId>org.slf4j</groupId> 116 <artifactId>slf4j-log4j12</artifactId> 117 <version>1.7.2</version> 118 </dependency> 119 <dependency> 120 <groupId>log4j</groupId> 121 <artifactId>log4j</artifactId> 122 <version>1.2.17</version> 123 </dependency> 124 <dependency> 125 <groupId>commons-io</groupId> 126 <artifactId>commons-io</artifactId> 127 <version>2.4</version> 128 </dependency> 129 <dependency> 130 <groupId>commons-fileupload</groupId> 131 <artifactId>commons-fileupload</artifactId> 132 <version>1.2.2</version> 133 </dependency> 134 <dependency> 135 <groupId>org.javassist</groupId> 136 <artifactId>javassist</artifactId> 137 <version>3.18.0-GA</version> 138 </dependency> 139 <dependency> 140 <groupId>junit</groupId> 141 <artifactId>junit</artifactId> 142 <version>4.11</version> 143 <scope>test</scope> 144 </dependency> 145 <dependency> 146 <groupId>commons-beanutils</groupId> 147 <artifactId>commons-beanutils</artifactId> 148 <version>1.8.3</version> 149 </dependency> 150 <dependency> 151 <groupId>javax.servlet</groupId> 152 <artifactId>servlet-api</artifactId> 153 <version>2.5</version> 154 <scope>provided</scope> 155 </dependency> 156 <dependency> 157 <groupId>javax.servlet.jsp</groupId> 158 <artifactId>jsp-api</artifactId> 159 <version>2.2</version> 160 <scope>provided</scope> 161 </dependency> 162 <!--mybatis使用jar--> 163 <dependency> 164 <groupId>org.mybatis.generator</groupId> 165 <artifactId>mybatis-generator-core</artifactId> 166 <version>1.3.2</version> 167 <scope>compile</scope> 168 <optional>true</optional> 169 </dependency> 170 <dependency> 171 <groupId>org.mybatis</groupId> 172 <artifactId>mybatis</artifactId> 173 <version>3.2.6</version> 174 </dependency> 175 <dependency> 176 <groupId>org.mybatis</groupId> 177 <artifactId>mybatis-spring</artifactId> 178 <version>1.2.0</version> 179 </dependency> 180 181 182 <!--连接oral驱动--> 183 <dependency> 184 <groupId>com.oracle</groupId> 185 <artifactId>ojdbc6</artifactId> 186 <version>11.2.0.4</version> 187 </dependency> 188 <!--连接mysql驱动--> 189 <dependency> 190 <groupId>mysql</groupId> 191 <artifactId>mysql-connector-java</artifactId> 192 <version>5.1.38</version> 193 </dependency> 194 <dependency> 195 <groupId>org.apache.ibatis</groupId> 196 <artifactId>ibatis-sqlmap</artifactId> 197 <version>2.3.4.726</version> 198 </dependency> 199 <dependency> 200 <groupId>com.jacob</groupId> 201 <artifactId>jacob</artifactId> 202 <version>1.17</version> 203 </dependency> 204 205 <dependency> 206 <groupId>org.apache.commons</groupId> 207 <artifactId>commons-compress</artifactId> 208 <version>1.5</version> 209 </dependency> 210 211 <dependency> 212 <groupId>org.apache.commons</groupId> 213 <artifactId>commons-lang3</artifactId> 214 <version>3.3.2</version> 215 </dependency> 216 217 <dependency> 218 <groupId>com.alibaba</groupId> 219 <artifactId>fastjson</artifactId> 220 <version>1.1.28</version> 221 </dependency> 222 223 <dependency> 224 <groupId>javax.persistence</groupId> 225 <artifactId>persistence-api</artifactId> 226 <version>1.0</version> 227 </dependency> 228 <dependency> 229 <groupId>org.springframework.amqp</groupId> 230 <artifactId>spring-rabbit</artifactId> 231 <version>1.3.6.RELEASE</version> 232 </dependency> 233 <dependency> 234 <groupId>org.aspectj</groupId> 235 <artifactId>aspectjweaver</artifactId> 236 <version>1.8.0</version> 237 </dependency> 238 <dependency> 239 <groupId>org.aspectj</groupId> 240 <artifactId>aspectjrt</artifactId> 241 <version>1.8.0</version> 242 </dependency> 243 <dependency> 244 <groupId>QRCode</groupId> 245 <artifactId>QRCode</artifactId> 246 <version>1.0.0</version> 247 </dependency> 248 249 <dependency> 250 <groupId>net.sf.json-lib</groupId> 251 <artifactId>json-lib</artifactId> 252 <version>2.4</version> 253 <classifier>jdk15</classifier> 254 </dependency> 255 256 <dependency> 257 <groupId>org.quartz-scheduler</groupId> 258 <artifactId>quartz</artifactId> 259 <version>1.8.5</version> 260 </dependency> 261 <dependency> 262 <groupId>javax.servlet</groupId> 263 <artifactId>javax.servlet-api</artifactId> 264 <version>3.1.0</version> 265 </dependency> 266 <dependency> 267 <groupId>org.projectlombok</groupId> 268 <artifactId>lombok</artifactId> 269 <version>1.16.10</version> 270 </dependency> 271 <!--新增jar包 --> 272 <dependency> 273 <groupId>org.springframework.security</groupId> 274 <artifactId>spring-security-core</artifactId> 275 <version>2.0.7.RELEASE</version> 276 </dependency> 277 <!--阿里巴巴druid数据库jar包--> 278 <dependency> 279 <groupId>com.alibaba</groupId> 280 <artifactId>druid</artifactId> 281 <version>1.1.0</version> 282 </dependency> 283 </dependencies> 284 285 <build> 286 <plugins> 287 <plugin> 288 <groupId>org.apache.maven.plugins</groupId> 289 <artifactId>maven-war-plugin</artifactId> 290 <version>2.2</version> 291 </plugin> 292 <plugin> 293 <groupId>org.apache.maven.plugins</groupId> 294 <artifactId>maven-compiler-plugin</artifactId> 295 <version>2.3.2</version> 296 <!-- <version>3.1</version> --> 297 <configuration> 298 <source>1.7</source> 299 <target>1.7</target> 300 <encoding>UTF-8</encoding> 301 <compilerArguments> 302 <verbose /> 303 <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath> 304 </compilerArguments> 305 </configuration> 306 </plugin> 307 </plugins> 308 </build> 309 </project>
5.web.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 version="3.1"> 6 <display-name>SSMTest</display-name> 7 <description>SSMTest Application</description> 8 <!--配置spring启动--> 9 <context-param> 10 <param-name>contextConfigLocation</param-name> 11 <param-value>classpath:application.xml</param-value> 12 </context-param> 13 <listener> 14 <listener-class> 15 org.springframework.web.context.ContextLoaderListener 16 </listener-class> 17 </listener> 18 <filter> 19 <filter-name>encodingFilter</filter-name> 20 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 21 <init-param> 22 <param-name>encoding</param-name> 23 <param-value>UTF-8</param-value> 24 </init-param> 25 <init-param> 26 <param-name>forceEncoding</param-name> 27 <param-value>true</param-value> 28 </init-param> 29 </filter> 30 <filter-mapping> 31 <filter-name>encodingFilter</filter-name> 32 <url-pattern>/*</url-pattern> 33 </filter-mapping> 34 <!--配置springmvc启动--> 35 <servlet> 36 <servlet-name>springMVC</servlet-name> 37 <servlet-class> 38 org.springframework.web.servlet.DispatcherServlet 39 </servlet-class> 40 <init-param> 41 <param-name>contextConfigLocation</param-name> 42 <param-value>classpath:spring-mvc.xml</param-value> 43 </init-param> 44 <load-on-startup>1</load-on-startup> 45 </servlet> 46 <servlet-mapping> 47 <servlet-name>springMVC</servlet-name> 48 <url-pattern>/</url-pattern> 49 </servlet-mapping> 50 </web-app>
6.下面来说src下的目录结构,如下图
7.application.xml 文件
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:context="http://www.springframework.org/schema/context" 4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.2.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> 14 <!-- 加载数据库配置文件--> 15 <context:property-placeholder location="classpath:jdbc.properties"/> 16 17 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" 18 destroy-method="close"> 19 <!--数据库驱动--> 20 <property name="driverClassName" value="${jdbc.driver}"/> 21 <!--连接数据库的url--> 22 <property name="url" value="${jdbc.url}"/> 23 <!--连接数据库的用户名--> 24 <property name="username" value="${jdbc.username}"/> 25 <!--连接数据库的密码--> 26 <property name="password" value="${jdbc.password}"/> 27 <!--最大连接数--> 28 <property name="maxActive" value="${jdbc.maxActive}"/> 29 <!--最大空闲数--> 30 <property name="maxIdle" value="${jdbc.maxIdle}"/> 31 <!--初始化连接数--> 32 <property name="initialSize" value="${jdbc.initialSize}"/> 33 </bean> 34 <tx:annotation-driven/> 35 <!--配置事务管理器,依赖于数据源--> 36 <bean id="transCation" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 37 <property name="dataSource" ref="dataSource"/> 38 </bean> 39 <!--配置session工厂--> 40 <bean id="sqlSeeesionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 41 <property name="dataSource" ref="dataSource"/> 42 <property name="configLocation" value="classpath:mybatis.xml"/> 43 </bean> 44 <!--mapper动态代理开发--> 45 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 46 <property name="basePackage" value="com.panpan.service"></property> 47 </bean> 48 </beans>
8.spring-mvc.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 7 http://www.springframework.org/schema/mvc 8 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.2.xsd "> 11 <!--spring2.5以后不需要配置注解启动--> 12 <!-- <context:annotation-config/> 13 <!– 启用spring mvc 注解 –> 14 --> 15 <!-- 设置使用注解的类所在的jar包 --> 16 <!-- <context:component-scan base-package="com.xxx.webController" /> --> 17 <mvc:annotation-driven /> 18 <context:component-scan base-package="com.panpan" /> 19 <mvc:default-servlet-handler/> 20 <!-- 完成请求和注解POJO的映射 --> 21 <bean 22 class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 23 <!--freemark配置--> 24 <bean id="freemarkConfig" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 25 <property name="location" value="classpath:freemarker.properties"></property> 26 </bean> 27 <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> 28 <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"></property> 29 <property name="cache" value="true"></property> 30 <property name="suffix" value=".ftl"></property> 31 <property name="contentType" value="text/html; charset=UTF-8"></property> 32 </bean> 33 <bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> 34 <property name="templateLoaderPath" value="/WEB-INF/views/"></property> 35 <property name="freemarkerSettings" ref="freemarkConfig"></property> 36 </bean> 37 </beans>
9.mybatis.xml文件,mybatis和spring整合之后配置文件很简单了。
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 6 <configuration> 7 <settings> 8 <!--配置实体类与数据库字段驼峰命名--> 9 <setting name="mapUnderscoreToCamelCase" value="true"/> 10 </settings> 11 <!-- 设置别名 --> 12 <typeAliases> 13 <!-- 2. 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 --> 14 <package name="com.panpan.entity" /> 15 </typeAliases> 16 17 </configuration>
10.log4j.properties日志文件
1 log4j.rootLogger=info,Console 2 3 log4j.appender.Console=org.apache.log4j.ConsoleAppender 4 log4j.appender.Console.encoding=utf-8 5 log4j.appender.Console.layout=org.apache.log4j.PatternLayout 6 log4j.appender.Console.layout.ConversionPattern=[%c]%m%n 7 8 #log4j.appender.File=org.apache.log4j.RollingFileAppender 9 log4j.appender.File=org.apache.log4j.DailyRollingFileAppender 10 11 log4j.appender.File.DatePattern = ‘.‘yyyy-MM-dd 12 log4j.appender.File.File=logs/SpringMvc.log 13 #log4j.appender.File.MaxFileSize=10MB 14 log4j.appender.File.Threshold=ALL 15 log4j.appender.File.layout=org.apache.log4j.PatternLayout 16 log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
11.freemarker.properties文件
1 tag_syntax=auto_detect 2 template_update_delay=60 3 default_encoding=UTF-8 4 output_encoding=UTF-8 5 locale=zh_CN 6 date_format=yyyy-MM-dd 7 time_format=HH\:mm\:ss 8 datetime_format=yyyy-MM-dd HH\:mm\:ss 9 number_format=\#
12.jdbc.properties数据库连接配置文件
1 jdbc.driver=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/school_bl 3 jdbc.username=root 4 jdbc.password=root 5 jdbc.maxActive=30 6 jdbc.maxIdle=10 7 jdbc.initialSize=5
13.实体类User.java这里我采用了lombok提供的@Data注解,省去了getter和setter方法。pom.xml中引入lombok依赖,如果使用idea,那在安装一下lombok的插件就可以使用啦。
1 package com.panpan.entity; 2 3 import lombok.Data; 4 5 import javax.persistence.Id; 6 import javax.persistence.Table; 7 8 /** 9 * Created by panpan on 2018/2/27. 10 */ 11 @Table(name = "user") 12 @Data 13 public class User { 14 @Id 15 private String id; 16 private String loginName; 17 private String loginPassword; 18 }
14.user接口定义,这个也不需要实现类。
1 package com.panpan.service.inter; 2 3 import com.panpan.entity.User; 4 import org.apache.ibatis.annotations.Select; 5 import org.springframework.stereotype.Service; 6 7 import java.util.List; 8 9 /** 10 * Created by panpan on 2018/3/2. 11 */ 12 @Service 13 public interface UserInter { 14 @Select("select * from school_bl.user t where t.id=#{id}") 15 public User findById(String id); 16 17 @Select("select * from school_bl.user") 18 public List<User> findAll(); 19 }
15.controller控制器层
1 package com.panpan.webController; 2 3 import com.panpan.entity.User; 4 import com.panpan.service.inter.UserInter; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.ResponseBody; 9 import org.springframework.web.servlet.ModelAndView; 10 11 import java.util.List; 12 13 /** 14 * Created by panpan on 2018/2/18. 15 */ 16 @Controller 17 @RequestMapping("sys") 18 public class Login { 19 @Autowired 20 UserInter userInter; 21 ModelAndView modelAndView = new ModelAndView(); 22 23 @RequestMapping("login") 24 @ResponseBody 25 public ModelAndView getLogin() { 26 27 User user = userInter.findById("1"); 28 modelAndView.addObject("user", user); 29 modelAndView.setViewName("login/login"); 30 return modelAndView; 31 } 32 33 @RequestMapping("getAllUsers") 34 @ResponseBody 35 public ModelAndView getAllUsers() { 36 List<User> users = userInter.findAll(); 37 modelAndView.addObject("users", users); 38 modelAndView.setViewName("test"); 39 return modelAndView; 40 } 41 }
16.把以上的都写完之后,咱们来看看web-app下面的目录结构,这些就是页面啦,先测试一下,让整个框架跑起来。
17.test.ftl页面
1 <h2> 2 <#if users??> 3 <#list users as user > 4 <td>${user.id!}</td> 5 <td> ${user.loginName!}</td> 6 <td>${user.loginPassword!}</td> 7 </br> 8 </#list> 9 </#if> 10 </h2>
18.login.ftl页面
1 <h2> 2 ${user.id!} 3 ${user.loginName!} 4 ${user.loginPassword!} 5 </h2>
19.mysql数据库结构
20.然后就可以把程序跑起来了,看的以下结果。
ok啦,框架能正常跑起来了,后面第二次内容将会添加更新,插入和删除数据,包括前台获取参数,分页查询。
感谢各位大牛们能看到结尾,如果有问题的地方可以在评论提出来,大家一起交流学习,您们的点评就是我学习的动力!
如果需要源码的话,也留个言哦,我把gtiHup上的地址给大家。