JDBC:(Java database connectivity) 目的:將Java語言和資料庫解耦和,使得一套Java程式能對應不同的資料庫。 方法:sun公司制定了一套連接資料庫的介面(API)。這套API叫做JDBC,JDBC的介面的實現類由資料庫廠家負責編寫,打包成jar包進行發佈,這些ja ...
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都粘合到一起,使我們處理起來更方便。