第一步:下载jar包并导入
1、mysql驱动包
2、mybatis环境包
第二步:创建MYSQL数据库
由于这是用于测试,只创建了test-usreinfo数据表
第三步:在src文件夹中创建mybatis-cfg.xml(mybatis全局配置文件)
<?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>
<environments default="mybatis-mysql">
<environment id="mybatis-mysql">
<transactionManager type="jdbc"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
<property name="username" value="root" />
<property name="password" value="" />
</dataSource>
</environment>
</environments> </configuration>
第四步:在src文件中撞见jar包
1、com.shengmu.model:数据模型包
2、com.shengmu.util:处理工具包
3、com.shengmu.DAO:数据库操作包
4、com.shengmu.test:测试包
第五步:在com.shengmu.model:数据模型包创建User.class
package com.shengmu.model; public class User { private int id;
private String username;
private String userpaw;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpaw() {
return userpaw;
}
public void setUserpaw(String userpaw) {
this.userpaw = userpaw;
} @Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", userpaw=" + userpaw + "]";
} }
第六步:创建usermapper.xml:数据操作配置文件,例如select、insert等操作。将此文件与User.class放在一起。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.shengmu.model.usermapper" >
<select id="SelectUserByUsername" parameterType="int" resultType="com.shengmu.model.User" >
select * from userinfo where id = #{id}
</select>
</mappe
第七步:在mybatis-cfg.xml文件中,加入usermapper.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>
<environments default="mybatis-mysql">
<environment id="mybatis-mysql">
<transactionManager type="jdbc"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
<property name="username" value="root" />
<property name="password" value="" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/shengmu/model/usermapper.xml" />
</mappers> </configuration>
其中:<mappers> <mapper resource="com/shengmu/model/usermapper.xml" /> </mappers>是usermapper.xml在mybatis-cfg.xml配置。
第八步:在com.shengmu.test中加入测试类TestSelect.class
package com.shengmu.test; import java.io.InputStream; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.shengmu.model.User; public class TestSelect {
public static void main(String[] args) {
String resource = "mybatis-cfg.xml";
try {
InputStream inputStream = TestSelect.class.getClassLoader().getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sessionFactory.openSession();
String statment = "com.shengmu.model.usermapper.SelectUserByUsername";
User user = session.selectOne(statment,1); session.commit();
System.out.println(user.getUsername());
session.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
最后run as --java application.