于软件行业混迹了很多年,谈不上做了哪些很牛的大项目,林林总总也服务过好几个公司,虽与大牛级别的人物大相径庭,但我还是能自信已经是超越了菜鸟级,无论是简单还是复杂点的项目,若独立去做也还是勉强可以接受与完成。我深知自身知识比较薄弱,不咋会写技能技术之类的文章,许多东西都无从下手或下口,希望努力能够有所弥补吧。
开发中用了许多东西,平常我却少于总结,许多仿佛就是为了适应工作,完成工作而用,用过了一段时间不用就又忘记了,因此自己想找些简单的东西,写写记记,万一能使自己有所进步呢。
下面就简单搭建一个近几年开发中常使用的框架SSM(Spring+SpringMVC+Mybatis),使用maven作为项目开发管理工具。
• 我的开发工具:idea2018.1.2;
• 系统:Windows 10;
• jdk版本:jdk8;
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.lk.springmvc.demogroupId>
<artifactId>springmvc-testartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<name>springmvc-testMavenWebappname>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.7maven.compiler.source>
<maven.compiler.target>1.7maven.compiler.target>
<spring.version>5.0.7.RELEASEspring.version>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
<version>3.7version>
dependency>
<dependency>
<groupId>commons-collectionsgroupId>
<artifactId>commons-collectionsartifactId>
<version>3.2.2version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>commons-dbcpgroupId>
<artifactId>commons-dbcpartifactId>
<version>1.4version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.11version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.6version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.9.6version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
<version>2.9.6version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.9.6version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.3version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
dependencies>
<build>
<finalName>springmvc-testfinalName>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
resource>
resources>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-pluginartifactId>
<version>3.0.0version>
plugin>
<plugin>
<artifactId>maven-resources-pluginartifactId>
<version>3.0.2version>
plugin>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<version>3.7.0version>
plugin>
<plugin>
<artifactId>maven-surefire-pluginartifactId>
<version>2.20.1version>
plugin>
<plugin>
<artifactId>maven-war-pluginartifactId>
<version>3.2.0version>
plugin>
<plugin>
<artifactId>maven-install-pluginartifactId>
<version>2.5.2version>
plugin>
<plugin>
<artifactId>maven-deploy-pluginartifactId>
<version>2.8.2version>
plugin>
plugins>
pluginManagement>
build>
project>
• 配置数据库连接信息文件:jdbc.properties(注:我引用了最新的MySQL数据库驱动,有早期版本配置略有差别)
#######jdbc############
#mysql-connector-java5之前的之前的驱动
#jdbc.driverClassName=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
#我当前引用的最新版本,所以改用mysql-connector-java6以后的驱动
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
#url
jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
#####
jdbc.username=root
jdbc.password=123456
dbcp.maxActive=5
dbcp.maxIdle=200
• spring-context.xml配置
主要引入数据源配置,配置一个数据源DataSource,配置SqlSessionFactoryBean,用于注册并扫描mapper中的sql语句,配置MapperScannerConfigurer,自动扫描DAO接口,配置注解方式事务,配置自动扫描包,注解等。
<beansxmlns="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.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd">
<beanid="config"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertyname="location"value="classpath:jdbc.properties"/>
bean>
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
<propertyname="maxActive"value="${dbcp.maxActive}"/>
<propertyname="maxIdle"value="${dbcp.maxIdle}"/>
<propertyname="driverClassName"value="${jdbc.driverClassName}"/>
<propertyname="url"value="${jdbc.url}"/>
<propertyname="username"value="${jdbc.username}"/>
<propertyname="password"value="${jdbc.password}"/>
<propertyname="maxWait"value="1000"/>
<propertyname="removeAbandoned"value="true"/>
<propertyname="removeAbandonedTimeout"value="60"/>
<propertyname="defaultAutoCommit"value="true"/>
bean>
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
bean>
<beanid="sqlSession"class="org.mybatis.spring.SqlSessionTemplate">
<constructor-argindex="0"ref="sqlSessionFactory"/>
bean>
<beanid="mapperScanner"class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname="basePackage"value="com.lk.springmvc.demo.pojo,com.lk.springmvc.demo.dao"/>
<propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>
bean>
<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"/>
bean>
<tx:annotation-driventransaction-manager="transactionManager"/>
<context:component-scanbase-package="com.lk.springmvc.demo.service.impl"/>
beans>
• spring-servlet.xml配置
主要配置SpringMVC前端控制器扫描包,对视图的解析,配置静态资源,json转换器,上传文件,拦截器等
<beans:beansxmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache.xsd">
<resourcesmapping="/static/**"location="/WEB-INF/static/"/>
<context:component-scanbase-package="com.lk.springmvc.demo.controller"/>
<beans:beanid="resourceViewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:propertyname="prefix"value="/WEB-INF/views/"/>
<beans:propertyname="suffix"value=".jsp"/>
<beans:propertyname="order"value="0"/>
beans:bean>
<annotation-driven>
<message-converters>
<beans:beanid="stringHttpMessageConverter"class="org.springframework.http.converter.StringHttpMessageConverter">
<beans:propertyname="supportedMediaTypes">
<beans:list>
<beans:value>text/html;charset=UTF-8beans:value>
<beans:value>application/json;charset=UTF-8beans:value>
beans:list>
beans:property>
beans:bean>
<beans:beanid="mappingJackson2HttpMessageConverter"class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<beans:propertyname="supportedMediaTypes">
<beans:list>
<beans:value>text/html;charset=UTF-8beans:value>
<beans:value>application/json;charset=UTF-8beans:value>
beans:list>
beans:property>
beans:bean>
message-converters>
annotation-driven>
<beans:beanid="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<beans:propertyname="maxUploadSize"value="1024000000"/>
beans:bean>
beans:beans>
• log4j.properties日志输出信息配置
#配置根Logger后面是若干个Appender
log4j.rootLogger=DEBUG,A1,R
#log4j.rootLogger=INFO,A1,R
#ConsoleAppender输出
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-ddHH:mm:ss,SSS}[%c]-[%p]%m%n
#File输出一天一个文件,输出路径可以定制,一般在根路径下
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=log.txt
#log4j.appender.R.MaxFileSize=500KB
#log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss,SSS}[%t][%c][%p]-%m%n
•web.xml配置
配置监听器,过滤器,初始化容器加载的配置文件路径,springMVC核心控制器
<web-appxmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>SpringMVCTestdisplay-name>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:spring-context.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>springMVCservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:spring-servlet.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springMVCservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>CharacterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
web-app>
至此,可在webapp下创建一个index.jsp页面,将项目添加至Tomcat中,启动,浏览器地址上访问: http://localhost:8080/spmvc ,若不出意外,项目应该是可以正常运行的。通过web.xml配置的默认欢迎页面,会加载出index.jsp内容,我的项目在浏览器上会显示出HelloWorld!
当然,这么多配置文件,在编写时,不可避免会有书写上的错误,或者引入包的缺失等,遇到这些情况,要根据错误提示信息,耐心寻找原因,解决错误。
表字段根据需求创建,这里为了完整只是简单的举例。
CREATE DATABASE `test` ;
USE `test`;
/*Table structure for table `user` */
CREATE TABLE `user` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(30) DEFAULT NULL,
`password` VARCHAR(50) DEFAULT NULL,
`sex` INT(11) DEFAULT NULL,
`createtime` DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8;
indexController:简单测试,没有任何业务层逻辑。
@Controller
public class indexController{
private final static Logger log=LoggerFactory.getLogger(indexController.class);
@RequestMapping("index")
public String index(){
return "index";
}
@RequestMapping("json")
@ResponseBody
public String json(){
return "success:0";
}
}
QueryController:简单实现了对用户表的增删改查操作
@Controller
@RequestMapping("user")
public class QueryController{
@Autowired
private UserService userService;
@RequestMapping("ulist")
public String getListUser(Model model){
List
对应视图层页面
启动项目,访问地址: http://localhost:8080/spmvc/user/ulist ,最后执行预览效果如图
业务逻辑层比较简单,只是实现了用户的增删改查接口。数据层和实体层,我这里是自己写的,大家可以使用Mybatis Generator自动生成实体类、DAO接口以及Mapping映射文件,其教程很多。
UserMapper:使用mybatis注解sql
/**
*可使用mybatis注解sql
*用@Select注解sql语句
*@return
*/
@Select("select * from user")
List
UserExtendMapper:通过mybatis提供的各种标签实现动态拼接sql语句
@Repository
public interface UserExtendMapper extends UserMapper{
/**
*添加
*@paramuser
*/
void insert(User user);
/**
*多条件查询
*@parammap
*@return
*/
List query(Map map);
/**
*更新
*@paramuser
*/
void updateUser(User user);
/**
*删除
*@paramid
*/
void delById(int id);
}
UserExtendMapper.xml文件
<mapper namespace="com.lk.springmvc.demo.dao.UserExtendMapper">
<insert id="insert" parameterType="com.lk.springmvc.demo.pojo.User">
insert into user(username,password,sex)values(#{username},#{password},#{sex})
insert>
<select id="query" parameterType="java.util.Map" resultType="com.lk.springmvc.demo.pojo.User">
select*from user
<where>
<if test="id!=null">
and id=#{id}
if>
<if test="name!=null and name!=''">
and username like'%${name}%'
if>
where>
select>
<update id="updateUser" parameterType="com.lk.springmvc.demo.pojo.User">
update user set username=#{username},password=#{password},sex=#{sex}
where id=#{id}
update>
<delete id="delById" parameterType="java.lang.Integer">
delete from user
where id=#{id}
delete>
mapper>
以上就是我搭建一个完整SSM框架的过程,不足之处请谅解指正,若感兴趣那就尝试着自己搭建一个吧,以便更好的理解知识。光看不练等于白看,没有看出来的系统,只有敲出来的代码。
https://gitee.com/keli88/spring-boot-demo/tree/master/springmvc-test