SSM衍生的配置文件
JDBC:(Java database connectivity)
目的:将Java语言和数据库解耦和,使得一套Java程序能对应不同的数据库。
方法:sun公司制定了一套连接数据库的接口(API)。这套API叫做JDBC,JDBC的接口的实现类由数据库厂家负责编写,打包成jar包进行发布,这些jar包通常被称为“驱动”,
Jar包:mysql-connector-java-*.*.*-bin.jar
JDBC开发的六部曲:
1、 注册驱动:
DriverManager.registerDriver(new Driver())
或者Class.forName(“com.mysql.jdbc.Driver”)这种方式将com.mysql.jdbc.Driver类装载在JVM当中,装载过程中会自动执行静态代码块,完成驱动的注册。
2、 获取数据库的连接对象
Connection conn = DriverManager.getConnection(url,user,password)
3、 获取数据库的操作对象
Statement state = conn.createStatement()
为了防止数据库的注入攻击
PreparedStatement ps = conn.createPreparedStatement()
4、 执行SQL语句
ResultSet res = ps.executeQuery(sql)
Boolean bool = ps.execute(sql)
5、 处理查询的结果集
如果是查询结果集,这里采用迭代器的原理,进行遍历
While(res.next()){}
6、 释放资源
先关ResultSet,再关PreparedStatement 最后关Connection
res.close()
ps.close()
conn.close()
Mybatis:本是apache的一个开源项目iBatis
JDBC虽然解决了Java代码和数据库的解耦和问题,但是它过于繁琐,重复的代码太多,因此Mybatis实际上是对JDBC的数据库操作的封装,它使得开发者只需要关注SQL本身。
1、mybatis-config.xml :mybatis的核心配置文件,配置了Mybatis运行过程中所需要的全局性信息。
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
2、通过Mybatis核心配置文件mybatis-config.xml 构建SqlSessionFactory即会话工厂
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
3、由会话工厂获取SqlSession,操作数据库是通过SqlSession来完成的
SqlSession session = sqlSessionFactory.openSession();
try {
// do work
} finally {
session.close();
}
4、Mybatis底层自定义了Executor执行器接口操作数据库,Executor接口有两个实现,一个是基本执行器、一个是缓存执行器。
5、Mapped Statement也是Mybatis一个底层封装对象,它包装了Mybatis配置信息及sql映射信息等。Mapper.xml文件中一个sql对应一个Mapped Statement对象,sql的id即是Mapped statement的id。
<?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="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>
6、 Mapped Statement对sql执行输入参数进行定义,包括简单类型、HashMap和自定义pojo,Executor通过Mapped Statement在执行sql前将输入的java对象映射至sql中,输入参数映射就相当于jdbc编程中对PreparedStatement设置参数。
7、Mapped Statement对sql执行输出结果进行定义,包括简单类型、HashMap、pojo,Executor通过Mapped Statement在执行sql后将输出结果映射至java对象中,输出结果映射过程相当于jdbc编程中对结果的解析处理过程
Spring:是一个轻量级的框架,作为一个容器,可以管理对象的生命周期,对象与对象之间的依赖关系,可以通过配置文件来定义对象。
Spring整合mybatis:将数据库
<?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">
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 数据源DataSource -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${mysql.url}" />
<property name="username" value="${mysql.username}" />
<property name="password" value="${mysql.password}" />
</bean>
<!-- 声明SqlSessionFactoryBean,创建 SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 声明mybatis的扫描器对象,目的是使用动态代理创建Dao接口的实现类对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="com.syd.spring.dao"></property>
</bean>
<!-- 声明Service对象,注入Dao -->
<context:component-scan base-package="com.syd.spring.service" />
</beans>
Mybatis-config.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>
<typeAliases>
<package name="com.syd.spring.beans"/>
</typeAliases>
<mappers>
<package name="com.syd.spring.dao"/>
</mappers>
</configuration>
Springmvc:是一种mvc框架,属于spring,要有spring的jar包作为支撑。
<?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">
<!-- 修改视图解析器的注册 InternalResourceViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀视图 prefix -->
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
<!-- 后缀 suffix-->
</bean>
<!-- <bean id="/user.do" class="syd.springmvc.user.UserController" /> -->
<!-- 声明组件扫描器 -->
<context:component-scan base-package="syd.springmvc.user" />
<context:component-scan base-package="syd.springmvc.exceptions" />
<!-- 注册注解驱动 -->
<mvc:annotation-driven />
</beans>
web.xml文件的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>25-SSM</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 注册Spring的监听器ContextLoaderListener, 创建Spring的容器对象 -->
<!-- 指定自定义配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 注册中央调度器DispatcherServlet, 创建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:conf/dispatcherServlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 注册字符集过滤器,解决post请求乱码的问题 -->
<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>
<!-- 强制request使用encoding的值 -->
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<!-- 前置response使用encoding的值 -->
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
SSM框架:基于spring将springmvc和mybatis进行整合
web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>25-SSM</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 注册中央调度器DispatcherServlet, 创建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:conf/dispatcherServlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 注册字符集过滤器,解决post请求乱码的问题 -->
<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>
<!-- 强制request使用encoding的值 -->
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<!-- 强制response使用encoding的值 -->
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
中央调度器(dispatcher.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">
<!-- springmvc配置文件:定义视图层的对象:处理器对象, 视图对象 -->
<!-- 声明组件扫描器,创建处理器对象 -->
<context:component-scan base-package="com.bjpowernode.controllers" />
<!-- 声明视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 声明注解驱动 -->
<mvc:annotation-driven />
<!-- 处理静态资源 -->
<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<!-- 声明组件扫描器,指定@Service, 创建Service对象 -->
<context:component-scan base-package="com.bjpowernode.service" />
<!-- 加载属性配置文件 -->
<context:property-placeholder location="classpath:conf/jdbc.properties"/>
<!-- 声明数据源DataSource, 使用druid数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.passwd}" />
</bean>
<!-- 声明SqlSessionFactoryBean,创建SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:conf/mybatis.xml" />
</bean>
<!-- 声明MyBatis的扫描器,使用动态代理,创建Dao接口的实现类对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="com.bjpowernode.dao" />
</bean>
</beans>
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>
<!-- 指定别名 -->
<typeAliases>
<package name="com.bjpowernode.beans" />
</typeAliases>
<!-- 指定sql映射文件的位置 -->
<mappers>
<package name="com.bjpowernode.dao"/>
</mappers>
</configuration>
Mybatis是持久层框架,使得连接数据库非常的简单方便,springmvc是控制层框架,使得处理前端请求更便捷,而spring是粘合剂,将mybats和springmvc都粘合到一起,使我们处理起来更方便。
SSM衍生的配置文件的更多相关文章
- ssm整合各配置文件
ssm整合 1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns ...
- 整合ssm框架之配置文件
原文:https://blog.csdn.net/zwyanqing/article/details/53039591 ssm整合 一.applicationContext.xml 1.配置数据源 & ...
- ssm框架整合,配置文件中的配置内容
转自:https://www.cnblogs.com/dong-dong-1/p/8724127.html 使用idea工具开发,用maven进行管理. 最近在写毕业设计,因为对ssm框架一直半解,常 ...
- 如何配置-整合ssm框架之配置文件
ssm整合 一.applicationContext.xml 1.配置数据源 <bean id="dataSource" class="org.springfram ...
- SSM的各个配置文件
SqlMapConfig.xml文件:(这是带了mybatis的分页插件的配置) <?xml version="1.0" encoding="UTF-8" ...
- SSM 加载配置文件
配置文件中 <bean id="address" class="org.springframework.beans.factory.config.Propertie ...
- SSM整合的配置文件
一.spring-web.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...
- ssm框架整合配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- SSM项目spring配置文件详细步骤(分门别类、灵巧记忆)
spring-dao.xml文件 1.配置外部db.property文件: <context:property-placeholder location="classpath:jdbc ...
随机推荐
- 洛谷P5057 [CQOI2006]简单题(线段树)
题意 题目链接 Sol 紫色的线段树板子题??... #include<iostream> #include<cstdio> #include<cmath> usi ...
- bootstrap 中的 iCheck 全选反选功能的实现
喜欢bootstrap 风格的同学应该知道,iCheck的样式还是很好看的. 官网: http://www.bootcss.com/p/icheck/ 进入正题,iCheck提供了一些方法,可以进行全 ...
- 浅谈 Underscore.js 中 _.throttle 和 _.debounce 的差异[转]
看的文章来自: https://blog.coding.net/blog/the-difference-between-throttle-and-debounce-in-underscorejs 使用 ...
- Android 虚拟多开系列二——技术原理
目录 Android虚拟多开应用有哪些? Android虚拟多开应用技术原理有哪几类? Android虚拟多开需求分析 反虚拟多开技术 ...
- eclipse4.5(mars)环境
官网下载页面: http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/mars/2/ ...
- C#实现字符串相似度算法
字符串的相似性比较应用场合很多,像拼写纠错.文本去重.上下文相似性等. 评价字符串相似度最常见的办法就是: 把一个字符串通过插入.删除或替换这样的编辑操作,变成另外一个字符串,所需要的最少编辑次数,这 ...
- yum 安装LAMP
一.安装 MySQL 首先来进行 MySQL 的安装.打开超级终端,输入: [root@localhost ~]# yum install mysql mysql-server 安装完毕,让 MySQ ...
- 如何提高Ajax性能
1.适当使用缓存机制 2.使用CDN内容分发来访问Jquery脚本: (1)自己公司架设CDN服务器 (2)使用第三方公司的,比如微软,谷歌等公司的CDN,但有时候不太靠谱 3.JS/CSS文件的打包 ...
- [翻译] BFKit
BFKit BFKit is a collection of useful classes to develop Apps faster. BFKit是一个有用的工具集合,帮助你快速开发. Insta ...
- 使用Visual Studio Code开发Arduino
首发于MSPrecious成长荟 https://zhuanlan.zhihu.com/p/30868224 使用Visual Studio Code开发Arduino 1.下载安装 VS Code ...