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 数据库的经典操作:增删改查. 在这一章我们主要说明一下简单的查询和增删改, ...
随机推荐
- 学习图像算法阶段性总结 (附一键修图Demo) 2016.04.19更新demo
今天特别感慨,自己从决定研究图像处理,势必要做出一键修图算法. 经历了,三个多月的书籍积累,三个多月的算法调整以及优化. 人是一种奇怪的动物,当你做不到的时候,你以为做到了,自己会感觉很爽,很有成就感 ...
- HTML 学习笔记 CSS样式(简介和语法)
CSS概述 CSS指层叠样式表(Cascading Style Sheets) 样式定义如何显示HTML元素 样式通常存储在样式表中 把样式添加到HTML4.0中 是为了解决内容与表现分离的问题 外部 ...
- 如何用javac 和java 编译运行整个Java工程 (转载)【转】在Linux下编译与执行Java程序
如何用javac 和java 编译运行整个Java工程 (转载) http://blog.csdn.net/huagong_adu/article/details/6929817 [转]在Linux ...
- HealthKit框架
HealthKit框架相关资料 链接: HealthKit框架参考 HealthKit开发快速入门教程之HealthKit数据的操作 HealthKit开发快速入门教程之HealthKit框架体系创建 ...
- Activity 与 Service 之间的消息传递
BgService代码 public class BgService extends Service { public static final String TAG = "BgServic ...
- win10显示此电脑
http://jingyan.baidu.com/article/3aed632e00dfe17011809169.html
- CentOS 7.0 Nvidia显卡安装步骤
from: http://blog.sina.com.cn/s/blog_49c0985a0102v3fa.html CentOS 7.0 Nvidia显卡安装步骤: 1 在英伟达官网下载相应驱动 搜 ...
- 运行mvc3.0项目所需dll
Microsoft.Web.Infrastructure 位置:C:\Program Files\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies ...
- struts2: 通过流输出实现exce导出
参考下面代码,在Action中加一个方法: // 导出excel public String excel() throws Exception { StringBuffer excelBuf = ne ...
- java中String、StringBuffer、StringBuilder的区别
java中String.StringBuffer.StringBuilder是编程中经常使用的字符串类,他们之间的区别也是经常在面试中会问到的问题.现在总结一下,看看他们的不同与相同. 1.可变与不可 ...