标签:mybatis
MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除
了几乎所有的 JDBC 代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML
或注解用于配置和原始映射,将接口和 Java的 POJOs(Plan Old Java Objects,普通的 Java
对象)映射成数据库中的记录。
每一个 MyBatis 的应 用程序 都以一 个 SqlSessionFactory 对象的 实例为 核心。
SqlSessionFactory 对 象 的 实 例 可 以 通 过SqlSessionFactoryBuilder 对象 来 获 得 。
SqlSessionFactoryBuilder 对象可以从 XML 配置文件,或从Configuration 类的习惯准备的实
例中构建 SqlSessionFactory 对象。
#数据库脚本 USE mysql #创建表 CREATE TABLE users ( userId INTEGER AUTO_INCREMENT PRIMARY KEY, userName VARCHAR(20) NOT NULL, userAge INTEGER NOT NULL
) #加入测试数据 INSERT INTO users(userName,userAge) VALUES(‘张三‘,22); INSERT INTO users(userName,userAge) VALUES(‘李四‘,33); INSERT INTO users(userName,userAge) VALUES(‘王五‘,44);
SELECT * FROM users |
mysql-connector-java-5.1.7-bin.jar
mybatis-3.2.7.jar
工程目录
package com.morris.mybatis.entity;
public class Users {
private Integer userId;
private String userName;
private Integer userAge;
public Integer getUserId() { return userId; }
public void setUserId(Integer userId) { this.userId = userId; }
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
public Integer getUserAge() { return userAge; }
public void setUserAge(Integer userAge) { this.userAge = userAge; }
public Users(Integer userId, String userName, Integer userAge) { super(); this.userId = userId; this.userName = userName; this.userAge = userAge; }
public Users() { super(); // TODO Auto-generated constructor stub }
@Override public String toString() { return "Users [userId=" + userId + ", userName=" + userName + ", userAge=" + userAge + "]"; }
}
|
<?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>
<!-- 配置mybatis的数据库连接 --> <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/mysql" /> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments>
<!-- 引入映射文件 --> <mappers> <mapper resource="com/morris/mybatis/entity/UsersMapper.xml"/> </mappers> </configuration> |
<?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.morris.mybatis.entity.Users">
<select id="selectUserByID" parameterType="java.lang.Integer" resultType="com.morris.mybatis.entity.Users"> select * from users where userId = #{id} </select> </mapper> |
package com.morris.mybatis.entity;
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;
public class Test {
public static void main(String[] args) throws Exception {
Reader reader = Resources.getResourceAsReader("configuration.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder() .build(reader);
SqlSession session = sqlSessionFactory.openSession();
String sql = "com.morris.mybatis.entity.Users.selectUserByID";
Users user = session.selectOne(sql, 1);
System.out.println(user);
}
} |
Users[userId=1, userName=张三, userAge=22]
标签:mybatis
原文地址:http://blog.csdn.net/u022812849/article/details/42105607