springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来

自己从事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不要忘记了。

springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来

2.最关键的地方。

2.1:web Module Deployment Descriptor 后面的xml文件,是你项目的入口。一般是web.xml文件,检查路径是否正确。

2.2:web Resource Directory 是对应你webapp下部署到tomcat的文件。

2.3:source root 是你的源文件,检查是否正确。

springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来

3.有一个更重要的设置,大家务必要检查一下,不然项目就无法启动,也不报什么错误。就是下面截图的几个参数设置,要和你本地的jdk版本一致。

springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来

3.2:有个更更更重要的地方一定要检查一下,把你这些jar包都put到lib下面。

springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来

4.其他的配置我就不说了,都一样。如果大家要这前面的过程,在评论说出来,我整理出一个word文档分享给大家。

下面我就直接贴出源码了。

pom.xml  .===这个地方我没有把版本组件提取出来,不过不影响使用,里面有些其他的jar在后面会使用其他的功能。

 <?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>cn.com.MyBigSys</groupId>
<artifactId>MyBigSys</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 定义使用组件的版本,单独提取出来方便以后升级! -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring.version>3.2.8.RELEASE</spring.version>
<mybatis.version>3.3.0</mybatis.version>
<mySql-driver.version>5.1.17</mySql-driver.version>
<durid.version>1.0.27</durid.version>
<fastjson.version>1.2.7</fastjson.version>
<aspectj.version>1.8.5</aspectj.version>
<mybatis-pagerHelper.version>4.1.0</mybatis-pagerHelper.version>
<mybatis-spring.version>1.2.3</mybatis-spring.version>
<mybatis-mapper.version>3.3.4</mybatis-mapper.version>
<jsqlparser.version>0.9.4</jsqlparser.version>
<log4j.version>1.2.17</log4j.version>
<dom4j.version>1.6.1</dom4j.version>
<aopalliance.version>1.0</aopalliance.version>
<fileupload.version>1.3.1</fileupload.version>
<jackson-mapper-asl.version>1.9.13</jackson-mapper-asl.version>
<jackson.version>2.5.1</jackson.version>
<quartz.version>2.2.2</quartz.version>
<commons-lang3.version>3.4</commons-lang3.version>
<commons-codec.version>1.10</commons-codec.version>
<servlet-api.version>2.5</servlet-api.version>
<jsp-api.version>2.2</jsp-api.version>
<junit.version>4.10</junit.version>
<httpclient.version>4.3.1</httpclient.version>
<httpmime.version>4.1.3</httpmime.version>
<lombok.version>1.14.8</lombok.version>
</properties> <!-- 声明项目需要的依赖组件 -->
<dependencies> <dependency>
<groupId>org</groupId>
<artifactId>jbarcode</artifactId>
<version>1.0.0</version>
</dependency>
<!--含spring的jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.3.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
<!--使用freemarker语法jar-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!--关于log4j日志jar-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.0-GA</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<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.2</version>
<scope>provided</scope>
</dependency>
<!--mybatis使用jar-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
</dependency> <!--连接oral驱动-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
<!--连接mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-sqlmap</artifactId>
<version>2.3.4.726</version>
</dependency>
<dependency>
<groupId>com.jacob</groupId>
<artifactId>jacob</artifactId>
<version>1.17</version>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.5</version>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.28</version>
</dependency> <dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>QRCode</groupId>
<artifactId>QRCode</artifactId>
<version>1.0.0</version>
</dependency> <dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency> <dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<!--新增jar包 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>2.0.7.RELEASE</version>
</dependency>
<!--阿里巴巴druid数据库jar包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<!-- <version>3.1</version> -->
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<verbose />
<bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
</plugins>
</build>
</project>

5.web.xml文件

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>SSMTest</display-name>
<description>SSMTest Application</description>
<!--配置spring启动-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置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:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

6.下面来说src下的目录结构,如下图

springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来

7.application.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<!-- 加载数据库配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
destroy-method="close">
<!--数据库驱动-->
<property name="driverClassName" value="${jdbc.driver}"/>
<!--连接数据库的url-->
<property name="url" value="${jdbc.url}"/>
<!--连接数据库的用户名-->
<property name="username" value="${jdbc.username}"/>
<!--连接数据库的密码-->
<property name="password" value="${jdbc.password}"/>
<!--最大连接数-->
<property name="maxActive" value="${jdbc.maxActive}"/>
<!--最大空闲数-->
<property name="maxIdle" value="${jdbc.maxIdle}"/>
<!--初始化连接数-->
<property name="initialSize" value="${jdbc.initialSize}"/>
</bean>
<tx:annotation-driven/>
<!--配置事务管理器,依赖于数据源-->
<bean id="transCation" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置session工厂-->
<bean id="sqlSeeesionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<!--mapper动态代理开发-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.panpan.service"></property>
</bean>
</beans>

8.spring-mvc.xml文件

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
<!--spring2.5以后不需要配置注解启动-->
<!-- <context:annotation-config/>
&lt;!&ndash; 启用spring mvc 注解 &ndash;&gt;
-->
<!-- 设置使用注解的类所在的jar包 -->
<!-- <context:component-scan base-package="com.xxx.webController" /> -->
<mvc:annotation-driven />
<context:component-scan base-package="com.panpan" />
<mvc:default-servlet-handler/>
<!-- 完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!--freemark配置-->
<bean id="freemarkConfig" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:freemarker.properties"></property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"></property>
<property name="cache" value="true"></property>
<property name="suffix" value=".ftl"></property>
<property name="contentType" value="text/html; charset=UTF-8"></property>
</bean>
<bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/views/"></property>
<property name="freemarkerSettings" ref="freemarkConfig"></property>
</bean>
</beans>

9.mybatis.xml文件,mybatis和spring整合之后配置文件很简单了。

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration>
<settings>
<!--配置实体类与数据库字段驼峰命名-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- 设置别名 -->
<typeAliases>
<!-- 2. 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->
<package name="com.panpan.entity" />
</typeAliases> </configuration>

10.log4j.properties日志文件

 log4j.rootLogger=info,Console

 log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.encoding=utf-8
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c]%m%n #log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File=org.apache.log4j.DailyRollingFileAppender log4j.appender.File.DatePattern = '.'yyyy-MM-dd
log4j.appender.File.File=logs/SpringMvc.log
#log4j.appender.File.MaxFileSize=10MB
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n

11.freemarker.properties文件

 tag_syntax=auto_detect
template_update_delay=60
default_encoding=UTF-8
output_encoding=UTF-8
locale=zh_CN
date_format=yyyy-MM-dd
time_format=HH\:mm\:ss
datetime_format=yyyy-MM-dd HH\:mm\:ss
number_format=\#

12.jdbc.properties数据库连接配置文件

 jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/school_bl
jdbc.username=root
jdbc.password=root
jdbc.maxActive=30
jdbc.maxIdle=10
jdbc.initialSize=5

13.实体类User.java这里我采用了lombok提供的@Data注解,省去了getter和setter方法。pom.xml中引入lombok依赖,如果使用idea,那在安装一下lombok的插件就可以使用啦。

 package com.panpan.entity;

 import lombok.Data;

 import javax.persistence.Id;
import javax.persistence.Table; /**
* Created by panpan on 2018/2/27.
*/
@Table(name = "user")
@Data
public class User {
@Id
private String id;
private String loginName;
private String loginPassword;
}

14.user接口定义,这个也不需要实现类。

 package com.panpan.service.inter;

 import com.panpan.entity.User;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service; import java.util.List; /**
* Created by panpan on 2018/3/2.
*/
@Service
public interface UserInter {
@Select("select * from school_bl.user t where t.id=#{id}")
public User findById(String id); @Select("select * from school_bl.user")
public List<User> findAll();
}

15.controller控制器层

 package com.panpan.webController;

 import com.panpan.entity.User;
import com.panpan.service.inter.UserInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView; import java.util.List; /**
* Created by panpan on 2018/2/18.
*/
@Controller
@RequestMapping("sys")
public class Login {
@Autowired
UserInter userInter;
ModelAndView modelAndView = new ModelAndView(); @RequestMapping("login")
@ResponseBody
public ModelAndView getLogin() { User user = userInter.findById("1");
modelAndView.addObject("user", user);
modelAndView.setViewName("login/login");
return modelAndView;
} @RequestMapping("getAllUsers")
@ResponseBody
public ModelAndView getAllUsers() {
List<User> users = userInter.findAll();
modelAndView.addObject("users", users);
modelAndView.setViewName("test");
return modelAndView;
}
}

16.把以上的都写完之后,咱们来看看web-app下面的目录结构,这些就是页面啦,先测试一下,让整个框架跑起来。

springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来

springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来

17.test.ftl页面

 <h2>
<#if users??>
<#list users as user >
<td>${user.id!}</td>
<td> ${user.loginName!}</td>
<td>${user.loginPassword!}</td>
</br>
</#list>
</#if>
</h2>

18.login.ftl页面

 <h2>
${user.id!}
${user.loginName!}
${user.loginPassword!}
</h2>

19.mysql数据库结构

springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来

springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来

20.然后就可以把程序跑起来了,看的以下结果。

springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来

springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来

ok啦,框架能正常跑起来了,后面第二次内容将会添加更新,插入和删除数据,包括前台获取参数,分页查询。

感谢各位大牛们能看到结尾,如果有问题的地方可以在评论提出来,大家一起交流学习,您们的点评就是我学习的动力!

如果需要源码的话,也留个言哦,我把gtiHup上的地址给大家。

上一篇:Openjudge-NOI题库-对齐输出


下一篇:Extjs 4.2.0 MVC 架构