ssm单项目整合
前言
spring、mybatis、springmvc的整合。
创建maven项目
main
├─java
│ └─com
│ └─alvin
│ ├─controller
│ ├─mapper
│ └─service
├─resources
│ ├─mybatis
│ └─spring
└─webapp
└─WEB-INF
添加依赖
代码
<!--配置SpringMVC的依赖,包括了Spring的相关依赖(IOC,AOP)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--配置SpringJdbc依赖,包括JdbcTemplate,和内置的数据源-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--配置SpringAOP的扩展支持,可以支持纯注解的AOP(@Aspect,五个通知的注解)-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<!-- 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<!--MySql数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!--MyBatis依赖包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--MyBatis和Spring的整合包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!--junit的测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--Spring的测试组件-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
<scope>test</scope>
</dependency>
<!--对JSP的支持-->
<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.0</version>
<scope>provided</scope>
</dependency>
<!--jsp的JSTL标准标签库的支持-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
配置文件
总览
tree ./src/main /F > treeAll.txt
touch jdbc.properties
touch mybatis/SqlMapConfig.xml
touch spring/applicationContext-dao.xml
touch spring/applicationContext-service.xml
touch spring/applicationContext-trans.xml
touch spring/springmvc.xml
main
├─java
│ └─com
│ └─alvin
│ ├─controller
│ ├─mapper
│ └─service
├─resources
│ │ jdbc.properties
│ │
│ ├─mybatis
│ │ SqlMapConfig.xml
│ │
│ └─spring
│ applicationContext-dao.xml
│ applicationContext-service.xml
│ applicationContext-trans.xml
│ springmvc.xml
│
└─webapp
│ index.jsp
│
└─WEB-INF
web.xml
jdbc配置
jdbc.properties
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://127.0.0.1:3306/db_test?characterEncoding=utf8
jdbc.username = root
jdbc.password = root
mappers.package = com.alvin.mapper
mybatis配置
SqlMapConfig.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>
</configuration>
dao层配置
applicationContext-dao.xml
- 加载jdbc文件
- 数据源
- SQLSessionFactory
- 包扫描
<?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.xsd">
<!--加载jdbc.properties配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 3. 配置数据源 -->
<!-- 数据源配置, 使用 Druid 数据库连接池 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 四大属性 -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 配置初始化连接个数 -->
<property name="initialSize" value="0" />
<!-- 配置最小空闲连接 -->
<property name="minIdle" value="0" />
<!-- 配置最大的活动连接 -->
<property name="maxActive" value="15" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 获取链接的时候,不校验是否可用,开启会有损性能
这里建议配置为TRUE,防止取到的连接不可用。-->
<property name="testOnBorrow" value="true" />
<!-- 归还链接到连接池的时候校验链接是否可用 -->
<property name="testOnReturn" value="false" />
<!-- 此项配置为true即可,不影响性能,并且保证安全性。
意义为:申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,
执行validationQuery检测连接是否有效 -->
<property name="testWhileIdle" value="true" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<!-- 链接使用超过时间限制是否回收 -->
<property name="removeAbandoned" value="true" />
<!-- 超过时间限制时间(单位秒),目前为5分钟,如果有业务处理时间超过5分钟,可以适当调整。 -->
<property name="removeAbandonedTimeout" value="300" />
<!-- #链接回收的时候控制台打印信息,测试环境可以加上true,线上环境false。会影响性能。 -->
<property name="logAbandoned" value="false" />
<!-- 验证连接有效与否的SQL,不同的数据配置不同 -->
<property name="validationQuery" value="select 1 " />
</bean>
<!--配置SqlSessionFactory,需要使用mybatis-spring整合包中的类-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源-->
<property name="dataSource" ref="dataSource"/>
<!--加载MyBatis的配置文件,SqlMapConfig.xml.现在为空文件-->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
</bean>
<!--配置Mapper扫描,需要使用mybatis-spring整合包中的类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--配置Mapper扫描的包-->
<property name="basePackage" value="com.alvin.mapper"/>
</bean>
</beans>
service层配置
applicationContext-service.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.xsd">
<!--扫描service的注解,让注解生效-->
<context:component-scan base-package="com.alvin.service"/>
</beans>
事务配置
applicationContext-trans.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: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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--这里报错,原因是IDEA在这个xml配置中没有找到dataSource-->
<!--不用管这个错误,Spring启动的时候,会把所有的配置全部加在,包括数据源的配置(在dao里)-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置在哪个方法上执行-->
<tx:advice id="tx" transaction-manager="transactionManager" >
<tx:attributes>
<!--所有以trans,save,update,delete开头的方法都会进行事务管理,
因为默认的配置就是要进行事务管理-->
<tx:method name="trans*"/>
<tx:method name="save*"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
<!--以下配置都不进行事务管理-->
<tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--配置在什么地方执行-->
<aop:config>
<aop:advisor advice-ref="tx"
pointcut="execution(* com.alvin.service.impl.*.*(..))"/>
</aop:config>
</beans>
controller配置
- 包扫描
- 注解驱动
- 视图解析器
springmvc.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: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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置Controller的包扫描-->
<context:component-scan base-package="com.alvin.controller"/>
<!--配置注解驱动-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml
项目首先加载的配置文件
- 加载所有配置文件
- 启动spring容器
- 解决post乱码
- 配置前端控制器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Archetype Created Web Application</display-name>
<!--配置Spring随着项目的启动创建,就是Spring和web项目的整合-->
<!--配置创建Spring容器需要加载的配置文件-->
<context-param>
<!--contextConfigLocation:是固定写法,是spring中的一个属性-->
<param-name>contextConfigLocation</param-name>
<!--因为Spring配置有很多,我们需要启动的时候全部加在,所以使用*-->
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<!--配置监听器,让Spring容器随着ServletContext的创建而创建-->
<!--(Tomcat启动,web应用就启动,Spring容器就创建了)-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置POST乱码解决的过滤器-->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置SpringMVC的前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载SpringMVC核心配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--配置所有的请求都进入SpringMVC-->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
使用
- 编写pojo
- 编写mapper接口
- 编写Mapper.xml映射文件
- 编写Service接口
- 编写Service实现
- 编写Controller
- 编写前端页面
ssm单项目整合的更多相关文章
- SSM 框架 ---项目整合
一.SSM框架理解 Spring(业务层) Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象. Spring的核心思想是IoC(控 ...
- IdentityServer4实战 - 与API单项目整合
一.前言 我们在实际使用 IdentityServer4 的时候,可能会在使用 IdentityServer4 项目添加一些API,比如 找回密码.用户注册.修改用户资料等,这些API与Identit ...
- IdentityServer4与API单项目整合(net core 3.X)
一.创建一个空的api项目 添加identityserver4的nuget包 配置config文件 public static IEnumerable<IdentityResource> ...
- SSM项目整合第一步 注册登陆实现
SSM项目整合第一步 注册: 项目目录: 一.数据库建表: 源码: ; -- ---------------------------- -- Table structure for t_user - ...
- SSM项目整合基本步骤
SSM项目整合 1.基本概念 1.1.Spring Spring 是一个开源框架, Spring 是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作 ...
- (转)shiro权限框架详解06-shiro与web项目整合(下)
http://blog.csdn.net/facekbook/article/details/54962975 shiro和web项目整合,实现类似真实项目的应用 web项目中认证 web项目中授权 ...
- (转) shiro权限框架详解06-shiro与web项目整合(上)
http://blog.csdn.net/facekbook/article/details/54947730 shiro和web项目整合,实现类似真实项目的应用 本文中使用的项目架构是springM ...
- Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码)
Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码) 备注: 之前在Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合中 ...
- 【JAVA - SSM】之SSM入门项目的搭建
最近学习了一下SSM.SSM是 Spring + SpringMVC + MyBatis 整合框架,非常适合WEB后台开发,也是当前很多人的不二选择.这篇博客带大家来创建一个学习SSM的入门程序,从搭 ...
随机推荐
- 可视化的Redis数据库管理工具redis-desktop-manager的初步使用(图文详解)
不多说,直接上干货! 无论是Linux 还是 Windows里安装Redis, Windows里如何正确安装Redis以服务运行(博主推荐)(图文详解) Windows下如何正确下载并安装可视化的Re ...
- slq 修改表结构
1.增加列: alter table tableName add columnName varchar(30) 2.修改列类型: alter table tableName alter column ...
- CSS学习笔记(基础篇)
CSS概念 CSS 指层叠样式表 (Cascading Style Sheets)(级联样式表) Css是用来美化html标签的,相当于页面化妆. 样式表书写位置: <head> < ...
- Numpy基础总结
一.文件读取 numpy.genfromtxt() 可以用来读取各种文件.常用语法大致如下: numpy.genfromtxt(fname, dtype=<type 'float'>, d ...
- java主线程结束和子线程结束之间的关系
(一)Main线程是个非守护线程,不能设置成守护线程. 这是因为,main线程是由java虚拟机在启动的时候创建的.main方法开始执行的时候,主线程已经创建好并在运行了.对于运行中的线程,调用Thr ...
- MySQL 报错MySQL server syntax to use near 'OPTION SQL_SELECT_LIMIT=DEFAULT'
在hive的应用中,出现如下错误时You have an error in your SQL syntax; check the manual that corresponds to your MyS ...
- Python 开发
1.GIL,CPython,Python跟编译器没关系,语言有多个编译器,如:JPython.IronPython等,其他语言如是.GIL对IO密集型友好,计算密集型惨淡 2.pass,定义空执行函数 ...
- Nodejs学习笔记(十六)—Pomelo介绍&入门
前言&介绍 Pomelo:一个快速.可扩展.Node.js分布式游戏服务器框架 从三四年前接触Node.js开始就接触到了Pomelo,从Pomelo最初的版本到现在,总的来说网易出品还算不错 ...
- RabbitMQ上手记录–part 3-发送消息
接上一part<<RabbitMQ上手记录–part 2 - 安装RabbitMQ>>,这里我们来看看如何通过代码实现对RabbitMQ的调用. RabbitMQ通常是安装在服 ...
- Golang panic用法
Go语言追求简洁优雅,所以,Go语言不支持传统的 try…catch…finally 这种异常,因为Go语言的设计者们认为,将异常与控制结构混在一起会很容易使得代码变得混乱.因为开发者很容易滥用异常, ...