当前位置:编程学习 > JAVA >>

Mybatils框架

Mybatils框架
1.什么是Mybatis
Mybatis是对JDBC的封装,它隐藏了具体的JDBC的API,它把SQL语句放到了配置文件中,它能自动把不同的输入数据映射到SQL语句的动态参数上,它能自动把SQL语句的执行结果映射为JAVA对象…… MyBatis是一个持久化层的框架!
Mybatis是一个ORM框架
可以说Mybatis是一个半自动的ORM框架,Hibernate是全自动的
2.
 
配置文件(SqlMapConfig.xml)
基本作用就是配置JDBC连接的有关信息,比如URL、用户名、密码等等
如:
<?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>
    <typeAliases>
       <typeAlias type="cn.com.leadfar.cms.backend.model.Admin" alias="Admin"/>
    </typeAliases>
    <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/cms" />
              <property name="username" value="root" />
              <property name="password" value="mysql" />
           </dataSource>
       </environment>
    </environments>
    <mappers>
       <mapper resource="cn/com/leadfar/cms/backend/dao/impl/AdminMapper.xml" />
    </mappers>
</configuration>
 
映射文件
 
基本作用就是编写SQL语句,同时给每个SQL语句定义一个唯一标识(ID),在程序中通过此ID来调用这条SQL语句。
 
<?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="cn.com.leadfar.cms.backend.model.Admin">
    <insert id="add" parameterType="Admin">
       insert into t_admin (username,password) values (#{username},#{password})
    </insert>
</mapper>
 
程序代码
 
       //创建SqlSession的工厂!
       SqlSessionFactory factory = null;
       try {
           //通过配置文件,创建工厂对象
           Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
           factory = new SqlSessionFactoryBuilder().build(reader);
       } catch (IOException e1) {
           e1.printStackTrace();
       }
     
       //打开一个session
       SqlSession session = factory.openSession();
     
       try {
           //插入
           session.insert("cn.com.leadfar.cms.backend.model.Admin.add", admin);
         
           //提交事务
           session.commit();
         
       } catch (Exception e) {
           e.printStackTrace();
           session.rollback();
       } finally{
           //关闭session
           session.close();
       }
 
 3.两种xml文件的配置
(1)
<?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">
<!-- 整个唯一标识sql的id为namespace+id  org.leadfar.mybatis.Person.insert-->
<mapper namespace="org.leadfar.Student">
<insert id="insert"  parameterType="Student">
insert into t_user (id,name,age,birthday,money)values(seq_user_id.nextval,#{name},#{age} ,#{birthday} ,#{money})
</insert>
<update id="update"  parameterType="Student">
update t_user  set name=#{name},age=#{age},birthday=#{birthday},money=#{money} where id=#{id}
</update>
<delete id="delete"  parameterType="Student">
delete t_user  where id=#{id}
</delete>
<select id="selectone"  parameterType="int" resultType="Student">
select *  from t_user  where id=#{id}
</select>
</mapper>
(2)
<?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>
    <!-- 别名 -->
    <typeAliases>
       <typeAlias type="org.leadfar.Student" alias="Student"/>
    </typeAliases>
    <!-- 配置数据库连接信息 -->
    <environments default="development">
       <environment id="development">
           <transactionManager type="JDBC" />
           <dataSource type="POOLED">
              <property name="driver" value="oracle.jdbc.driver.OracleDriver" />
              <property name="url" value="jdbc:oracle:thin:@localhost:1521:leadfar" />
              <property name="u
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,