MyBits前身是iBitis,相对于Hibernate而言,它是半自动化ORM框架。本着分享和学习的目的,笔者将陆续把学习笔记与代码贴出,希望对想学习mybitis的同窗们有所帮助。
鉴于我的测试数据库是mysql,同仁们现在把mysql装好,然后准备好mysql驱动以及从官网上下载mybitis(http://code.google.com/p/mybatis/)。我下载的是最新的mybatis-3.2.2.zip。
创建数据库test
然后创建表t_user
[sql] view plain copy
- DROP TABLE IF EXISTS `t_user`;
- CREATE TABLE `t_user` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `name` varchar(20) DEFAULT NULL,
- `pass` varchar(20) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=gbk;
- INSERT INTO `t_user` VALUES (‘1‘, ‘ssssssssssssssss‘, ‘ddsssssssdd‘);
然后创建与表对应的Java bean对象
[java] view plain copy
- <span style="color:#000000;">package com.jefry;
- public class User {
- private int id;
- private String name;
- private String pass;
- public String getPass() {
- return pass;
- }
- public void setPass(String pass) {
- this.pass = pass;
- }
- 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;
- }
- }</span>
接下来,在classpath目录下面创建与配置mybatis-config.xml文件
内容如下:
[html] view plain copy
- <?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://127.0.0.1:3306/test"/>
- <property name="username" value="root"/>
- <property name="password" value="root"/>
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <!--这个文件下面将创建-->
- <mapper resource="com/jefry/UserMapper.xml"/>
- </mappers>
- </configuration>
通过mybatis-config.xml中<mapper resource="com/jefry/UserMapper.xml"/>配置项,我们在包com.jefry下面创建文件UserMapper.xml
内容如下:
[html] view plain copy
- <?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="User">
- <!--resultType 表示com.jefry.User这样一个实体对象 -->
- <select id="selectUser" parameterType="int" resultType="com.jefry.User">
- select * from t_user where id = #{id}
- </select>
- </mapper>
编写测试代码
[java] view plain copy
- package com.jefry;
- import java.io.IOException;
- import java.io.InputStream;
- 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 {
- static String resource = "mybatis-config.xml";
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- InputStream inputStream = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- SqlSession session = sqlSessionFactory.openSession();
- try {
- //User.selectUser中User对应UserMapper.xml文件中标签<mapper namespace="User"> namespace的值
- //selectUser 对应UserMapper.xml文件中标签<select id="selectUser" parameterType="int" resultType="com.jefry.User">中id值
- User user = session.selectOne("User.selectUser", 1);
- System.out.println("user.getName()=" + user.getName());
- } finally {
- session.close();
- }
- }
- }
通过运行就可以结果:user.getName()=ssssssssssssssss
时间: 2024-10-13 19:42:51