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 数据库的经典操作:增删改查. 在这一章我们主要说明一下简单的查询和增删改, ...
随机推荐
- ZBrush中怎样对遮罩进行反选
通过对ZBrush的学习,我们知道了如何手动创建遮罩,手动创建遮罩相对来说是最简单有效的方法,在某些特定的使用场合会起到事半功倍的效果.创建遮罩我们可以结合Ctrl键在物体保持编辑的状态下来执行,您可 ...
- poj1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8859 Accepted: 3728 ...
- OC中的指针
NSError *err = nil; NSError __strong **error = &err; //因为在oc中,通过* *err 创建的指针是用__strong修改的,所以要一致, ...
- Fastlane为iOS带来持续部署
Fastlane是一组工具套件,旨在实现iOS应用发布流程的自动化,并且提供一个运行良好的持续部署流程,只需要运行一个简单的命令就可以触发这个流程. Fastlane是一个ruby脚本集合,其中囊括了 ...
- .net AES加密解密
using System; using System.Collections.Generic; using System.Text; using System.Secur ...
- squid代理服务器根据代理IP路由
import os ips = os.popen("""ifconfig |grep 'inet addr:'|awk '{print $2}'| sed '$d'| s ...
- 2795: [Poi2012]A Horrible Poem
2795: [Poi2012]A Horrible Poem Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 484 Solved: 235[Subm ...
- Jquery 数组操作
1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限, ...
- Windows 8.1 新增控件之 CommandBar
上一篇为大家介绍了AppBar 的相关内容,本篇继续介绍CommandBar 的使用方法.与AppBar 相比而言,CommandBar 在开发使用方面较为单一,在按键布局上分为主控区(Primary ...
- 尝试HTML + JavaScript 编写Windows App
一直以来博文中使用最多的就是C# + XAML.进入Windows App时代,又多了一对 Javascript + HTML组合,这对于Web开发的程序员来说再熟悉不过了.其实小编也做过几年的Web ...