com.frank.domain

  Blog.java

 1 package com.frank.domain;
 2 
 3 public class Blog {
 4     
 5     private Integer id;
 6     private String title;
 7     private String content;
 8     public Integer getId() {
 9         return id;
10     }
11     public void setId(Integer id) {
12         this.id = id;
13     }
14     public String getTitle() {
15         return title;
16     }
17     public void setTitle(String title) {
18         this.title = title;
19     }
20     public String getContent() {
21         return content;
22     }
23     public void setContent(String content) {
24         this.content = content;
25     }
26     
27     
28 
29 }
View Code

  BlogMapper.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.frank.domain.BlogMapper">
6     <select id="selectBlog" resultType="com.frank.domain.Blog">
7         select * from Blog where id = #{id}
8     </select>
9 </mapper>
View Code

com.frank.test

  Test.java

 1 package com.frank.test;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 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 import com.frank.domain.Blog;
12 
13 public class Test {
14 
15     public static void main(String[] args) throws IOException {
16         // TODO Auto-generated method stub
17 
18         String resource="mybatis-config.xml";
19         InputStream inputStream=Resources.getResourceAsStream(resource);
20         SqlSessionFactory sessionFaction=new SqlSessionFactoryBuilder().build(inputStream);
21         SqlSession session=sessionFaction.openSession();
22         
23         Blog blog=session.selectOne("com.frank.domain.BlogMapper.selectBlog", 1);
24         System.out.println(blog.getTitle());
25 
26     }
27 
28 }
View Code

 

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             <dataSource type="POOLED">
10                 <property name="driver" value="com.mysql.jdbc.Driver" />
11                 <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis" />
12                 <property name="username" value="root" />
13                 <property name="password" value="frank1994" />
14             </dataSource>
15         </environment>
16     </environments>
17     <mappers>
18         <mapper resource="com/frank/domain/BlogMapper.xml" />
19     </mappers>
20 </configuration>
View Code