使用maven创建的一个quickstart项目,然后在pom.xml中添加MyBatis的相关依赖,pom.xml文件如下:
junit
junit
4.11
test
org.mybatis
mybatis
3.4.0
mysql
mysql-connector-java
5.1.6
然后观察下IDEA的项目结构:File -> Project Structure ->Modules
此时,代码的源路径为: src/main/java 资源文件的源路径为:src/main/java/resource
InputStream inputstream = class.getClassLoader().getResourceAsStream(resource)
在使用以上方法读取资源的时候,在IDEA中,如果resource不是设置的资源文件的源路径,那么读出的io流会报错,
为null,参见:getResourceAsStream的几种路径配置 。
以上为项目结构设置,下面简单介绍下使用 xml 文件 以及 注解方式的Mybatis使用方式。
首先,我们看主配置文件conf.xml:
需要注意的几个地方:
1.
2.
3.
然后,我们再来看一个具体的
这里一个mapper.xml文件与一个接口相对应,这里是对一个user表的操作来举例,user表里分别有id,name,corp(公司)属性。
Mybatis为一个ORM工具,即对象-关系映射模型,那么我们下面分别介绍下 对象User,以及将此对象与关系数据库映射的文件 UserOp.interface 与 UserOp.xml 。
1. User 类
public class user {
private int id;
private String name;
private String corp;
public user(){}
public user(int id, String name, String corp) {
this.id = id;
this.name = name;
this.corp = corp;
}
public user(String name, String corp) {
this.name = name;
this.corp = corp;
}
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;
}
public String getCorp() {
return corp;
}
public void setCorp(String corp) {
this.corp = corp;
}
}
需要注意的是,如果不加默认的构造函数,会报错:
org.apache.ibatis.executor.ExecutorException: No constructor found in com....
所以需要加一个默认的构造函数。
2. UserOp.interface
public interface UserOP {
public void addUser(user user);
public void updateUser(user user);
public void deleteUser(int id);
public user getUser(int id);
}
此接口中定义了几种操作,在使用时,调用此接口中的方法。
3.UserOp.xml
insert into user (name,corp) values (#{name}, #{corp})
update user set name = #{name}, corp = #{corp} where id = #{id}
delete from user where id = #{id}
需要注意的几点:
id = #{id},其中 前面的id为数据库的属性,第二个为对象的属性。
4. 测试类
public class test_main_op {
public static void main(String[] args) {
String resource = "conf.xml";
InputStream is = test_main.class.getClassLoader().getResourceAsStream(resource);
// 创建SqlSessonFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//获取Session
SqlSession session = sessionFactory.openSession(true);
try {
//获取操作类
UserOP userOP = session.getMapper(UserOP.class);
user user1 = new user("wade","nba");
//完成操作
userOP.addUser(user1);
user1.setCorp("lining");
userOP.updateUser(user1);
} finally {
//关闭Session
session.close();
}
}
}
至此,一个简单的MyBatis使用Demo就完成了,下面看看如何使用注解的方式来进行sql查询。
1. GetUserInfoAnnotation.interface
public interface GetUserInfoAnnotation {
@Select("select * from user where id = #{id}")
public user getUser(int id);
}
2.测试类
public class test_main_anotation {
public static void main(String[] args) {
String resource = "conf.xml";
InputStream is = test_main.class.getClassLoader().getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
.build(is);
// 增加配置
Configuration conf = sessionFactory.getConfiguration();
conf.addMapper(GetUserInfoAnnotation.class);
SqlSession session = sessionFactory.openSession();
try {
GetUserInfoAnnotation getUserInfo = session.getMapper(GetUserInfoAnnotation.class);
user user1 = getUserInfo.getUser(1);
System.out.println(user1.getId() + " " + user1.getName() + " " + user1.getCorp());
} finally {
session.close();
}
}
}