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都粘合到一起,使我們處理起來更方便。