博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MyBatis学习总结
阅读量:4964 次
发布时间:2019-06-12

本文共 6405 字,大约阅读时间需要 21 分钟。

1.引入jar包到lib目录下:只需要mybatis的一个mybatis.jar及数据库的jar包。

2。在src下新建xml配置文件,即上图中的conf.xml

1 
2 3
4 5
6
7
8 9
10 11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 37
38
39 40
41
42

3。新建数据库表NEWS,和实体类News.java

1 package com.hanqi; 2  3 import java.util.Date; 4  5 public class News { 6  7     private Integer id; 8     private String title; 9     private String contant;10     private Date createdate;11     private String author;12     public News(Integer id, String title, String contant, String author) {13         super();14         this.id = id;15         this.title = title;16         this.contant = contant;17         this.author = author;18     }19     20     public News(String title, String contant, String author) {21         super();22         this.title = title;23         this.contant = contant;24         this.author = author;25     }26 27 28     public News(Integer id, String title) {29         super();30         this.id = id;31         this.title = title;32     }33     34     35     public News() {36     }37 38 39     public Integer getId() {40         return id;41     }42     public void setId(Integer id) {43         this.id = id;44     }45     public String getTitle() {46         return title;47     }48     public void setTitle(String title) {49         this.title = title;50     }51     public String getContant() {52         return contant;53     }54     public void setContant(String contant) {55         this.contant = contant;56     }57     58     /**59      * @return the createdate60      */61     public Date getCreatedate() {62         return createdate;63     }64     /**65      * @param createdate the createdate to set66      */67     public void setCreatedate(Date createdate) {68         this.createdate = createdate;69     }70     /**71      * @return the author72      */73     public String getAuthor() {74         return author;75     }76     /**77      * @param author the author to set78      */79     public void setAuthor(String author) {80         this.author = author;81     }82     @Override83     public String toString() {84         return "News [id=" + id + ", title=" + title + ", contant=" + contant + ", createdate=" + createdate + "]";85     }86 }

4.新建映射配置文件:newsMapper.xml

1 
2 3
4
7
10
11
12
15
16 17
18
22 23
24
25
26
27 28
29 30
31
35
36 insert into news (id, title, contant, createdate, author) values ( HIBERNATE_SEQUENCE.nextval, #{title}, #{contant}, sysdate, #{author})37 38
39

5.新建测试用例,进行测试

1 package com.hanqi; 2  3 import static org.junit.Assert.*; 4  5 import java.io.IOException; 6 import java.io.Reader; 7 import java.util.*; 8  9 import org.apache.ibatis.io.Resources;10 import org.apache.ibatis.session.SqlSession;11 import org.apache.ibatis.session.SqlSessionFactory;12 import org.apache.ibatis.session.SqlSessionFactoryBuilder;13 import org.junit.Test;14 15 public class TestMyBatis {16 17     @Test18     public void test() throws Exception {19         //1.加载配置文件到输入流里20         Reader reader = Resources.getResourceAsReader("conf.xml");21         22         //2.创建工厂类SqlSessionFactory23         SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);24         25         //3.获取sqlSession26         SqlSession ss = ssf.openSession();27         28         //4.调用数据库操作29         //单数据查询30         News n = ss.selectOne("com.hanqi.newsMapper.getNewsByID",77);31         32         //输出结果33         System.out.println("n=" + n);34         35         //插入数据36         News n1 = new News("ddd","eeee","aaa");37         38         int i = ss.insert("com.hanqi.newsMapper.insertNews", n1);39         40         System.out.println("insert="+ i);41         42         //查询数据43         List
ln = ss.selectList("com.hanqi.newsMapper.getAllList");44 45 System.out.println("ln=......"+ln);46 47 //条件查询48 HashMap
hm = new HashMap
();49 50 hm.put("tit", "d");51 hm.put("aut", "a");52 53 List
ln1 = ss.selectList("com.hanqi.newsMapper.getList",hm);54 55 System.out.println("ln1=#######"+ln1);56 57 //测试注解update58 News n2 = new News(95,"测试MyBatis","测试","测试");59 60 int i1 = ss.update("com.hanqi.newsInterface.updateNews", n2);61 62 System.out.println("n3="+i1);63 64 //注解查询65 List
ln2 = ss.selectList("com.hanqi.newsInterface.selectList");66 67 System.out.println("ln2=@@@@@@@@"+ln2);68 69 //提交70 ss.commit();71 //关闭session72 ss.close();73 //关闭流74 reader.close();75 76 }77 78 }

6.也可以用注解代替映射配置文件,但是需要新建一个实体类对应的接口,在接口方法上加注解

1 package com.hanqi; 2  3 import java.util.List; 4  5 import org.apache.ibatis.annotations.*; 6  7 public interface newsInterface { 8      9     @Update("update news set title=#{title}, contant=#{contant}, author=#{author} where id =#{id}")10     public int updateNews(News news);11     12     @Select("select * from news")13     public List
selectList();14 }
View Code

 

转载于:https://www.cnblogs.com/dirgo/p/5173409.html

你可能感兴趣的文章
避免内存重叠memmove()性能
查看>>
【ASP.NET】从服务器端注册客户端脚本
查看>>
Infix to Postfix Expression
查看>>
SELECT LOCK IN SHARE MODE and FOR UPDATE
查看>>
Perl/Nagios – Can’t locate utils.pm in @INC
查看>>
目录导航「深入浅出ASP.NET Core系列」
查看>>
简易爬虫(爬取本地数据)
查看>>
python 进程间通信
查看>>
深拷贝 vs 浅拷贝 释放多次
查看>>
Javascript 有用参考函数
查看>>
点群的判别(三)
查看>>
GNSS 使用DFT算法 能量损耗仿真
查看>>
【转】Simulink模型架构指导
查看>>
MYSQL数据库的导出的几种方法
查看>>
SQL Server-5种常见的约束
查看>>
硬件之美
查看>>
[转载]java开发中的23种设计模式
查看>>
表格的拖拽功能
查看>>
函数的形参和实参
查看>>
文字过长 用 ... 表示 CSS实现单行、多行文本溢出显示省略号
查看>>