剛剛開始學,mybatis,怕自己忘記基礎,趕緊記錄了下來,歡迎各位大神指錯!
mybatis所需的jar包:mybatis-3.4.2.jar
mybatis與spring整合中需要的jar包:mybatis-spring-1.3.1.jar
其他jar包就找自己的需求來添加:比如oracle的驅動包,spring需要的相應的包
注:雖然配置文件里寫了:增刪改查,但是測試只測試查詢所有,其他的自己進行測試了哈
大概思路:
首先:創建數據,建表,插入測試數據
再而:創建與表相對應的實體類
下一步:在接口中定義一些數據訪問的方法
進一步:為MyBatis ORM創建的xml映射文件定義實現數據訪問需要的sql腳本
最后:使用spring.xml完成整合,進行小測試
建表,插數據
CREATE TABLE books(bid NUMBER PRIMARY KEY ,bname VARCHAR2(50) NOT NULL ,bprice NUMBER NOT NULL ); INSERT INTO books(bid,bname,bprice) VALUES (1,'百鬼夜行',12.5); INSERT INTO books(bid,bname,bprice) VALUES (2,'陌上花開',22.5); INSERT INTO books(bid,bname,bprice) VALUES (3,'夏目友人帳',32.5);
創建實體類

package edu.nf.mybatis.entity; /** * Created by Administrator on 2017/3/27. */ public class Books { private long bid; private String bname; private double bprice; public Books() { } public Books(long bid, String bname, double bprice) { this.bid = bid; this.bname = bname; this.bprice = bprice; } public long getBid() { return bid; } public void setBid(long bid) { this.bid = bid; } public String getBname() { return bname; } public void setBname(String bname) { this.bname = bname; } public double getBprice() { return bprice; } public void setBprice(double bprice) { this.bprice = bprice; } @Override public String toString() { return "Books{" + "bid=" + bid + ", bname='" + bname + '\'' + ", bprice=" + bprice + '}'; } }
定義訪問接口

package edu.nf.mybatis.mapper; import edu.nf.mybatis.entity.Books; import org.apache.ibatis.annotations.Param; import java.util.List; /** * Created by Administrator on 2017/3/27. * 圖書數據訪問的接口 */ public interface IBookDao { /*獲取所有書籍*/ public List<Books> getAll(); /*通過ID獲取單個數據*/ public Books getBookById(@Param("bid") int bid); /*添加數據*/ public int add(Books entity); /*根據ID刪除*/ public int del(int bid); /*更新*/ public int update(Books entity); }
創建xml映射文件定義實現數據訪問需要的sql腳本

<?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="edu.nf.mybatis.mapper.IBookDao"> <!--id應該是對應接口的包名+接口名--> <!--獲取所有書籍--> <!--因為在spring.xml文件中的sqlSessionFactory中配置屬性 <property name="typeAliasesPackage" value="edu.nf.mybatis.entity"></property>--> <!--所以這里resultType="Books"就只需要寫類名Books,而不是edu.nf.mybatis.entity.Books了--> <select id="getAll" resultType="Books"> select bid,bname,bprice from books </select> <!--通過編號獲取書籍--> <select id="getBookById" resultType="Books"> select bid,bname,bprice from books where bid = #{bid}; </select> <!--添加書籍--> <insert id="add"> insert into books(bid,bname,bprice) values (#{bid},#{bname},#{bprice}); </insert> <!--刪除書籍--> <delete id="del"> delete from books where bid = #{bid}; </delete> <!--更新書籍--> <update id="update"> update books set bid = #{bid},bname = #{bname},bprice = #{bprice} where bid = #{bid}; </update> </mapper>
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" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!--1 引入屬性文件,在配置中占位使用 --> <context:property-placeholder location="classpath:db.properties" /> <!--6 容器自動掃描IOC組件 --> <context:component-scan base-package="edu.nf.mybatis"/> <!--2 配置C3P0數據源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!--驅動類名 --> <property name="driverClass" value="${driver}" /> <!-- url --> <property name="jdbcUrl" value="${url}" /> <!-- 用戶名 --> <property name="user" value="${user}" /> <!-- 密碼 --> <property name="password" value="${password}" /> </bean> <!--3 會話工廠bean sqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 數據源 --> <property name="dataSource" ref="dataSource"></property> <!-- 配置這里后在bookMapper.xml文件中,sql語句返回的類型就不許寫全路徑,直接寫類名就好了--> <property name="typeAliasesPackage" value="edu.nf.mybatis.entity"></property> <!-- sql映射文件路徑 --> <property name="mapperLocations" value="classpath:mybatis/*.xml"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定要自動掃描接口的基礎包,實現接口 --> <property name="basePackage" value="edu.nf.mybatis.mapper" /> </bean> <!--5 聲明式事務管理 --> <!--定義事物管理器,由spring管理事務 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--支持注解驅動的事務管理,指定事務管理器 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>

import edu.nf.mybatis.entity.Books; import edu.nf.mybatis.sevice.BooksService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; /** * Created by Administrator on 2017/3/27. */ public class testMybatis { @Test public void test2(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml"); BooksService booksService=ctx.getBean(BooksService.class); List<Books> books=booksService.getAll(); System.out.println(books); } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置路徑,默認是 "WEB-INF/applicationContext.xml" --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <servlet-mapping> <servlet-name>myBatis</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
test2測試運行結果: