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都粘合到一起,使我们处理起来更方便。
上一篇: 浅谈python中正则的常用方法
下一篇: 学习python路途中....