First, create a database my, and insert a table in the user database, and then insert a row in the user table, as follows:
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,'c','a','1234555666');
Second, create mybatis profile 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>
Next, create entity classes user.java, placed under 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 + "]"; } }
Again, create a IUser interface placed under com.zk.dao, as follows:
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);
}
Finally, to achieve a main function
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(); } } }
Program is structured as follows: