首先,创建一个数据库my,并在数据库中插入一张表user,然后在user表中插入一行数据,代码如下:
create database my; use my; create table user( id int(10) auto_increment, name varchar(64), level varchar(256), phone varchar(256), primary key(id) ); insert into user(id,name,level,phone) values(1,'a','a','1234555666');
其次,创建mybatis配置文件Configure.xml
<?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.zk.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://localhost:3306/my" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <mappers> <!-- // power by http://www.yiibai.com 注释掉咯... <mapper resource="com/yiibai/mybatis/models/User.xml" />--> </mappers> </configuration>
接着,创建实体类user.java,放在com.zk.models下,
package com.zk.models; public class user { private Integer id; private String name; private String level; private String phone; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLevel() { return level; } public void setLevel(String level) { this.level = level; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return "user [id=" + id + ", name=" + name + ", level=" + level + ", phone=" + phone + "]"; } }
再次,创建一个IUser接口,放置在com.zk.dao下,如下:
package com.zk.dao; import org.apache.ibatis.annotations.Select; import com.zk.models.user; public interface IUser {
/*注解方式实现*/ @Select("select * from user where id=#{id}") public user getUserById(int id); }
最后,实现一个main函数
package Main; 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.zk.dao.IUser; import com.zk.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.getLevel()); System.out.println("主页:"+user.getPhone()); } finally { session.close(); } } }
程序结构如下: