一、环境:Eclipse、Mysql、Mysql驱动包、Mybatis3.jar
二、项目结构(这里创建的是java项目,不是web的)
三、数据库脚本
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`name` varchar(45) NOT NULL,
`dept` varchar(200) NOT NULL,
`website` varchar(200) DEFAULT NULL,
`phone` varchar(16) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';
SELECT * FROM db_lin.user;
四、实例代码
1、user.java
package com.linbilin.mybatis.models;
public class User {
private int id;
private String name;
private String dept;
private String phone;
private String 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;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", dept=" + dept
+ ", phone=" + phone + ", website=" + website + "]";
}
}
2、User.xml
<?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">
<!--
此时这个namespace的值可以随便写,你这边可以把namespace理解为一个类,
select的id理解为namespace这个类的一个方法名。
但如果是按照接口来编程的话,namespace就得写接口的全路径了
-->
<mapper namespace="com.linbilin.mybatis.models.UserMapper">
<select id="getUserByID" parameterType="int" resultType="User">
select * from `user` where id = #{id}
</select>
</mapper>
3、MybatisUtils.java
package com.linbilin.mybatis.utils;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
static {
try {
reader = Resources.getResourceAsReader("config/Configure.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSession() {
return sqlSessionFactory;
}
}
4、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.linbilin.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/db_lin" />
<property name="username" value="root" />
<property name="password" value="ls" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/linbilin/mybatis/models/User.xml" />
</mappers>
</configuration>
5、MyTest.java
package com.linbilin.mybatis.test;
import org.apache.ibatis.session.SqlSession;
import com.linbilin.mybatis.models.User;
import com.linbilin.mybatis.utils.MybatisUtils;
public class MyTest {
public static void main(String[] args) {
SqlSession session = MybatisUtils.getSession().openSession();
try {
User user = (User) session.selectOne("com.linbilin.mybatis.models.UserMapper.getUserByID", 1);
if (user != null) {
String userInfo = user.toString();
System.out.println(userInfo);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
session.close();// 关闭session,释放资源
}
}
}
五、运行结果
六、附件