mybatis初体验
1.创建一个Web项目或者创建一个 控制台 应用程序 做一个简单的增删改查操作
首先看项目结构图
mybatis的配置文件configuration.xml此文件作为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>
<!-- 加载数据源配置文件 -->
<properties resource="jdbc.properties"></properties>
<!-- 设置类型别名 -->
<typeAliases>
<typeAlias alias="BookInfo" type="com.dragon.entity.BookInfo"/>
</typeAliases>
<!-- 配置开发环境 -->
<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="com/dragon/entity/BookInfo.xml"/>
</mappers>
</configuration>
jdbc.properties配置文件
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
username=system
password=orcl
book实体类
package com.dragon.entity;
import java.io.Serializable;
public class BookInfo implements Serializable{
public BookInfo() {
super();
}
public BookInfo(int bookId, String bookTitle, Double bookPrice) {
super();
book_id = bookId;
book_title = bookTitle;
book_price = bookPrice;
}
/**
*
*/
private static final long serialVersionUID = 1L;
private int book_id;
private String book_title;
private Double book_price;
public int getBook_id() {
return book_id;
}
public void setBook_id(int bookId) {
book_id = bookId;
}
public String getBook_title() {
return book_title;
}
public void setBook_title(String bookTitle) {
book_title = bookTitle;
}
public Double getBook_price() {
return book_price;
}
public void setBook_price(Double bookPrice) {
book_price = bookPrice;
}
}
bookMapper配置文件 BookInfo.xml
<?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="com.dragon.entity.BookInfo" > <!-- 创建insert parameterType 参数为设置传入类型 insert没有返回类型--> <insert id="inserts" parameterType="com.dragon.entity.BookInfo" > insert into BookInfo(book_id,book_title,book_price) values (#{book_id},#{book_title},#{book_price}) </insert> <!-- 创建查询语句 parameterType 为设置传入参数 --> <select id="get" parameterType="com.dragon.entity.BookInfo" resultType="BookInfo"> select * from BookInfo </select> <!-- 根据编号查询 --> <select id="selById" parameterType="int" resultType="BookInfo"> SELECT * FROM BookInfo WHERE book_id = #{book_id} </select> <!-- 根据编号删除 --> <delete id="deleteBook" parameterType="int"> DELETE FROM BookInfo WHERE book_id = #{book_id} </delete> <!-- 更新 --> <update id="updateBook" parameterType="com.dragon.entity.BookInfo"> UPDATE BookInfo SET book_title = #{book_title}, book_price = #{book_price} WHERE book_id = #{book_id} </update> </mapper>
控制台测试
package com.dragon.test;
import java.io.IOException; import java.io.InputStream; import java.util.List;
import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.dragon.entity.BookInfo;
public class Test {
/** * @param args */ @SuppressWarnings("unchecked") public static void main(String[] args) { // TODO Auto-generated method stub //定义文件 文件名称为mybatis的配置文件的名称 String resource = "configuration.xml"; //创建文件输入流 InputStream stream; try {
stream = Resources.getResourceAsStream(resource); //创建sqlSessionFactory对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream); //打开session对象 SqlSession session = sqlSessionFactory.openSession();
/////////////////查全部////////////////// //调用mapper文件中的id为get的sql方法 List<BookInfo> list = session.selectList("com.dragon.entity.BookInfo.get"); System.out.println("图书的信息是:\n编号\t名称\t价格"); for (BookInfo bookInfo : list) { System.out.println(bookInfo.getBook_id()+"\t"+bookInfo.getBook_title()+"\t"+bookInfo.getBook_price()); } System.out.println("---------------------------------------------------------------------"); /////////////根据编号查询 BookInfo bookInfo = (BookInfo)session.selectOne("com.dragon.entity.BookInfo.selById", 1); System.out.println("图书的信息是:\n编号\t名称\t价格"); System.out.println(bookInfo.getBook_id()+"\t"+bookInfo.getBook_title()+"\t"+bookInfo.getBook_price());
/////////////新增 int result =session.insert("com.dragon.entity.BookInfo.inserts", new BookInfo(4,"net培训教程3",78.53)); //提交事务 session.commit(); ///////插入成功则返回1 System.out.println(result); ////////删除 int delResult = session.delete("com.dragon.entity.BookInfo.deleteBook", 3); session.commit(); ////删除成功返回1 System.out.println(delResult); //////更新 int updateResult = session.update("com.dragon.entity.BookInfo.updateBook", new BookInfo(1,"net培训教程",78.53) ); session.commit(); System.out.println(updateResult); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
}
一个简单的jdbc对mybatis的操作 希望能够对您有所帮助!
<wbr></wbr>
数据库
CREATE TABLE BookInfo (
book_id number(5) PRIMARY key not null,
book_title nvarchar2(50) ,
book_price number(6,2)
);
insert into BookInfo values (1,'java',54.8);
insert into BookInfo values(2,'C#编程',66);
commit;
select * from BookInfo;
DROP TABLE BookInfo;