1.创建一个接口:IUser,并在其中声明对应的操作方法
public interface IUser {
@Select("select * from user where id= #{id}")
public User getUserByID(int id);
}
2. 创建对应映射接口的SQL语句
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="User" type="com.mybatis.models.User" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/testdb" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mybatis/models/User.xml" />-->
</mappers>
</configuration>
在包:com.mybatis.models 下创建一个User.java 类文件
package com.mybatis.models;
public class User {
private int id;
private String name;
private String dept;
private String phone;
private String website;
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
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 getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
3.测试接口映射 在 src 这个目录下创建一个类:Main.java,用来测试整个配置和程序运行结果,有关 Main.java 的代码详细内容如下
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 com.mybatis.dao.IUser;
import com.mybatis.models.User;
public class Main {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
static {
try {
reader = Resources.getResourceAsReader("config/Configure.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
sqlSessionFactory.getConfiguration().addMapper(IUser.class);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSession() {
return sqlSessionFactory;
}
public static void main(String[] args) {
SqlSession session = sqlSessionFactory.openSession();
try {
IUser iuser = session.getMapper(IUser.class);
User user = iuser.getUserByID(1);
System.out.println("名字:"+user.getName());
System.out.println("所属部门:"+user.getDept());
System.out.println("主页:"+user.getWebsite());
} finally {
session.close();
}
}
}
|