MyBatis环境搭建配置文件+入门视频下载

1、MyBatis优点
操作简单话,代码量少,效率高,成本就降低了
2、MyBatis缺点
参数只能限制为一个
selece语都要手动来写

3、与JDBC的关系:是对JDBC的扩展
把sql语句和java代码分离后,改了sql语句不用改动java代码

 

<!--SqlMapConfig.xml配置文件-->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!--导入数据库连接配置信息 -->
<properties resource="SqlMap.properties"></properties>
<!-- type="JDBC"表示使用jdbc 进行事务管理SIMPLE 使用简单的方式-->
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driver}"></property>
<property name="JDBC.ConnectionURL" value="${url}"></property>
<property name="JDBC.Username" value="${username}"></property>
<property name="JDBC.Password" value="${password}"></property>
</dataSource>
</transactionManager>

<!-- 映射文件 -->
<sqlMap resource="entity/Student.xml"/>

</sqlMapConfig>

 

<!--SqlMap.properties 文件-->

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:ORCL
username=scott
password=abc123

<!-- Student映射文件 -->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap>
<!-- 给操作的类取个别名 以便下面好用 -->
<typeAlias alias="Student" type="entity.Student"/>

<!--selectAllStudent相当于下面查询语句的一个别称 -->
<select id="selectAllStudent" resultClass="Student">
select * from Student
</select>

<!--parameterClass 传进来的参数类型 -->
<select id="selectStudentById" parameterClass="int" resultClass="Student">
select * from Student where sid=#sid#
</select>

<!--添加类型要一一对应,不然会类型转换报错 没有返回值类型就不用配置 resultClass属性-->
<insert id="insertStudent" parameterClass="Student" >
insert into Student(sid,sname,major,birth,score) values(#sid#,#sname#,#major#,#birth#,#score#)
</insert>

<!-- 中间的那个配置表示去查找序列 把序列的下一个值付给Strudent对象中的一个属性 -->
<insert id="insertStudentBySequence" parameterClass="Student">
<selectKey resultClass="int" keyProperty="sid">
  select seqenct_student.nextVal from dual
</selectKey>
  insert into Student(sid,sname,birth,major,score)
  values(#sid#,#sname#,#birth#,#major#,#score#)
</insert>
<!--删除对象-->
<delete id="deleteStudentById" parameterClass="int">
delete from Student where sid=#sid#
</delete>

<!--修改信息-->
<update id="updateStudentById" parameterClass="Student">
update Student
set sname=#sname#,
major=#major#,
birth=#birth#,
score=#score#
where sid=#sid#
</update>

<!-- 模糊查询 -->
<select id="selectStudentByName" parameterClass="String" resultClass="Student">
select sid,sname,major,birth,score from Student
where sname like '%$sname$%'
</select>
</sqlMap>

 

入门教学视频下载链接:http://pan.baidu.com/s/1laU4m

 

 

 

posted @ 2014-04-08 22:42  莫名字  阅读(300)  评论(0编辑  收藏  举报