您的位置:首页 > 其它

Mybatis

2015-08-27 16:45 357 查看
1.hibernate和mybatis区别
hibernate是对象关系映射框架,对象和表全部映射完成
mybatis是对象映射框架,半自动,sql是有开发人员写
优点:轻量级,灵活,开发人员控制sql的能力强,sql写在配置文件里,修改很方便。
缺点:兼容性差,做不到跨数据库

2.使用myBatis:
1.新建项目,导入架包
2.配置myBatis的核心配置文件
myBatis-cfg.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>
<!--<typeAliases>
<typeAlias type="" alias=""/>
</typeAliases>
-->
<environments default="hdjd">
<environment id="hdjd">
<!-- 当前事务归谁管理:JDBC管理,Managed由容器管理 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 由谁提供数据源:POOLED 由数据库连接池提供;unPOOLED由传统方式,每次请求都需要打开和关闭数据库;jndi提供 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/hibernate"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/ecjtu/dao/DepartmentDaoMapper.xml"/>//添加配置映射
</mappers>

</configuration>
3.编写实现类和接口
接口:
package org.ecjtu.dao;
public interface IDepartmentDao {
public int selectcount();
}
实现类:
public class DepartmentDaoimpl implements IDepartmentDao{
@Override
public int selectcount() {
// TODO Auto-generated method stub
SqlSession session=MyBatisUtil.getSession();
int i=session.selectOne("org.ecjtu.dao.IDepartmentDao.selectcount");
return i;
}
}

4.创建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=“接口名称”
根标签的每一个标签都代表接口中的每一个方法。
<select id="接口里的方法"
parameterType="参数类型"
resultType="返回值类型">
</select>
<mapper namespace="org.ecjtu.dao.IDepartmentDao">
【返回一个int】
<select id="selectcount" resultType="int">
<!-- sql语句,不能写分号 -->
select count(*) from department
</select>
【参数为int,返回值为Department】
<select id="sel_DeptByid" parameterType="int"
resultType="org.ecjtu.entity.Department">
select * from department where did=#{did}
</select>
【返回多个对象Department】
<select id="sel_all" resultMap="deptMap">
select * from department
</select>
<resultMap type="org.ecjtu.entity.Department" id="deptMap">
<id property="did" column="did"></id>
<result property="dname" column="dname"/>
<result property="loc" column="LOC"/>
</resultMap>
【插入语句】
<insert id="insert" parameterType="org.ecjtu.entity.Department"
useGeneratedKeys="true" keyProperty="did">
insert into Department(dname,loc) values(#{dname},#{loc})
</insert>
测试:
public static void t5(){
SqlSession session=MyBatisUtil.getSession();
Department dep=new Department();
dep.setDname("饺子军团");
dep.setLoc("2015-8-27");

// session.insert("org.ecjtu.dao.IDepartmentDao.insert",dep);
session.getMapper(IDepartmentDao.class).insert(dep);
try {
session.commit();
System.out.println("submit success!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
session.rollback();
System.out.println("failed");
}
MyBatisUtil.closeSession();
}

</mapper>
---------sql语句【sql操作的是表名和列名】
但是要求列名与表明相同,不同则通过as关键字来使一致。
5.对第二步进行修改
添加映射文件。
6.添加工具类并且修改工具类的myhabits的名称,使与自己的配置文件相同。

7.测试
public void test(){
/*1.
DepartmentDaoimpl depimpl=new DepartmentDaoimpl();
System.out.println(depimpl.selectcount());*/
//2.
SqlSession session=MyBatisUtil.getSession();
IDepartmentDao dao=session.getMapper(IDepartmentDao.class);
int i=dao.selectcount();
System.out.println(i);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: