最近一直用springboot开发,今天突然想用ssm框架开发一下东西,发现有点忘了步骤,懒人工具SpringBoot使用多了还是会让人退化啊,于是准备从头理一遍ssm整合,最后再加上一个Shiro认证和权限框架。
工具:IDEA 2017.2.1
框架:Spring 4.3.10RELEASE,Mybatis 3.4.4 ,Shiro 最新版1.3.2,Maven3.3.9
源码地址:http://git.oschina.net/sunknightzy/ssms
预备工作
首先新建一个动态的Maven WEB模块(IDEA中的Module【模块】相当于eclipse中的Project)
选中如图的选项,点击next
一直下一步到最后一步,配置目录
点击finish,完成,等待构建完成,如图
和标准的maven目录相差目录需要手动补全,点击菜单栏中的File--Project Structure--Module--ssms
将目录补全,我这里先不补test文件夹,需要的时候再添加,我的结构图
新建controller(控制器)、service(业务逻辑层)、dao(数据访问层)、commons(通用工具类)、entity(实体类)包,结构如下
好的,结构搭建好了,然后开始动手整合吧,
第一步:导入jar包
1. 首先通过Maven导入Spring和SpringMVC的相关jar包
<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>xin.sunzy</groupId>
<artifactId>ssms</artifactId>
<packaging>war</packaging>
<version>v1.0.0</version>
<name>ssms Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.3.10.RELEASE</spring.version>
</properties>
<dependencies>
<!--Spring Start-->
<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>
<!--添加 web支持-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!--Spring mvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--事务支持-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!--Spring End-->
<!--添加JSP和servlet支持-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>LATEST</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>LATEST</version>
<scope>provided</scope>
</dependency>
<!--测试包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>ssms</finalName>
</build>
</project>
2. 配置web.xml文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-context.xml</param-value>
</context-param>
<!--字符编码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--应用启动监听器,spring的入口-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--springMVC的入口-->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3. 编写spring-context.xml和spring-mvc.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"
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-4.3.xsd">
<!--不扫描Controller和ControllerAdvice注解,去除和springmvc中重叠的部分-->
<context:component-scan base-package="xin.sunzy.ssms">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
</beans>
<?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-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--只扫描Controller和ControllerAdvice注解,去除和spring中重叠的部分-->
<context:component-scan base-package="xin.sunzy.ssms.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:resources mapping="/res/" location="/res/**"></mvc:resources>
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
第二步:编写一个Controller类和配置tomcat服务器
1. 编写Controller类
在xin.sunzy.ssms.controller包中编写MyController.java
package xin.sunzy.ssms.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@ResponseBody
@RequestMapping("/test")
public String test(){
return "hello world";
}
}
2. 配置tomcat
配置如上,比较简单,配置好后点击启动,访问http://localhost/test 返回如下
到这里spring和springmvc就整合完成了
第三步:整合Mybatis
单独的使用mybatis,需要在mybatis-config.xml配置数据源、配置mapper文件的扫描路径等,在spring中集成mybatis就是把这些配置文件集成到spring的配置文件中去。
1. 导入mybatis的jar包
<!--数据源和数据库配驱动置 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- 日志文件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<!-- 引入Jackson ,JSON工具包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.9</version>
</dependency>
2. 新建一个spring-mybatis.xml
这个文件就是用来完成spring和mybatis的整合的。这里面也没多少行配置,主要的就是自动扫描,自动注入,配置数据库。注释也很详细,大家看看就明白了。
spring-mybatis.xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 引入数据源配置文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config/druid.properties" />
</bean>
<!--Druid DataSource-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${spring.datasource.url}"/>
<property name="driverClassName" value="${spring.datasource.driver-class-name}"/>
<property name="username" value="${spring.datasource.username}"/>
<property name="password" value="${spring.datasource.password}"/>
<property name="initialSize" value="${spring.datasource.initialSize}"/>
<property name="minIdle" value="${spring.datasource.minIdle}"/>
<property name="maxActive" value="${spring.datasource.maxActive}"/>
<property name="maxWait" value="${spring.datasource.maxWait}"/>
<property name="timeBetweenEvictionRunsMillis" value="${spring.datasource.timeBetweenEvictionRunsMillis}"/>
<property name="minEvictableIdleTimeMillis" value="${spring.datasource.minEvictableIdleTimeMillis}"/>
<property name="validationQuery" value="${spring.datasource.validationQuery}"/>
<property name="testWhileIdle" value="${spring.datasource.testWhileIdle}"/>
<property name="testOnBorrow" value="${spring.datasource.testOnBorrow}"/>
<property name="testOnReturn" value="${spring.datasource.testOnReturn}"/>
<property name="poolPreparedStatements" value="${spring.datasource.poolPreparedStatements}"/>
<property name="maxPoolPreparedStatementPerConnectionSize" value="${spring.datasource.maxPoolPreparedStatementPerConnectionSize}"/>
<property name="connectionProperties" value="${spring.datasource.connectionProperties}"/>
<property name="useGlobalDataSourceStat" value="${spring.datasource.useGlobalDataSourceStat}"/>
</bean>
<bean class="org.mybatis.spring.SqlSessionTemplate" id="sqlSession">
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
</bean>
<!-- spring和MyBatis完美整合,可以不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
<!--引入mybatis配置文件,也可以写在spring中,但是会比较臃肿,我更喜欢前者-->
<property name="configLocation" value="classpath:config/mybatis-config.xml"></property>
</bean>
<!-- 自动扫描com/hys/app/**/dao下的所有dao接口,并实现这些接口,可直接在程序中使用dao接口,不用再获取sqlsession对象,但是我还是喜欢使用SqlSessionTemplate,根据自己的个人爱好吧 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- basePackage 属性是映射器接口文件的包路径。 你可以使用分号或逗号 作为分隔符设置多于一个的包路径 -->
<property name="basePackage" value="xin.sunzy.ssms.dao"/>
<!--
因为会自动装配 SqlSessionFactory和SqlSessionTemplate
所以没 有 必 要 去 指 定 SqlSessionFactory或 SqlSessionTemplate
因此可省略不配置;
但是,如果你使 用了一个 以上的 DataSource,那么自动装配可能会失效。
这种 情况下,你可以使用sqlSessionFactoryBeanName或sqlSessionTemplateBeanName属性来设置正确的 bean名称来使用;
-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
</beans>
数据源的配置文件druid.properties
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/ssms?useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,wall,log4j
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
spring.datasource.useGlobalDataSourceStat=true
还有别忘了在spring-context.xml文件中引入spring-mybatis.xml文件,否则mybatis整合不进去,怎么引入呢,看图
3. 新建mybatis-config.xml文件
这一步可有可无,这些都可以在spring-mybatis.xml文件中配置完成,为了查找方便,我还是单独拎出来了,个人喜欢模块化的配置。
<?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="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="true"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25000"/>
</settings>
<typeAliases>
<package name="xin.sunzy.ssms.entity.po"/>
<package name="xin.sunzy.ssms.entity.bo"/>
</typeAliases>
</configuration>
4. 新建Dao接口和实现类
首先需要在数据库中建一张表,我建了一张很简单的表valid,仅做测试使用
然后新建接口IValidDao和实现类ValidDao,这个表太简单了,这里就直接使用注解了,关于使用mapper.xml的使用,在下面的整合shiro中还会用到,对于简单的sql使用注解会非常省时,对于复杂的sql语句使用xml文件会比较方便。
package xin.sunzy.ssms.dao.intf;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import xin.sunzy.ssms.entity.po.Valid;
@Mapper
@Repository
public interface IValidDao {
/**
* 由主键获取对象
* @return
* @throws Exception
*/
@Select("select * from valid where id = #{id}")
Valid getValidByPrimaryKey(@Param(value = "id") Integer id) throws Exception;
@Select("select * from valid where id = #{id}")
Valid getValidById(int id) throws Exception;
}
再接着就是新建Service层了,接口和实现类
package xin.sunzy.ssms.service.intf;
import xin.sunzy.ssms.entity.po.Valid;
public interface IValidService {
Valid getValidByPrimaryKey(Integer id) throws Exception;
Valid getValidById(Integer id) throws Exception;
}
package xin.sunzy.ssms.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xin.sunzy.ssms.dao.intf.IValidDao;
import xin.sunzy.ssms.entity.po.Valid;
import xin.sunzy.ssms.service.intf.IValidService;
@Service("validService")
public class ValidService implements IValidService {
@Autowired
private IValidDao validDao;
@Override
public Valid getValidByPrimaryKey(Integer id) throws Exception {
return validDao.getValidByPrimaryKey(id);
}
@Override
public Valid getValidById(Integer id) throws Exception {
return validDao.getValidById(id);
}
}
接着就是Controller层,新建一个MyController类
package xin.sunzy.ssms.controller;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import xin.sunzy.ssms.entity.po.Valid;
import xin.sunzy.ssms.service.intf.IValidService;
@Controller
public class MyController {
@Autowired
private IValidService validService;
@ResponseBody
@RequestMapping("/test")
public Object test(){
return "<h2>hello world</h2>";
}
@ResponseBody
@RequestMapping("/valid")
public Object valid(){
try {
return validService.getValidById(1);
} catch (Exception e) {
return new Valid("404","失败");
}
}
}
好的这时候,启动你的tomcat,在浏览器中访问http://127.0.0.1/valid ,返回如下的字符串,就是整合完成了,如果返回{"code":"404","content":"失败"},那就没有部署成功,如果遇到了bug,那么就慢慢的排除吧,程序员就是一步一步爬坑的过程。
源码在下篇(Spring + SpringMVC + Mybatis + Shiro 整合(下)):
https://my.oschina.net/GinkGo/blog/1524467