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 数据库的经典操作:增删改查. 在这一章我们主要说明一下简单的查询和增删改, ...
随机推荐
- IDEA使用(1)intellIJ idea 配置 svn
以前开发工具一直用的是Eclipse/MyEclipse,虽然早就听说过Idea而且也尝试用过几次, 说实话一开始使用idea真是很不习惯,不只是快捷键不同:比如项目和模块.服务器(如Tomcat)配 ...
- MVC编写的新闻页面
1.新闻发布系统 2.架构确立 3.数据表确立 4.分层 entity dao BaseDao sqlserver jar包 接口层(NewsDetailDAO) impl (NewsDetailDA ...
- java 27 - 9 反射之 动态代理的概述和实现
代理:本来应该自己做的事情,却请了别人来做,被请的人就是代理对象. 举例:春季回家买票让人代买 动态代理: 在程序运行过程中产生的这个对象 而程序运行过程中产生对象其实就是我们刚才反射讲解的内容,所以 ...
- Lambda表达式详解(转载)
原文链接:http://www.cnblogs.com/knowledgesea/p/3163725.html lambda简介 lambda运算符:所有的lambda表达式都是用新的lambda运算 ...
- 如何为logo配色
原网链接:http://design.jobbole.com/125287/ 色彩是带有情绪的.我们能感知到的色彩能带来各种各样的情绪,也能传达一种思想或一种文化. 企业logo的色彩就是利用上面的原 ...
- 定时取数据库的schema,并推送到git服务器
写了个脚本,定时去数据库取schema,并推送到公司的git里. #daily_schema.py #/usr/bin/env python import os import datetime,tim ...
- 【转】TestFlight APP测试(IOS如何让上架前给其他人测试)
原文网址:http://blog.csdn.net/dexin5195/article/details/43966571 大家都知道, 以前iOS项目要测试只需要上传到testflightapp.co ...
- Java命令行的执行参数
Java 程序命令行参数说明 启动Java程序的方式有两种: # starts a Java virtual machine, loads the specified class, and invok ...
- Anterior and posterior commissures
Source: https://en.wikipedia.org/wiki/Posterior_commissure Figrues archive.
- .NET Core)的ZooKeeper异步客户端
支持断线重连.永久watcher.递归操作并且能跨平台(.NET Core)的ZooKeeper异步客户端 阅读目录 什么是ZooKeeper? 项目介绍 提供的功能 使用说明 FAQ 在公司内部 ...