MyBatis整合Spring+SpringMVC搭建一个web项目(SSM框架)
本文讲解如何搭建一个SSM架构的web站点
【工具】
IDEA、SqlYog、Maven
【简述】
该项目由3个模块组成:dao(数据访问层)、service(业务处理层)、web(表现层)
dao层:负责与数据库交互,包含entity(实体类)和dao(持久化方法类)包
service层:负责业务处理和事务管理,将表现层输入输出的数据进行处理后持久化保存、返回表现层所需要的各种数据
web层:网站项目,负责与用户交互
【开始搭建】
一、dao层
依赖:mysql-connector-java、mybatis、mybatis-spring、spring-jdbc、spring-core、spring-context、spring-aop
说明:该层需要配置与数据库交互,我们用的是mysql数据库,所以依赖mysql连接库、mybatis库;我们要和spring整合,所以依赖mybatis-spring库、spring-jdbc为;另外我们需要使用spring来管理实例,所以需要依赖spring套餐(core、context、aop)
spring配置文件:
spring-dao.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
<!--<property name="driverClassName" value="com.mysql.cj.jdbc.MysqlDataSource"></property>-->
<!--<property name="url" value="jdbc:mysql://localhost:3306/mycode_statistics?characterEncoding=UTF-8&serverTimezone=GMT"></property>-->
<!--<property name="username" value="root"></property>-->
<!--<property name="password" value="root"></property>-->
<!--</bean>-->
<!--上面和下面这两种配置数据源的方式都可以,效果是一样的-->
<bean id="dataSource" class="com.mysql.cj.jdbc.MysqlDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/mycode_statistics?characterEncoding=UTF-8&serverTimezone=GMT"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="cc.yzeng.entity"></property>
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
<property name="basePackage" value="cc.yzeng.dao"></property>
</bean>
</beans>
该文件配置了3个对象:
1:dataSource,数据源对象,用于连接数据库,使用 org.springframework.jdbc.datasource.DriverManagerDataSource 或 com.mysql.cj.jdbc.MysqlDataSource 都可以,效果一样。
2:sqlSessionFactoryBean,数据库连接对象,用于打开与数据库的连接,获得会话,这是mybatis-spring里封装的对象 org.mybatis.spring.SqlSessionFactoryBean ,封装自mybatis的SqlSessionFactory,专门用于ioc容器管理的sqlSessionFactory对象
属性dataSource,引用数据源对象
属性typeAliasesPackage,用于给配置文件里的type指定别名,通常写实体类包名,这样我们在写映射文件的时候经常用到实体类,就可以不用写包名了
3:MapperScannerConfigurer,映射文件自动配置,用于将接口与配置文件自动关联并生成代理类,这样就可以通过接口->配置文件 来访问数据库了,而不用写接口实现类
属性sqlSessionFactoryBeanName,引用sqlSessionFactoryBean对象
属性basePackage,指接口所在的包名,且配置文件也要放在与包名相同的目录下
xml映射文件:
示例,路径:resources/cc.yzeng.dao/UserDao.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cc.yzeng.dao.UserDao">
<insert id="addUser" parameterType="User">
insert into user values(#{userId},#{username},#{password},#{addTime},#{status})
</insert>
<select id="findUserByUserName" resultType="User">
select * from user where username = #{username}
</select>
<select id="findAll" resultType="User">
select * from user
</select>
<update id="updateUser" parameterType="User">
update user
<set>
<if test="username != null">
username = #{username},
</if>
<if test="status != null">
status = #{status},
</if>
</set>
where userId = #{userId}
</update>
</mapper>
以上是一个xml映射文件的示例,一个实体类对应一个映射文件,映射文件的路径与接口的包名一致(而不是实体类的包名)、文件名与接口类名一致、id与方法名一致(insert、select、update标签)
二、service层
依赖:spring-jdbc、spring-core、spring-context、spring-aop、spring-tx、spring-aspects、dao层
spring配置文件:
spring-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"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:apo="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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<import resource="spring-dao.xml"></import>
<context:component-scan base-package="cc.yzeng.service"></context:component-scan> <!--事务管理-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <tx:advice transaction-manager="transactionManager" id="transactionInterceptor">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"></tx:method>
<tx:method name="create*" propagation="REQUIRED"></tx:method>
<tx:method name="update*" propagation="REQUIRED"></tx:method>
<tx:method name="*" read-only="true"></tx:method>
</tx:attributes>
</tx:advice> <apo:config>
<apo:pointcut id="aa" expression="execution(public * cc.yzeng.service.impl.*.*(..))"></apo:pointcut>
<apo:advisor advice-ref="transactionInterceptor" pointcut-ref="aa"></apo:advisor>
</apo:config> <apo:aspectj-autoproxy></apo:aspectj-autoproxy>
</beans>
该配置文件由:导入dao配置文件、自动扫描组件、事务管理三块组成,由于不同层都有一个spring配置文件,所以要按照依赖关系分别导入这些文件
三、web层
依赖:spring-webmvc
说明:依赖spring-webmvc这个库会自动引用其它spring框架需要的库
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>
<!--所有请求指向springMVC的入口-->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--配置spring配置文件所在路径,默认为WEB-INF目录-->
<param-name>namespace</param-name>
<param-value>spring.xml</param-value>
</init-param>
</servlet> <servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!--content目录不走springMVC处理-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/content</url-pattern>
</servlet-mapping>
</web-app>
spring.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"> <!--自动扫描bean-->
<context:component-scan base-package="cc.yzeng.controller"></context:component-scan>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
配置自动扫描指向controller目录,就可以使用注解方式配置路由了。
controller类(示例):
package cc.yzeng.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class IndexController {
@RequestMapping("home")
@ResponseBody
public String Home(){
return "hello,world!";
}
}
MyBatis整合Spring+SpringMVC搭建一个web项目(SSM框架)的更多相关文章
- ssm整合总结(一)--第一步之使用maven搭建一个web项目
本文内容来自:山硅谷,本文内容整合了任务2,任务3,任务4内容.http://www.gulixueyuan.com/my/course/50 1说明 1.1该项目使用的知识点有 1.1.1校验方式是 ...
- 使用Maven+ssm框架搭建一个web项目
1,前期准备:Eclipse(Mars.2 Release (4.5.2)).jdk1.7.tomcat7.maven3.2.1 2.使用eclipse中的maven新建一个web项目 点击next: ...
- Spring Boot(一):如何使用Spring Boot搭建一个Web应用
Spring Boot Spring Boot 是Spring团队旗下的一款Web 应用框架 其优势可以更快速的搭建一个Web应用 从根本上上来讲 Spring Boot并不是什么新的框架技术 而是在 ...
- 1.SpringBoo之Helloword 快速搭建一个web项目
背景: Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配 ...
- 1.SpringBoot之Helloword 快速搭建一个web项目
背景: Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配 ...
- 使用maven整合spring+springmvc+mybatis
使用maven整合spring+springmvc+mybatis 开发环境: 1. jdk1.8 2. eclipse4.7.0 (Oxygen) 3. mysql 5.7 在pom.xml文件中, ...
- 如何搭建一个WEB服务器项目(二)—— 对数据库表进行基本的增删改查操作
使用HibernateTemplate进行增删改查操作 观前提示:本系列文章有关服务器以及后端程序这些概念,我写的全是自己的理解,并不一定正确,希望不要误人子弟.欢迎各位大佬来评论区提出问题或者是指出 ...
- Eclipse的maven构建一个web项目,以构建SpringMVC项目为例
http://www.cnblogs.com/javaTest/archive/2012/04/28/2589574.html springmvc demo实例教程源代码下载:http://zuida ...
- Spring搭建MVC WEB项目[转]
原文链接:http://blog.csdn.net/initphp/article/details/8208349 1.创建一个web项目 2.假设,我们已经安装完毕Spring所需要的依赖包,以及一 ...
随机推荐
- ConsoleWebsocketServer服务端和ConsoleWebsocketClient客户端
本文是简述了Websocket的服务端和客户端的实时通讯过程,Websocket的服务端和客户端的具体使用使用了2种Websocket的服务端和2种客户端. 以下代码使用的是Visual Studio ...
- GB28181技术基础之2 - H264与PS封包
二. PS封包 PS 是 GB28181 规定的标准封包格式(也是存储格式),在讲 PS 之前,先介绍几种相关的 数据格式概念: 1)ES 基本流 (Elementary Streams)是直接从编码 ...
- git只提交修改部分的代码
思路: 先用git status 查找出哪些文件被修改过了,然后 只git commit odin/code/pom.xml 1. $ git status (查看当前更改的代码) On branch ...
- 方法不会等待Task执行完才返回数据
- 服务器端实时推送技术之SseEmitter的用法
这是SpringMVC提供的一种技术,可以实现服务端向客户端实时推送数据.用法非常简单,只需要在Controller提供一个接口,创建并返回SseEmitter对象,发送数据可以在另一个接口调用其se ...
- windows下安装node【转】
windows下安装node报错2503.2502 windows下命令行安装,首先使用管理员权限获取cmd msiexec /package node-v10.16.0-x64.msi 根据提示一步 ...
- 改进初学者的PID-介绍
最近看到了Brett Beauregard发表的有关PID的系列文章,感觉对于理解PID算法很有帮助,于是将系列文章翻译过来!在自我提高的过程中,也希望对同道中人有所帮助.作者Brett Beaure ...
- 组件文档系统-md-react-styleguidist
推荐指数:
- Cas(09)——通过Proxy访问其它Cas应用
通过Proxy访问其它Cas应用 目录 1.1 原理 1.2 配置 1.2.1 代理端 1.2.2 被代理端 1.3 请求示例 考虑这样一种场景:有两个应用App1 ...
- LeetCode:三数之和【15】
LeetCode:三数之和[15] 题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的 ...