搭建好mybatis之后 進行對數據庫的操作
添加語句
在映射文件中添加語句
<mapper namespace="com.wn.dao.StudentDao">
<insert id="insertStudent" parameterType="StudentBean">
insert into student(name,age,score) values(#{name},#{age},#{score})
</insert>
映射文件要放在與接口一個目錄下
namespace:必須是對應接口的全限定名
id :dao中的方法名字
parameterType:傳入的參數類型 可以省略
添加語句后 獲取主鍵的值 賦值給主鍵
<insert id="insertStudentGetId" parameterType="StudentBean">
insert into student(name,age,score) values(#{name},#{age},#{score})
<!-- keyProperty:查詢結果賦值給studentbean的id屬性
resultType:返回結果的數據類型
order:執行順序 mysql在數據添加后
-->
<selectKey keyProperty="id" resultType="int" order="AFTER">
select @@identity
</selectKey>
</insert>
2.刪除語句
<update id="updateStudent" >
update student set name=#{name} ,age=#{age},score=#{score}
where id = #{id}
</update>
3.查詢語句
/**
* 靜態參數
* @param student
*/
void insertStudent(StudentBean student);
List<StudentBean> selectStudentAll();
//根據姓名模糊查詢
List<StudentBean> selectStudentByName(String name);
//多參數查詢 使用map作為方法參數
List<StudentBean> selectStuByMap1(Map<String, Object> map);
List<StudentBean> selectStuByMap2(Map<String, Object> map);
List<StudentBean> selectStuByParameters1(String name , int age);
List<StudentBean> selectStuByParameters2(String name , StudentBean student);
List<StudentBean> selectStuByParameters3(@Param("name") String name , @Param("age")int age);
List<StudentBean> selectStuByParameters4(@Param("name") String name , @Param("student") StudentBean student);
<select id="selectStudentAll" resultType="StudentBean">
select * from student
</select>
<select id="selectStudentByName" resultType="StudentBean">
select * from student where name like '%' #{name} '%'
</select>
<select id="selectStuByMap1" resultType="StudentBean">
<!-- 查詢的參數是Map #{map的key} 使用的數據是key對應的value-->
select * from student where name like '%' #{name} '%' and age>#{age}
</select>
<select id="selectStuByMap2" resultType="StudentBean">
<!-- 查詢的參數是Map #{map的key} 使用的數據是key對應的value
map的key是student,value是student對象-->
select * from student where name like '%' #{name} '%' and age > #{student.age}
</select>
<select id="selectStuByParameters1" resultType="StudentBean">
select * from student where name like '%' #{0} '%' and age > #{1}
</select>
<select id="selectStuByParameters2" resultType="StudentBean">
select * from student where name like '%' #{0} '%' and age > #{1.age}
</select>
動態參數
/**
* 動態參數
*/
//mybatis 動態參數類似域jstl《c:》
//if拼接 sql語句要跟上 where 1 =1
List<StudentBean> selectStudentByIf(StudentBean student);
<select id="selectStudentByIf" resultType="StudentBean">
select * from student where 1=1
<if test="name!=null and name!=''">
and name like '%' #{name} '%'
</if>
<if test="age>0">
and age > #{age}
</if>
</select>
//不生成 1= 1 提高效率 自動在sql語句拼接的時候加上where 關鍵字
List<StudentBean> selectStudentByWhere(StudentBean student);
<select id="selectStudentByWhere" resultType="StudentBean">
select * from student
<where>
<if test="name!=null and name!=''">
and name like '%' #{name} '%'
</if>
<if test="age>0">
and age > #{age}
</if>
</where>
</select>
//多選一
List<StudentBean> selectStudentByChoose(StudentBean student);
<select id="selectStudentByChoose" resultType="StudentBean">
select * from student
<choose>
<when test="name!=null and name!=''">
where name like '%' #{name} '%'
</when>
<when test="age>0">
where age > #{age}
</when>
<otherwise>
where 1 = 2
</otherwise>
</choose>
</select>
List<StudentBean> selectStudentByForeachArray(int[] ids);
<select id="selectStudentByForeachArray" resultType="StudentBean">
select * from student
<if test="array!=null and array.length>0">
where id in
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</if>
</select>
List<StudentBean> selectStudentByForeachList(List<Integer> ids);
<select id="selectStudentByForeachList" resultType="StudentBean">
select * from student
<if test="list!=null and list.size>0">
where id in
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</if>
</select>
List<StudentBean> selectStudentByForeachStudent(List<StudentBean> students);
<select id="selectStudentByForeachStudent" resultType="StudentBean">
select * from student
<if test="list!=null and list.size>0">
where id in
<foreach collection="list" item="student" open="(" close=")" separator=",">
#{student.id}
</foreach>
</if>
</select>
List<StudentBean> selectStudentBySqlFragement();
<select id="selectStudentBySqlFragement" resultType="StudentBean">
select <include refid="fragement" /> student
</select>
<sql id="fragement"> * from</sql>
//統計一張表的總數據條數 分頁的總條數
int selectStudentCount();
<select id="selectStudentCount" resultType="int">
select count(*) from student
</select>
<!-- 動態參數 -->!
本站文章版權歸原作者及原出處所有 。內容為作者個人觀點, 并不代表本站贊同其觀點和對其真實性負責,本站只提供參考并不構成任何投資及應用建議。本站是一個個人學習交流的平臺,網站上部分文章為轉載,并不用于任何商業目的,我們已經盡可能的對作者和來源進行了通告,但是能力有限或疏忽,造成漏登,請及時聯系我們,我們將根據著作權人的要求,立即更正或者刪除有關內容。本站擁有對此聲明的最終解釋權。