新建一个mybatis HelloWorld
1、下载mybatis
https://github.com/mybatis/mybatis-3/
没有梯子好像打不开
下载一个最新版本,我这里下载的是mybatis-3.4.1.zip
里面有mybatis需要的jar和文档
2、新建一个java project ,新建lib目录把jar复制到lib下面
3、新建一个简单log4j配置文件log4j.properties,内容如下:
log4j.rootLogger = DEBUG,CONSOLE log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} -%-4r [%t] %-5p %x - %m%n log4j.logger.java.sql.ResultSet = DEBUG
log4j.logger.java.sql.Connection = DEBUG
log4j.logger.java.sql.Statement = DEBUG
log4j.logger.java.sql.PreparedStatement = DEBUG
4、新建一张测试表news 和一个测试的pojo
News.java内容:
package com.hy;
import java.sql.Date;
public class News {
private Integer id;
private String title;
private String content;
private String author;
private Date createdate;
public News() {
}
public News(String title, String content, String author, Date createdate) {
super();
this.title = title;
this.content = content;
this.author = author;
this.createdate = createdate;
}
public Date getCreatedate() {
return createdate;
}
public void setCreatedate(Date createdate) {
this.createdate = createdate;
}
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 getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
5、写一个mybatis基础配置文件mybatis.config.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/hibernate" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/hy/map/News.xml"/>
</mappers>
</configuration>
文件中主要包含environments连接数据库的基本配置,mappers实体操作文件主要放sql语句
6、新建New.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.hy">
<select id="getNews" resultType="com.hy.News" parameterType="int">
select * from news where id=#{id}
</select>
</mapper>
parameterType 是指参数类型 7、写一个main test测试类
package com.hy; import java.io.IOException;
import java.io.Reader; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class Main { public static void main(String[] args) {
String resource = "com/hy/map/mybatis.config.xml";
Reader reader = null;
SqlSession sqlSession = null;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
sqlSession = sqlSessionFactory.openSession();
News news = sqlSession.selectOne("getNews", 1);
System.out.println(news.getTitle());
} }
a、加载基本配置文件;b、获取SqlSessionFactory ;c、获取sqlsession;d、执行对应id的sql返回数据
新建一个mybatis HelloWorld的更多相关文章
- 我的第一个SpringProject——HelloWorld
我的第一个Spring项目HelloWorld还是花了不少时间,在工具配置上耽误了 我使用的是Eclipse,开始Maven+intellij+Spring搞不太懂 首先要配置Spring: 打开ec ...
- zynq学习01 新建一个Helloworld工程
1,好早买了块FPGA板,zynq 7010 .终极目标是完成相机图像采集及处理.一个Window C++程序猿才开始学FPGA,一个小菜鸟,准备转行. 2,关于这块板,卖家的官方资料学起来没劲.推荐 ...
- Android Studio新建一个HelloWorld 程序(App)
Android Studio新建一个HelloWorld程序(App) 新建 或者直接启动程序(注:如果已有程序,此方法会直接打开最近一次关闭从程序) 更改App名 选择App运行平台 选择模板 更改 ...
- Java Learning 001 新建一个Java工程 HelloWorld程序
Java Learning 001 新建一个Java工程 HelloWorld程序 Step 1 . 在Eclipse 软件里,点击: File -> New -> Java Projec ...
- Mybaits 源码解析 (一)----- 搭建一个mybatis框架(MyBatis HelloWorld)
源码分析之前先搭一个mybatis的demo,这个在看源码的时候能起到了很大的作用,因为在看源码的时候,会恍然大悟,为什么要这么配置,为什么要这么写.(老鸟可以跳过这篇) 开发环境的准备 创建mave ...
- [J2EE]MyBatis HelloWorld
一.MyBatis简单介绍 iBatis是apche的一个开源项目.2010年迁移到google code后改名为MyBatis,2013年前已到github.MyBatis是一个基于java的持久层 ...
- Ionic2学习笔记(1):新建一个页面
作者:Grey 原文地址: http://www.cnblogs.com/greyzeng/p/5532323.html 新建一个页面: 借上一篇中的HelloWorl ...
- Android学习笔记(一)——新建一个项目
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 1.打开Android Studio时出现以下界面,点击”start a new Android Studio ...
- 第一次使用Android Studio时你应该知道的一切配置(二):新建一个属于自己的工程并安装Genymotion模拟器
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
随机推荐
- 市委组织部考核项目——利用EasyUi中可编辑的DataGrid控件对多行数据进行编辑并提交
http://blog.csdn.net/cjr15233661143/article/details/19041165 市委组织部考核项目中需要录入原始数据,拿开发区的数据录入举例说明,见下图,需要 ...
- SQLServer2014内存优化表评测
SQLServer2014内存优化表评测 分类: SQL内存表2014-06-20 11:49 1619人阅读 评论(11) 收藏 举报 目录(?)[-] SQLServer2014的使用基本要求 内 ...
- ASP.NET MVC中的拦截器
在ASP.NET MVC中,有三种拦截器:Action拦截器.Result拦截器和Exception拦截器, 所谓的拦截器也没有什么的,只是写一个类,继承另一个类和一个接口,顺便实现接口里面的方法而以 ...
- 07-Java 中的IO操作
1.Java IO简介: (1)I/O :in \out 即输入与输出.基本功能:读写. (2)IO流:作用:读写设备上的数据,硬盘文件.内存.键盘.网络-- 根据数据的走向,可分为:输入流.输出流. ...
- shell脚本实例-菜单样例
1.9.1 实例需求 用户在进行Linux系统管理的过程中,经常需要用到查看进程的信息.用户的信息等常用的功能.本例针对这一需求,使用shell编程实现基本的系统管理 功能.通过本程序,可以按照要求实 ...
- 添加和删除节点(HTML 元素)。
JavaScript HTML DOM 元素(节点) 添加和删除节点(HTML 元素). 创建新的 HTML 元素 如需向 HTML DOM 添加新元素,您必须首先创建该元素(元素节点),然后向一 ...
- WAS8.5安装
由于公司网络禁止上传图片,在网上找到相近的,安装过程图,可参照:http://www.open-open.com/doc/view/488fe6eaa2084da6b87cc18f1c00d2a8 1 ...
- Request Session生命周期及struts1 中service的编写
现在接手的项目是一个早期的struts1框架的项目.同时也是刚开始接触web 以及struts1架构. 在处理多个action时,有一个tab子页面需要每5s自动刷新一次. 然后在测试过程中发现,点击 ...
- javascript 与和非
|| : 在javascript中,返回第一个真值,除非都是假值返回最后一个值(也是假值). 1 || 0; 0 || 1; 0 || 0; 0 || undefined; // undefined ...
- Python基础(三)——集合、有序 无序列表、函数、文件操作
1.Set集合 class set(object): """ set() -> new empty set object set(iterable) -> n ...