今天刚听说iBATIS改名为MyBatis,实际上就是iBATIS 3.0.1。以前都是用hibernate或者JPA,同时听说iBATIS 3与iBATIS 2 相比,变化挺大的,于是今天花了点时间学习了一下,做了个小例子。
准备工作:建立相应的数据库,表,添加一条记录用于测试。
第一步当然是建立项目,导入jar包,导入mybatis-3.0.1.jar,和mysql的jdbc驱动。
第二步 新建一个类User,包含主键,用户名,密码等属性
package cn.dcr.mybatis.entity; public class User { private Long id; private String userName; private String password; private int age; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
第三步,写sql映射的xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="cn.dcr.mybatis.entity.UserMapper"> <select id="selectOne" parameterType="long" resultType="cn.dcr.mybatis.entity.User"> select * from user where id = #{id} </select> </mapper>
第四步,写MyBatis配置文件,configuration.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="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/test"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/dcr/mybatis/entity/User.xml"/> </mappers> </configuration>
第五步,编写Util类,用于初始化SqlSessionFactory
package cn.dcr.mybatis.util; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class Util { private static SqlSessionFactory sqlSessionFactory = null; static { String resource = "configuration.xml"; Reader reader = null; try { reader = Resources.getResourceAsReader(resource); } catch (IOException e) { e.printStackTrace(); } sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } public static SqlSessionFactory getSqlSessionFactory(){ return sqlSessionFactory; } }
第六步,编写Dao类,用于封装增删改查等操作(为简单起见,这个类只有一个根据id查询出对应的User对象的方法)
package cn.dcr.mybatis.dao; import org.apache.ibatis.session.SqlSession; import cn.dcr.mybatis.entity.User; import cn.dcr.mybatis.util.Util; public class UserDao { public User query(Long id){ SqlSession session = Util.getSqlSessionFactory().openSession(); User user = null; try{ user = (User)session.selectOne("cn.dcr.mybatis.entity.UserMapper.selectOne", id); }catch(Exception e){ e.printStackTrace(); }finally{ session.close(); } return user; } }
第七步,编写测试类
package cn.dcr.mybatis.test; import cn.dcr.mybatis.dao.UserDao; import cn.dcr.mybatis.entity.User; public class UserTest { public static void main(String[] args){ UserDao userDao = new UserDao(); User user = userDao.query(1L); System.out.println(user.getId()); System.out.println(user.getUserName()); System.out.println(user.getPassword()); System.out.println(user.getAge()); } }
运行一下,能够成功查询出对应的记录。
先到这,等有功夫在研究一下详细用法。