一、概念
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录。
二、配置mybatis的配置文件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>
<!-- jdbc的属性文件 -->
<properties resource="jdbc.props"/>
<settings>
<!-- 日志文件 -->
<setting name="logImpl" value="LOG4J"/>
<!-- 懒加载 -->
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
<!-- 给实体类起别名 默认是实体类的类名 -->
<typeAliases>
<package name="org.lanqiao.entity"/>
</typeAliases>
<!--环境默认是开发环境-->
<environments default="development">
<environment id="development">
<!--事务管理-->
<transactionManager type="JDBC"/>
<!--数据源-->
<dataSource type="POOLED">
<!--连接数据库,从jdbc属性文件获取连接的信息-->
<property name="driver" value="${jdbc_driverClass}"/>
<property name="url" value="${jdbc_url}"/>
<property name="username" value="${jdbc_username}"/>
<property name="password" value="${jdbc_password}"/>
</dataSource>
</environment>
</environments>
<!--连接映射文件-->
<mappers>
<mapper resource="org/lanqiao/mapper/StudentMapper.xml"/>
<mapper resource="org/lanqiao/mapper/SgroupMapper.xml"/>
<mapper resource="org/lanqiao/mapper/CourseMapper.xml"/>
</mappers>
</configuration>
1.jdbc属性文件 jdbc.properties
jdbc_url=jdbc:mysql://localhost:3306/telecom
jdbc_driverClass=com.mysql.jdbc.Driver
jdbc_username=root
jdbc_password=123
2.日志文件 log4j.properties
log4j.rootLogger=DEBUG,std
log4j.appender.std=org.apache.log4j.ConsoleAppender
log4j.appender.std.layout=org.apache.log4j.SimpleLayout
3.映射文件
<?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="org.lanqiao.dao.StudentDao">
<sql id="columnList">sid,sname,sage,gid</sql>
<sql id="colummList1">sid,sname,sage</sql>
<!-- 防止属性名和表中的字段名不一致 -->
<resultMap type="Student" id="studentResultMap">
<id property="sid" column="sid"/>
<result property="sname" column="sname"/>
<result property="sage" column="sage"/>
</resultMap>
<!-- 一对一和一对多 -->
<resultMap type="Student" id="studentResultMap1">
<id property="sid" column="sid"/>
<result property="sname" column="sname"/>
<result property="sage" column="sage"/>
<association property="sgroup" column="gid" select="org.lanqiao.dao.SgroupDao.selectSgroupBygid"/>
<collection property="course" column="sid" select="org.lanqiao.dao.CourseDao.selectCourseByCid"/>
</resultMap>
<!-- 查询 -->
<select id="selectStudentAll" resultMap="studentResultMap">
select <include refid="columnList"/> from student
</select>
<!-- 添加 -->
<insert id="insertStudent" keyProperty="sid" useGeneratedKeys="true">
insert into student(sid,sname,sage) values(null,#{sname},#{sage})
</insert>
<!-- 修改
Set (1)set会在指定位置添加set
(2)自动把最后一个条件逗号","去掉
(3)当所有条件都不满足,就不会添加set -->
<update id="updateStudentBySid">
update student
<set>
<if test="sname !=''and sname!=null ">
sname=#{sname},
</if>
<if test="sage!=null">
sage=#{sage},
</if>
</set>
where sid=#{sid}
</update>
<!-- 一对一查询和一对多查询 -->
<select id="selectStudent" resultMap="studentResultMap1">
select <include refid="columnList"/> from student
</select>
<!-- 模糊查询 -->
<select id="selectStudentBySname" resultMap="studentResultMap1">
select <include refid="columnList"/> from student where sname like concat('%',#{sname},'%')
</select>
<!-- 条件查询 -->
<select id="selectStudentByCondition" resultMap="studentResultMap1">
select * from student
<where>
<if test="sid!=null">
and sid=#{sid}
</if>
<if test="sname!='' and sname!=null">
and sname like concat('%',#{sname},'%')
</if>
<if test="sage!=null">
and sage =#{sage}
</if>
</where>
</select>
<!-- 批量查询 -->
<select id="selectStudentByMany" resultMap="studentResultMap1">
<!-- select * from student where sid in(2,8,9) -->
select <include refid="columnList"/> from student where sid in
<foreach collection="list" item="item" open="(" close=")" separator=",">
#{item.sid}
</foreach>
</select>
<!-- 批量插入 -->
<insert id="insertStudentMany">
<!-- insert into student(sid,sage,sname) values(null,"王1",22),(null,"王2",23) -->
insert into student (sid,sname,sage) values
<foreach collection="list" item="item" separator=",">
(null,#{item.sname},#{item.sage})
</foreach>
</insert>
</mapper>