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所需要的依赖包,以及一 ...
随机推荐
- windows下jenkins的安装与配置
项目自动化部署: https://www.cnblogs.com/wuxunyan/p/9592953.html https://blog.csdn.net/qq_37372007/article/d ...
- 为什么vue组件的属性,有的需要加冒号“:”,有的不用?
https://segmentfault.com/q/1010000010929963/a-1020000010930077 <tab :line-width="2" act ...
- 使用cmi工具连接服务器远程装机exsi
使用cmi工具连接服务器远程装机exsi 网宿机房有两台服务器磁盘坏掉了,后面换了磁盘需要重新初始化系统 访问:http://192.168.48.133/cgi/url_redirect.cgi?u ...
- consul_nginx_uprsync动态负载均衡
consul_nginx_uprsync动态负载均衡 环境准备: 原理描述: 将Nginx的负载均衡后端服务器配置信息写入consul的接口中,upsync插件通过读取consul的配置,然后持久化到 ...
- 2018 遇到selenium.common.exceptions.WebDriverException问题
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PAT ...
- Python3入门(十三)——常用内置模块之集合模块collections
1.namedtuple 主要用来定义一种数据类型:它具有Tuple的不变性,而且又能通过属性来访问 例如定义坐标: from collections import namedtuple Point ...
- Qt编写气体安全管理系统9-数据查询
一.前言 数据查询模块在整个系统中难度最低,由于Qt对数据库操作的封装堪称完美,所以各种查询都是手到擒来,不费吹灰之力.Qt中内置了sqlite数据库,你可以在数据库插件目录sqldrivers发现q ...
- Linux记录-批量更改当前目录的文件后缀名
#!/bin/bash path=. for file in $(ls $path) do if [ -f $file ] then filename=${file%.*} bak=${file#*. ...
- Flink 中定时加载外部数据
社区中有好几个同学问过这样的场景: flink 任务中,source 进来的数据,需要连接数据库里面的字段,再做后面的处理 这里假设一个 ETL 的场景,输入数据包含两个字段 “type, useri ...
- 使用JSQLParser解析SQL中涉及到的表
首先添加Maven依赖: <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId& ...