一个小例子来看下MyBatis的运行过程:
1、添加mybatis依赖包
在github上有下载:https://github.com/mybatis/mybatis-3/releases
2、mybatis 配置文件
最核心的配置,有这些,就足够运转了。
<?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> <!-- 尽管可以配置多个环境,每个 SqlSessionFactory 实例只能选择其一 每个数据库对应一个 SqlSessionFactory 实例 --> <environments default="development"> <environment id="development"> <!-- 如果你正在使用 Spring + MyBatis,则没有必要配置事务管理器, 因为 Spring 模块会使用自带的管理器来覆盖前面的配置。 --> <transactionManager type="JDBC"></transactionManager> <!-- UNPOOLED:无连接池 POOLED:有连接池 JNDI:集中或者在外部配置数据源 --> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="conf/sql/userMapper.xml" /> <mapper resource="conf/sql/classMapper.xml"/> <mapper resource="conf/sql/userDMapper.xml"/> <mapper resource="conf/sql/userPMapper.xml"/> </mappers> </configuration>
3、sql数据映射文件
一个简单的SQL映射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"> <!-- 1、完全限定名(比如“com.mypackage.MyMapper.selectAllThings”)将被直接查找并且找到即用。 2、短名称(比如“selectAllThings”)如果全局唯一也可以作为一个单独的引用。 如果不唯一,有两个或两个以上的相同名称(比如“com.foo.selectAllThings ”和“com.bar.selectAllThings”), 那么使用时就会收到错误报告说短名称是不唯一的,这种情况下就必须使用完全限定名。 --> <mapper namespace="org.mybatis.UserMapper"> <select id="selectUser" resultType="User"> select * from user </select> <select id="selectUserById" resultType="User" parameterType="int"> select * from user where id = #{id} </select> <!-- useGeneratedKeys="true" keyProperty="id" 在mysql环境下,可以返回主键 --> <insert id="saveUser" useGeneratedKeys="true" keyProperty="id" parameterType="User"> insert into user(name,sex,age) values(#{name},#{sex},#{age}) </insert> <update id="updateUser" parameterType="User"> update user set name = #{name}, sex = #{sex},age = #{age} where id = #{id} </update> <delete id="deleteUser" parameterType="int"> delete from user where user.id = #{id} </delete> </mapper>
4、获取sqlSessionFactory,以及SqlSession.
获取SqlSessionFactory有两种方式,这里我们先来看第一种,以XML配置文件来创建sqlSessionFactory
/** * 使用XML配置文件获取sqlsession * @return */ public static SqlSession createSqlSessionByXML(){ //获取config.xml文件 String resource = "conf/config.xml"; InputStream input = null; try { input = Resources.getResourceAsStream(resource); //获得sqlsession SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(input); session = factory.openSession(); return session; } catch (IOException e) { e.printStackTrace(); return null; } }
5、执行映射SQL
在这里,使用junit4来测试执行方法,如下:
@Test public void queryUsersByXML(){ //使用xml配置执行 try{ session = SessionUtil.createSqlSessionByXML(); //使用mapper.xml文件执行 User user = session.selectOne("org.mybatis.UserMapper.selectUserById",1); }finally{ session.close(); } }