码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis入门

时间:2014-10-20 18:54:41      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   java   for   

1,本文采用的Mybatis框架版本为: mybatis-3.0.4.jar 
2, 以持久化一个简单的User bean为例 
User实体类的代码如下: 



Java代码 复制代码 收藏代码
1.package bean;   
2.   
3.public class User {   
4.    private String name;   
5.    private Integer age;   
6.   
7.    public String getName() {   
8.       return name;   
9.    }   
10.    public void setName(String name) {   
11.       this.name = name;   
12.    }   
13.    public Integer getAge() {   
14.       return age;   
15.    }   
16.    public void setAge(Integer age) {   
17.       this.age = age;   
18.    }   
19.    public User(String name, Integer age) {   
20.       super();   
21.       this.name = name;   
22.       this.age = age;   
23.    }   
24.   
25.    public User() {   
26.       super();   
27.    }  //必须要有这个无参构造方法,不然根据UserMapper.xml中的配置,在查询数据库时,将不能反射构造出User实例   
28.}  


User实体的映射器取名为UserMapper(接口),其代码如下 



Java代码 复制代码 收藏代码
1.package Mapper;   
2.import bean.User;   
3.   
4.public interface UserMapper {   
5.    public void insertUser(User user);   
6.    public User getUser(String name);   
7.}  


注意,该接口类似于Hibernate中的DAO层接口,唯一不同的是,MyBatis中,只声明该接口就可以,不需要去实现. 
UserMapper映射器对应的配置文件如下(相当于Hibernate中的User.hbm.xml,当然这里面不是属性映射字段,而是直接是映射器中各种方法对应的sql语句): 



Xml代码 复制代码 收藏代码
1.<?xml version="1.0" encoding="UTF-8" ?>   
2.<!DOCTYPE mapper   
3.    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   
4.    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">   
5.<mapper namespace="Mapper.UserMapper">   
6.<!-- 这里namespace必须是UserMapper接口的路径,不然要运行的时候要报错 “is not known to the MapperRegistry”-->   
7.    <insert id="insertUser" parameterType="User">   
8.       insert into vincent_user(name,age) values(#{name},#{age})   
9.       <!-- 这里sql结尾不能加分号,否则报“ORA-00911”的错误 -->   
10.    </insert>   
11.   
12.    <!-- 这里的id必须和UserMapper接口中的接口方法名相同,不然运行的时候也要报错 -->   
13.    <select id="getUser" resultType="User" parameterType="java.lang.String">   
14.        select * from vincent_user where name=#{name}   
15.    </select>   
16.</mapper>  


MyBatis的配置文件这里取名为mybatis-config.xml其配置代码如下(相当与Hibernate的hibernate.cfg.xml): 



Xml代码 复制代码 收藏代码
1.<?xml version="1.0" encoding="UTF-8"?>   
2.<!DOCTYPE configuration   
3.    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"   
4.    "http://mybatis.org/dtd/mybatis-3-config.dtd">   
5.   
6.<configuration>   
7.    <settings>   
8.        <!-- changes from the defaults for testing -->   
9.        <setting name="cacheEnabled" value="false" />   
10.        <setting name="useGeneratedKeys" value="true" />   
11.        <setting name="defaultExecutorType" value="REUSE" />   
12.    </settings>   
13.    <typeAliases>   
14.       <typeAlias alias="User" type="bean.User"/>   
15.    </typeAliases>   
16.    <environments default="development">   
17.       <environment id="development">   
18.           <transactionManager type="jdbc"/>   
19.           <dataSource type="POOLED">   
20.              <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>   
21.              <property name="url" value="jdbc:oracle:thin:@192.168.56.201:1521:system"/>   
22.              <property name="username" value="vincent"/>   
23.              <property name="password" value="1234"/>   
24.           </dataSource>   
25.       </environment>   
26.    </environments>   
27.    <mappers>   
28.        <mapper resource="resource/UserMapper.xml" />   
29.    </mappers>   
30.</configuration>  



加载配置文件得到sqlSessionFactory的工具类 



Java代码 复制代码 收藏代码
1.package Util;   
2.   
3.import java.io.IOException;   
4.import java.io.Reader;   
5.   
6.import org.apache.ibatis.io.Resources;   
7.import org.apache.ibatis.session.SqlSessionFactory;   
8.import org.apache.ibatis.session.SqlSessionFactoryBuilder;   
9.   
10.public class MyBatisUtil  {   
11.    private  final static SqlSessionFactory sqlSessionFactory;   
12.    static {   
13.       String resource = "resource/mybatis-config.xml";   
14.       Reader reader = null;   
15.       try {   
16.           reader = Resources.getResourceAsReader(resource);   
17.       } catch (IOException e) {   
18.           System.out.println(e.getMessage());   
19.   
20.       }   
21.       sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);   
22.    }   
23.   
24.    public static SqlSessionFactory getSqlSessionFactory() {   
25.       return sqlSessionFactory;   
26.    }   
27.}  



测试代码如下: 




Java代码 复制代码 收藏代码
1.package TestMapper;   
2.import org.apache.ibatis.session.SqlSession;   
3.import org.apache.ibatis.session.SqlSessionFactory;   
4.import org.junit.Test;   
5.   
6.public class TestMapper {   
7.    static SqlSessionFactory sqlSessionFactory = null;   
8.    static {   
9.       sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();   
10.    }   
11.   
12.    @Test   
13.    public void testAdd() {   
14.       SqlSession sqlSession = sqlSessionFactory.openSession();   
15.       try {   
16.           UserMapper userMapper = sqlSession.getMapper(UserMapper.class);   
17.           User user = new User("tom",new Integer(5));   
18.           userMapper.insertUser(user);   
19.           sqlSession.commit();//这里一定要提交,不然数据进不去数据库中   
20.       } finally {   
21.           sqlSession.close();   
22.       }   
23.    }   
24.   
25.    @Test   
26.    public void getUser() {   
27.       SqlSession sqlSession = sqlSessionFactory.openSession();   
28.       try {   
29.           UserMapper userMapper = sqlSession.getMapper(UserMapper.class);   
30.           User user = userMapper.getUser("jun");   
31.           System.out.println("name: "+user.getName()+"|age: "+user.getAge());   
32.       } finally {   
33.           sqlSession.close();   
34.       }   
35.    }   
36.   
37.}  

-----------------------------------------------------------------------------------------------------------

来源于:http://yancc.iteye.com/blog/983815

mybatis入门

标签:style   blog   http   color   io   os   ar   java   for   

原文地址:http://www.cnblogs.com/dingyingsi/p/4037921.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!