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,内容如下:

  1. log4j.rootLogger = DEBUG,CONSOLE
  2.  
  3. log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
  4. log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
  5. log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} -%-4r [%t] %-5p %x - %m%n
  6.  
  7. log4j.logger.java.sql.ResultSet = DEBUG
  8. log4j.logger.java.sql.Connection = DEBUG
  9. log4j.logger.java.sql.Statement = DEBUG
  10. log4j.logger.java.sql.PreparedStatement = DEBUG

4、新建一张测试表news 和一个测试的pojo

News.java内容:

  1. package com.hy;
  2.  
  3. import java.sql.Date;
  4.  
  5. public class News {
  6. private Integer id;
  7. private String title;
  8. private String content;
  9. private String author;
  10. private Date createdate;
  11.  
  12. public News() {
  13.  
  14. }
  15.  
  16. public News(String title, String content, String author, Date createdate) {
  17. super();
  18. this.title = title;
  19. this.content = content;
  20. this.author = author;
  21. this.createdate = createdate;
  22. }
  23.  
  24. public Date getCreatedate() {
  25. return createdate;
  26. }
  27.  
  28. public void setCreatedate(Date createdate) {
  29. this.createdate = createdate;
  30. }
  31.  
  32. public Integer getId() {
  33. return id;
  34. }
  35. public void setId(Integer id) {
  36. this.id = id;
  37. }
  38. public String getTitle() {
  39. return title;
  40. }
  41. public void setTitle(String title) {
  42. this.title = title;
  43. }
  44. public String getContent() {
  45. return content;
  46. }
  47. public void setContent(String content) {
  48. this.content = content;
  49. }
  50. public String getAuthor() {
  51. return author;
  52. }
  53. public void setAuthor(String author) {
  54. this.author = author;
  55. }
  56.  
  57. }

5、写一个mybatis基础配置文件mybatis.config.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <environments default="development">
  7. <environment id="development">
  8. <transactionManager type="JDBC">
  9. </transactionManager>
  10. <dataSource type="POOLED">
  11. <property name="driver" value="com.mysql.jdbc.Driver" />
  12. <property name="url" value="jdbc:mysql://127.0.0.1:3306/hibernate" />
  13. <property name="username" value="root" />
  14. <property name="password" value="root" />
  15. </dataSource>
  16. </environment>
  17. </environments>
  18. <mappers>
  19. <mapper resource="com/hy/map/News.xml"/>
  20. </mappers>
  21. </configuration>

文件中主要包含environments连接数据库的基本配置,mappers实体操作文件主要放sql语句

6、新建New.xml:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.hy">
  6. <select id="getNews" resultType="com.hy.News" parameterType="int">
  7. select * from news where id=#{id}
  8. </select>
  9. </mapper>
  1. parameterType 是指参数类型
  2.  
  3. 7、写一个main test测试类
  1. package com.hy;
  2.  
  3. import java.io.IOException;
  4. import java.io.Reader;
  5.  
  6. import org.apache.ibatis.io.Resources;
  7. import org.apache.ibatis.session.SqlSession;
  8. import org.apache.ibatis.session.SqlSessionFactory;
  9. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  10.  
  11. public class Main {
  12.  
  13. public static void main(String[] args) {
  14. String resource = "com/hy/map/mybatis.config.xml";
  15. Reader reader = null;
  16. SqlSession sqlSession = null;
  17. try {
  18. reader = Resources.getResourceAsReader(resource);
  19. } catch (IOException e) {
  20. // TODO Auto-generated catch block
  21. e.printStackTrace();
  22. }
  23.  
  24. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
  25. sqlSession = sqlSessionFactory.openSession();
  26. News news = sqlSession.selectOne("getNews", 1);
  27. System.out.println(news.getTitle());
  28. }
  29.  
  30. }

a、加载基本配置文件;b、获取SqlSessionFactory ;c、获取sqlsession;d、执行对应id的sql返回数据

新建一个mybatis HelloWorld的更多相关文章

  1. 我的第一个SpringProject——HelloWorld

    我的第一个Spring项目HelloWorld还是花了不少时间,在工具配置上耽误了 我使用的是Eclipse,开始Maven+intellij+Spring搞不太懂 首先要配置Spring: 打开ec ...

  2. zynq学习01 新建一个Helloworld工程

    1,好早买了块FPGA板,zynq 7010 .终极目标是完成相机图像采集及处理.一个Window C++程序猿才开始学FPGA,一个小菜鸟,准备转行. 2,关于这块板,卖家的官方资料学起来没劲.推荐 ...

  3. Android Studio新建一个HelloWorld 程序(App)

    Android Studio新建一个HelloWorld程序(App) 新建 或者直接启动程序(注:如果已有程序,此方法会直接打开最近一次关闭从程序) 更改App名 选择App运行平台 选择模板 更改 ...

  4. Java Learning 001 新建一个Java工程 HelloWorld程序

    Java Learning 001 新建一个Java工程 HelloWorld程序 Step 1 . 在Eclipse 软件里,点击: File -> New -> Java Projec ...

  5. Mybaits 源码解析 (一)----- 搭建一个mybatis框架(MyBatis HelloWorld)

    源码分析之前先搭一个mybatis的demo,这个在看源码的时候能起到了很大的作用,因为在看源码的时候,会恍然大悟,为什么要这么配置,为什么要这么写.(老鸟可以跳过这篇) 开发环境的准备 创建mave ...

  6. [J2EE]MyBatis HelloWorld

    一.MyBatis简单介绍 iBatis是apche的一个开源项目.2010年迁移到google code后改名为MyBatis,2013年前已到github.MyBatis是一个基于java的持久层 ...

  7. Ionic2学习笔记(1):新建一个页面

    作者:Grey 原文地址: http://www.cnblogs.com/greyzeng/p/5532323.html                 新建一个页面: 借上一篇中的HelloWorl ...

  8. Android学习笔记(一)——新建一个项目

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 1.打开Android Studio时出现以下界面,点击”start a new Android Studio ...

  9. 第一次使用Android Studio时你应该知道的一切配置(二):新建一个属于自己的工程并安装Genymotion模拟器

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

随机推荐

  1. PHP使用mail()函数发送邮件流程以及注意事项

    1. 配置 php.ini sendmail_path = /usr/sbin/sendmail -f yourname@sth.com -t -i 2. 使用函数 mail($to, $subjec ...

  2. WebApiThrottle限流框架

    ASP.NET Web API Throttling handler is designed to control the rate of requests that clients can make ...

  3. 【转】SVN的UUID错误

    操作TortoiseSVN时,报如下错误:       Command Update       Repository UUID '62b86956-73d9-2945-ba87-0546d71898 ...

  4. JS刷新页面的几种方法(转)

    Javascript刷新页面的几种方法: 1 history.go(0) 2 location.reload() 3 location=location 4 location.assign(locat ...

  5. python selenium下载电子书

    妹纸推荐书籍<御伽草纸>,网上找了很久都找不到下载,估计是被Amazon版权了,但是在网易云阅读看到有书,所以就写个代码下载下来. 由于网易云阅读是js加载,用requests或者下载ht ...

  6. css之padding,marging

    padding:内边距,所有浏览器都支持,不允许使用负值 继承内部格式生成了10px的边距. 属性: auto:浏览器计算机内边距. length:规定以具体单位计的内边距值,比如像素.厘米等.默认值 ...

  7. maven项目依赖小试牛刀

    1.先建立空的wbh-parent,留下pom.xml;将项目中用的jar包依赖全放进去: 2.建立core项目,当然全是maven的,这个建好后,是用来让其他项目引用的,所以必须用maven ins ...

  8. azure 云上MySQL最新版本 MySQL5.7.11 批量自动化一键式安装 (转)

    --背景云端 以前都喜欢了源码安装mysql,总觉得源码是高大上的事情,不过源码也需要时间,特别是make的时候,如果磁盘和cpu差的话,时间很长很长,在虚拟机上安装mysql尤其甚慢了. 现在业务发 ...

  9. MySql之on duplicate key update详解

    在我们的日常开发中,你是否遇到过这种情景:查看某条记录是否存在,不存在的话创建一条新记录,存在的话更新某些字段.你的处理方式是不是就是按照下面这样? $result = mysql_query('se ...

  10. Java异常之checked与unchecked

    首先,java的异常分为Error和Exception.这两类都是接口Throwable的子类.Error及Exception及其子类之间的关系,大致可以用下图简述. 注意事项: 1. Error仅在 ...