MyBatis学习总结
1.引入jar包到lib目录下:只需要mybatis的一个mybatis.jar及数据库的jar包。
2。在src下新建xml配置文件,即上图中的conf.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 3 <configuration> 4 5 <!-- 定义别名 --> 6 <typeAliases> 7 <typeAlias type="com.hanqi.News" alias="News"/> 8 9 </typeAliases> 10 11 <!-- 环境配置 --> 12 <environments default="test"> 13 <!-- 开发环境 --> 14 <environment id="development"> 15 <!-- 事务管理器 --> 16 <transactionManager type="JDBC" /> 17 <!-- 数据源 POOLED池连接;NOPOOLED 非池连接--> 18 <dataSource type="POOLED"> 19 <property name="driver" value="oracle.jdbc.driver.OracleDriver" /> 20 <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" /> 21 <property name="username" value="test" /> 22 <property name="password" value="test" /> 23 </dataSource> 24 </environment> 25 <!-- 测试环境 --> 26 <environment id="test"> 27 <transactionManager type="JDBC" /> 28 <dataSource type="POOLED"> 29 <property name="driver" value="oracle.jdbc.driver.OracleDriver" /> 30 <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" /> 31 <property name="username" value="test" /> 32 <property name="password" value="test" /> 33 </dataSource> 34 </environment> 35 </environments> 36 37 <mappers> 38 <mapper resource="com/hanqi/newsMapper.xml"/> 39 40 <mapper class="com.hanqi.newsInterface"/> 41 </mappers> 42 </configuration>
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 createdate 60 */ 61 public Date getCreatedate() { 62 return createdate; 63 } 64 /** 65 * @param createdate the createdate to set 66 */ 67 public void setCreatedate(Date createdate) { 68 this.createdate = createdate; 69 } 70 /** 71 * @return the author 72 */ 73 public String getAuthor() { 74 return author; 75 } 76 /** 77 * @param author the author to set 78 */ 79 public void setAuthor(String author) { 80 this.author = author; 81 } 82 @Override 83 public String toString() { 84 return "News [id=" + id + ", title=" + title + ", contant=" + contant + ", createdate=" + createdate + "]"; 85 } 86 }
4.新建映射配置文件:newsMapper.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <mapper namespace="com.hanqi.newsMapper"> 4 <!-- 5 根据id查询得到一个News对象 6 --> 7 <select id="getNewsByID" parameterType="int" resultType="News"> 8 select * from news where id=#{id} 9 </select> 10 <!-- 定义结果集 --> 11 <resultMap type="News" id="newsList"> 12 <!-- <id property="id" column="news_id"/> 13 <result property="title" column="tit"/> 14 --> 15 </resultMap> 16 17 <!-- 多数据查询 --> 18 <select id="getAllList" resultMap="newsList"> 19 select * from news 20 21 </select> 22 23 <!-- 定义条件集 --> 24 <parameterMap type="java.util.HashMap" id="titlelike"> 25 <parameter property="tit"/> 26 <parameter property="aut"/> 27 28 </parameterMap> 29 30 <!-- 传多个条件多数据查询 --> 31 <select id="getList" parameterMap="titlelike" resultMap="newsList"> 32 select * from news where title like '%'||#{tit}||'%' and author like '%'||#{aut}||'%' 33 34 </select> 35 <insert id="insertNews" parameterType="News"> 36 insert into news (id, title, contant, createdate, author) values ( HIBERNATE_SEQUENCE.nextval, #{title}, #{contant}, sysdate, #{author}) 37 38 </insert> 39 </mapper>
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 @Test 18 public void test() throws Exception { 19 //1.加载配置文件到输入流里 20 Reader reader = Resources.getResourceAsReader("conf.xml"); 21 22 //2.创建工厂类SqlSessionFactory 23 SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader); 24 25 //3.获取sqlSession 26 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<News> ln = ss.selectList("com.hanqi.newsMapper.getAllList"); 44 45 System.out.println("ln=......"+ln); 46 47 //条件查询 48 HashMap<String, Object> hm = new HashMap<String, Object>(); 49 50 hm.put("tit", "d"); 51 hm.put("aut", "a"); 52 53 List<News> ln1 = ss.selectList("com.hanqi.newsMapper.getList",hm); 54 55 System.out.println("ln1=#######"+ln1); 56 57 //测试注解update 58 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<News> ln2 = ss.selectList("com.hanqi.newsInterface.selectList"); 66 67 System.out.println("ln2=@@@@@@@@"+ln2); 68 69 //提交 70 ss.commit(); 71 //关闭session 72 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<News> selectList(); 14 }