mybatis 概述
1.mybatis是一款优秀的数据持久层框架
2.mybatis是对jdbc功能进行轻量级的封装
①提供了统一的数据库信息配置,统一放在xml中进行读取
②将SQL语句提取到了一个xml文件中,提供动态SQL的功能
③提供了结果自动映射的封装,是一个orm来进行实现(将数据中的记录与java中的对象进行关系映射)
④对jdbc原生接口进行了封装,提供了一些mybatis自己的原生接口和类来进行实现
mybatis核心操作
参数传递
1.传递单个参数,就直接进行传参
2.传递多个参数时,则进行加入"@Param"标签进行绑定
3.但是一般传递多个参数时(两个以上),都会用一个对象进行传参,它会自动的为我们进行封装数据
示例如下:
//单个参数传递
Admin findAdminById(int id);
//两个参数传递
Admin login(@Param("acc") String account,@Param("pwd") String password);
//使用对象进行传递
Admin login1(Admin admin);
4.传递参数过程中对参数进行操作时有两种方式 #{} 和${}
这两种方式的区别
① #{} 是预编译状态,它会将SQL进行编译好后才取值进行操作,能够防止SQL注入
② ${} 是直接将传递参数的值进行拼接,取值后才进行编译,无法防止SQL注入
注意!!:
mybatis在进行排序时使用order by 动态参数时要使用 ${}
增加,删除,修改
1.增加功能
示例如下:
//测试类
@Test
public void save() {
//创建 SqlSession
SqlSession sqlSession = MybatisUtil.getSqlSession();
//获得接口代理对象
AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
Admin admin = new Admin();
admin.setAccount("whh");
admin.setPassword("22222");
adminDao.saveAdmin(admin);
//提交事务
sqlSession.commit();
//关闭
sqlSession.close();
}
//实现接口
public interface AdminDao {
void saveAdmin(Admin admin);
}
//进行SQL语句操作
//这是在XML文件中
<insert id="saveAdmin" parameterType="Admin" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
insert into admin (account,password) value(#{account},#{password})
</insert>
2.删除功能
示例如下:
//测试类
@Test
public void delete() {
//创建 SqlSession
SqlSession sqlSession = MybatisUtil.getSqlSession();
//获得接口代理对象
AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
adminDao.deleteAdmin(3);
//提交事务
sqlSession.commit();
//关闭
sqlSession.close();
}
//实现接口
public interface AdminDao {
void deleteAdmin(int id);
}
//进行SQL语句操作
//这是在XML文件中
<delete id="deleteAdmin" parameterType="Admin">
delete from admin where id=#{id}
</delete>
3.修改功能
示例如下:
//测试类
@Test
@Test
public void update() {
//创建 SqlSession
SqlSession sqlSession = MybatisUtil.getSqlSession();
//获得接口代理对象
AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
Admin admin = new Admin();
admin.setId(3);
admin.setAccount("xyj");
admin.setPassword("222");
adminDao.updateAdmin(admin);
//提交事务
sqlSession.commit();
//关闭
sqlSession.close();
}
//实现接口
public interface AdminDao {
void updateAdmin(Admin admin);
}
//进行SQL语句操作
//这是在XML文件中
<update id="updateAdmin" parameterType="Admin">
update admin set account=#{account},password=#{password} where id=#{id}
</update>
查询
直接查询
1.单表查询
@Test
public void findAdmins() {
//创建 SqlSession,用来每次与数据库会话使用,与数据库交互一次,就需要创建一个会话
SqlSession sqlSession = MybatisUtil.getSqlSession();
//接口代理对象,AdminDao.class拿到类被加载内存时的class对象
AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
//由代对象调用与接口中方法名相同的id的sql
List<Admin> admins = adminDao.findAdmins();
System.out.println(admins);
//关闭sqlSession
sqlSession.close();
}
//实现接口
public interface AdminDao {
List<Admin> findAdmins();
}
//进行SQL语句操作
//这是在XML文件中
<select id="findAdmins" resultType="Admin">
select id,account,password from admin
</select>
2.多表查询
关联对象中必须写resulrtMap
<!--直接查询-->
<resultMap id="studentMap" type="Student">
<id column="id" property="id"></id>
<result column="num" property="num"></result>
<result column="name" property="name"></result>
<result column="gender" property="gender"></result>
<result column="birthday" property="birthday"></result>
<result column="oper_time" property="operTime"></result>
<!--Dorm对象-->
<association property="dorm" javaType="Dorm">
<result column="dormNum" property="num"></result>
</association>
<!--Admin对象-->
<association property="admin" javaType="Admin">
<result column="account" property="account"></result>
</association>
</resultMap>
<select id="findStudentByID" parameterType="int" resultMap="studentMap">
SELECT
s.id,
s.num,
s.name,
s.gender,
s.birthday,
s.oper_time,
d.num dormNum,
a.account
FROM student s LEFT JOIN admin a ON s.adminid = a.id
LEFT JOIN dorm d ON s.dormid = d.id
where s.id = #{id}
</select>
嵌套查询
<!--嵌套查询-->
<resultMap id="studentMap1" type="Student">
<id column="id" property="id"></id>
<result column="num" property="num"></result>
<result column="name" property="name"></result>
<result column="gender" property="gender"></result>
<result column="birthday" property="birthday"></result>
<result column="oper_time" property="operTime"></result>
<!--宿舍对象-->
<association property="dorm" javaType="Dorm" column="dormid" select="findDormById"></association>
<!--管理员对象-->
<association property="admin" javaType="Admin" column="adminid" select="findAdminById"></association>
</resultMap>
<select id="findStudentByID1" parameterType="int" resultMap="studentMap1">
SELECT
s.id,
s.num,
s.name,
s.gender,
s.birthday,
s.dormid,
s.adminid,
s.oper_time
FROM student s
where id =#{id}
</select>
<!--单独查询宿舍对象-->
<select id="findDormById" resultType="Dorm">
SELECT num FROM dorm WHERE id=#{dormid}
</select>
<!--单独查询管理员对象-->
<select id="findAdminById" resultType="Admin">
SELECT account FROM admin WHERE id=#{adminid}
</select>
mybatis动态SQL
动态SQL进行操作
<!--查询SQL-->
<select id="findStudents" resultType="Student">
select * from student
<trim prefix="where" prefixOverrides="and|or">
<if test="num!=null">
and num = #{num}
</if>
<if test="name!=null">
and name = #{name}
</if>
<if test="gender!=null">
and gender = #{gender}
</if>
</trim>
</select>
<!--修改语句-->
<update id="updateStudent">
update student
<set>
<choose>
<when test="num!=null">
num=#{num},
</when>
<when test="name!=null">
name=#{name},
</when>
<otherwise>
gender=#{gender}
</otherwise>
</choose>
</set>
where id = #{id}
</update>
<!--删除语句-->
<delete id="deleteStudents">
delete from student where id in
<foreach collection="list" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</delete>