1.引入jar包到lib目录下:只需要mybatis的一个mybatis.jar及数据库的jar包。
2。在src下新建xml配置文件,即上图中的conf.xml
1 2 34 5 6 7 10 11 128 9 13 14 36 3715 16 25 2617 18 19 2420 21 22 23 27 3528 29 3430 31 32 33 38 4239 40 41
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 34 7 10 11 12 15 16 17 18 22 23 2425 29 30 31 3526 27 28 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 Listln = 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 ListselectList();14 }