一个简单的例子
我对MyBatis的理解仅仅是:它以前叫IBtais,现在投到谷歌帐下,改名MyBatis,这个框架把JDBC中的重复代码封装起来,减少代码量。仅仅知道这些而已,慢慢学习吧,从网上找了一个最简单的例子。
MyBatis+MySQL的,记录如下:
①数据库
在MySQL中,test数据库下,简历user表,一共两个字段id和name,建表语句略。
②需导入的包
只有两个:
mybatis-3.0.3.jar
mysql-connector-java-5.1.9-bin.jar(JDBC包)
③目录结构
采用最简单的结构,domain包下有且仅有四个文件
configuration.xml
User.xml
User.java
Test.java
④实体类javabean,总共两个变量
view plaincopy to clipboardprint?
01.package domain;
02.
03.public class User {
04. private int id;
05. private String name = null;
06. public User() {
07. }
08. public User(int id, String name) {
09. this.id = id;
10. this.name = name;
11. }
12. public int getId() {
13. return id;
14. }
15. public void setId(int id) {
16. this.id = id;
17. }
18. public String getName() {
19. return name;
20. }
21. public void setName(String name) {
22. this.name = name;
23. }
24. @Override
25. public String toString() {
26. return "User [id=" + id + ", name=" + name + "]";
27. }
28.}
package domain;
public class User {
private int id;
private String name = null;
public User() {
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
⑤MyBatis总配置文件configuration.xml
view plaincopy to clipboardprint?
01.
02.03. "-//mybatis.org//DTD Config 3.0//EN"
04. "">
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23. "-//mybatis.org//DTD Config 3.0//EN"
"">
⑥User类的配置文件User.xml
view plaincopy to clipboardprint?
01.
02.03. "-//mybatis.org//DTD Mapper 3.0//EN"
04. "">
05.
06.
09.
"-//mybatis.org//DTD Mapper 3.0//EN"
"">
⑦测试用例
view plaincopy to clipboardprint?
01.package domain;
02.
03.import java.io.IOException;
04.import java.io.Reader;
05.import org.apache.ibatis.io.Resources;
06.import org.apache.ibatis.session.SqlSession;
07.import org.apache.ibatis.session.SqlSessionFactory;
08.import org.apache.ibatis.session.SqlSessionFactoryBuilder;
09.import domain.User;
10.
11.public class Test {
12. public static void main(String[] args) throws IOException {
13. String resource = "domain/configuration.xml";
14. Reader reader = Resources.getResourceAsReader(resource);
15. SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
16. SqlSession session = ssf.openSession();
17. try {
18. User user = (User) session.selectOne("selectUser", "1");
19. System.out.println(user);
20. } catch (Exception e) {
21. e.printStackTrace();
22. } finally {
23. session.close();
24. }
25. }
26.}
package domain;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import domain.User;
public class Test {
public static void main(String[] args) throws IOException {
String resource = "domain/configuration.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = ssf.openSession();
try {
User user = (User) session.selectOne("selectUser", "1");
System.out.println(user);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
}
}
以上,MyEclipse 8.5下编译通过
阅读(2684) | 评论(0) | 转发(0) |