新建一个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 ...
随机推荐
- System.Diagnostics命名空间里的Debug类和Trace类的用途
在 .NET 类库中有一个 System.Diagnostics 命名空间,该命名空间提供了一些与系统进程.事件日志.和性能计数器进行交互的类库.当中包括了两个对开发人员而言十分有用的类--Debug ...
- 00-Java 语言简介
一.开发环境搭建: (一).JAVA语言简介: 1.JAVA语言简介: (1)什么是JAVA:Java是一种计算机编程语言.它是一种计算机编程语言.它是一种软件开发平台.它是一种软件运行平台.它是一种 ...
- linux概念之cpu分析
http://ilinuxkernel.com/?cat=4 Linux CPU占用率原理与精确度分析1 CPU占用率计算原理在Linux/Unix 下,CPU 利用率分为用户态.系统态和空闲态,分 ...
- OpenJudge计算概论-第二个重复出现的数
/*======================================================================== 第二个重复出现的数 总时间限制: 1000ms 内 ...
- windows下android开发环境搭建
JDK的安装和Java环境变量的设置 1 JDK下载地址 JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.h ...
- EnterpriseLibrary之Caching应用
http://blog.csdn.net/linux7985/article/details/6239433 http://cache.baiducontent.com/c?m=9f65cb4a8c8 ...
- Linux From Scratch [2]
1. gcc需要的一些lib GMP:A free library for arbitrary precision arithmetic, operating on signed integers, ...
- maven本地仓库的配置以及如何修改默认.m2仓库位置
本地仓库是远程仓库的一个缓冲和子集,当你构建Maven项目的时候,首先会从本地仓库查找资源,如果没有,那么Maven会从远程仓库下载到你本地仓库.这样在你下次使用的时候就不需要从远程下载了.如果你所需 ...
- CentOS 7.0 安装中文输入法
这是个蛋疼的问题 1.Applications -- System Tools -- Setting -- Regin & Language 2.input source -- + -- mo ...
- Env:Winmanager插件使用
转自:http://www.cnblogs.com/feichexia/archive/2012/11/06/vim_WinManager.html 1.准备winmanger插件,从下面网址下即可: ...