MyBatis学习总结
1.引入jar包到lib目录下:只需要mybatis的一个mybatis.jar及数据库的jar包。

2。在src下新建xml配置文件,即上图中的conf.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>
<typeAlias type="com.hanqi.News" alias="News"/> </typeAliases> <!-- 环境配置 -->
<environments default="test">
<!-- 开发环境 -->
<environment id="development">
<!-- 事务管理器 -->
<transactionManager type="JDBC" />
<!-- 数据源 POOLED池连接;NOPOOLED 非池连接-->
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="test" />
<property name="password" value="test" />
</dataSource>
</environment>
<!-- 测试环境 -->
<environment id="test">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="test" />
<property name="password" value="test" />
</dataSource>
</environment>
</environments> <mappers>
<mapper resource="com/hanqi/newsMapper.xml"/> <mapper class="com.hanqi.newsInterface"/>
</mappers>
</configuration>
3。新建数据库表NEWS,和实体类News.java

package com.hanqi;
import java.util.Date;
public class News {
private Integer id;
private String title;
private String contant;
private Date createdate;
private String author;
public News(Integer id, String title, String contant, String author) {
super();
this.id = id;
this.title = title;
this.contant = contant;
this.author = author;
}
public News(String title, String contant, String author) {
super();
this.title = title;
this.contant = contant;
this.author = author;
}
public News(Integer id, String title) {
super();
this.id = id;
this.title = title;
}
public News() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContant() {
return contant;
}
public void setContant(String contant) {
this.contant = contant;
}
/**
* @return the createdate
*/
public Date getCreatedate() {
return createdate;
}
/**
* @param createdate the createdate to set
*/
public void setCreatedate(Date createdate) {
this.createdate = createdate;
}
/**
* @return the author
*/
public String getAuthor() {
return author;
}
/**
* @param author the author to set
*/
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", contant=" + contant + ", createdate=" + createdate + "]";
}
}
4.新建映射配置文件:newsMapper.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.hanqi.newsMapper">
<!--
根据id查询得到一个News对象
-->
<select id="getNewsByID" parameterType="int" resultType="News">
select * from news where id=#{id}
</select>
<!-- 定义结果集 -->
<resultMap type="News" id="newsList">
<!-- <id property="id" column="news_id"/>
<result property="title" column="tit"/>
-->
</resultMap> <!-- 多数据查询 -->
<select id="getAllList" resultMap="newsList">
select * from news </select> <!-- 定义条件集 -->
<parameterMap type="java.util.HashMap" id="titlelike">
<parameter property="tit"/>
<parameter property="aut"/> </parameterMap> <!-- 传多个条件多数据查询 -->
<select id="getList" parameterMap="titlelike" resultMap="newsList">
select * from news where title like '%'||#{tit}||'%' and author like '%'||#{aut}||'%' </select>
<insert id="insertNews" parameterType="News">
insert into news (id, title, contant, createdate, author) values ( HIBERNATE_SEQUENCE.nextval, #{title}, #{contant}, sysdate, #{author}) </insert>
</mapper>
5.新建测试用例,进行测试
package com.hanqi; import static org.junit.Assert.*; import java.io.IOException;
import java.io.Reader;
import java.util.*; 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 org.junit.Test; public class TestMyBatis { @Test
public void test() throws Exception {
//1.加载配置文件到输入流里
Reader reader = Resources.getResourceAsReader("conf.xml"); //2.创建工厂类SqlSessionFactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader); //3.获取sqlSession
SqlSession ss = ssf.openSession(); //4.调用数据库操作
//单数据查询
News n = ss.selectOne("com.hanqi.newsMapper.getNewsByID",77); //输出结果
System.out.println("n=" + n); //插入数据
News n1 = new News("ddd","eeee","aaa"); int i = ss.insert("com.hanqi.newsMapper.insertNews", n1); System.out.println("insert="+ i); //查询数据
List<News> ln = ss.selectList("com.hanqi.newsMapper.getAllList"); System.out.println("ln=......"+ln); //条件查询
HashMap<String, Object> hm = new HashMap<String, Object>(); hm.put("tit", "d");
hm.put("aut", "a"); List<News> ln1 = ss.selectList("com.hanqi.newsMapper.getList",hm); System.out.println("ln1=#######"+ln1); //测试注解update
News n2 = new News(95,"测试MyBatis","测试","测试"); int i1 = ss.update("com.hanqi.newsInterface.updateNews", n2); System.out.println("n3="+i1); //注解查询
List<News> ln2 = ss.selectList("com.hanqi.newsInterface.selectList"); System.out.println("ln2=@@@@@@@@"+ln2); //提交
ss.commit();
//关闭session
ss.close();
//关闭流
reader.close(); } }
6.也可以用注解代替映射配置文件,但是需要新建一个实体类对应的接口,在接口方法上加注解
package com.hanqi;
import java.util.List;
import org.apache.ibatis.annotations.*;
public interface newsInterface {
@Update("update news set title=#{title}, contant=#{contant}, author=#{author} where id =#{id}")
public int updateNews(News news);
@Select("select * from news")
public List<News> selectList();
}
MyBatis学习总结的更多相关文章
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作(转载)
本文转载自:http://www.cnblogs.com/jpf-java/p/6013540.html 上一篇博文MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybati ...
- MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合(转载)
孤傲苍狼 只为成功找方法,不为失败找借口! MyBatis学习总结(八)--Mybatis3.x与Spring4.x整合 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: m ...
- MyBatis学习总结(七)——Mybatis缓存(转载)
孤傲苍狼 只为成功找方法,不为失败找借口! MyBatis学习总结(七)--Mybatis缓存 一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的 ...
- (原创)mybatis学习二,spring和mybatis的融合
mybatis学习一夯实基础 上文介绍了mybatis的相关知识,这一节主要来介绍mybaits和spring的融合 一,环境搭建 1,jar包下载,下载路径为jar包 2,将包导入到java工程中 ...
- (原创)mybatis学习一,夯实基础
一,what?(是什么) MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可 ...
- MyBatis学习--简单的增删改查
jdbc程序 在学习MyBatis的时候先简单了解下JDBC编程的方式,我们以一个简单的查询为例,使用JDBC编程,如下: Public static void main(String[] args) ...
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对u ...
- 【Todo】Mybatis学习-偏理论
之前写过好几篇Mybatis相关的文章: http://www.cnblogs.com/charlesblc/p/5906431.html <SSM(SpringMVC+Spring+Myba ...
- MyBatis学习系列三——结合Spring
目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring MyBatis在项目中应用一般都要结合Spring,这一章主要把MyBat ...
- MyBatis学习系列二——增删改查
目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring 数据库的经典操作:增删改查. 在这一章我们主要说明一下简单的查询和增删改, ...
随机推荐
- FFT,NTT 专题
学习傅里叶的基本性质及其代码,可以参考大神理解 还有 ACdream 的博客 贴一下NTT的模板: using namespace std; typedef long long ll; int n; ...
- 记录mysql的具体操作明细
在MySQL中使用init-connect与binlog来实现用户操作追踪记录 2014-07-28 20:55:38 分类: MySQL 前言:测试环境莫名其妙有几条重要数据被删除了,由于在binl ...
- MYSQL数据库的操作
Mysql的连接方式: 1.原生函数:mysql_connect($server,$username,$password); //打开一个到Mysql服务器的连接 mysql_select_db( ...
- java 22 - 6 多线程之线程调度和设置线程的优先级
线程调度 假如我们的计算机只有一个 CPU,那么 CPU 在某一个时刻只能执行一条指令,线程只有得到 CPU时间片,也就是使用权,才可以执行指令. 那么Java是如何对线程进行调用的呢? 线程有两种调 ...
- parentNode的兼容性问题
IE9下获取对象层次中的父对象,9以上获取文档层次中的父对象 <table><tr><p><td id="haha">haha< ...
- 夯实基础之php学习-2提高篇
1,Jpgraph, 详见Php图形化jpgraph 2,文件系统 文件的操作步骤:打开文件->操作文件->关闭文件 打开文件fopen(filename,mode) 关闭文件fclose ...
- BZOJ 1087 【SCOI2005】 互不侵犯King
Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...
- json解析性能比较(gson与jackson) (zz)
现在json的第三方解析工作很多,如json-lib,gson,jackson,fastjson等等.在我们完成一般的json-object转换工作时,几乎都没有任何问题.但是当数据的量上来时,他们的 ...
- Oracle 常用操作【02】数据库特性
1. 导出 oracle 注释 -- 表明細+表注释+字段明细+字段注释 a.一个用户下的表明細+表注释+字段明细+字段注释 select ATC.OWNER, atC.TABLE_NAME, utc ...
- Python2.7-异常和工具
来自<python学习手册第四版>第七部分,而且本书发布的时候3.1还未发布,所以针对本书的一些知识会有些滞后于python的版本,具体更多细节可以参考python的标准手册. 一.异常基 ...