mapper中的class文件中的方法名字和要對應(yīng)的xml文件中的id要一致
mapper的xml中可以不指定傳入?yún)?shù)類型 但是結(jié)果類型必須要寫
靜態(tài)SQL
多個參數(shù)模糊查詢
///mapper中的Java文件代碼j
public List<Customer> findCustomerByConditions(@Param("name")String username,@Param("jobs")String job);
//mapper中的xml文件代碼
<select id="findCustomerByConditions" resultType="com.wk.po.Customer">
select * from t_customer where username like "%"#{username}"%" and jobs = #{job};
</select>
在mapper的Java文件中不允許寫同名的方法 (Mybatis判別不了)
@param映射名字可以隨意設(shè)置
一個對象入?yún)? 對象的屬性中有值
///mapper中的Java文件代碼
public List<Customer> findCustomerByConditions2(Customer customer);
##在mapper的xml文件中 可以不寫parameterType 上面的例子就沒有寫
<select id="findCustomerByConditions2" resultType="com.wk.po.Customer">
select * from t_customer where username like "%"#{username}"%" and jobs = #{jobs};
</select>
//這里{}里面的名字要和po中的屬性名一致 (默認參數(shù)綁定為對象的屬性值)
需要在測試時將對象設(shè)置值 并傳入對象
Customer customer2 = new Customer();
customer2.setUsername("a");
customer2.setJobs("teacher");
List<Customer> list=customerMapper.findCustomerByConditions2(customer2);
Map入?yún)? (當有多個值來自不同的對象 就不能放到同一個對象中 這時候需要用到map)
##mapper中的Java文件代碼
public List<Customer> findCustomerByConditions3(Map<String,Object> map);
##在mapper的xml文件中 可以不寫parameterType 上面的例子就沒有寫
<select id="findCustomerByConditions3" resultType="com.wk.po.Customer">
select * from t_customer where username like "%"#{username_key}"%" and jobs = #{jobs_key};
</select>
//(默認參數(shù)綁定為map集合中的key值)
##test中代碼
HashMap<String,Object> map = new HashMap<>();
map.put("username_key", "a");
map.put("jobs_key", "teacher");
List<Customer> list=customerMapper.findCustomerByConditions3(map);
對于表的增刪改統(tǒng)一的返回值為void
增加
##mapper中的Java文件代碼
/*
* insert
*/
public void insertCustomer(Customer c) throws SQLException;
##xml中的代碼
<insert id="insertCustomer">
insert into t_customer values(null,#{username},#{jobs},#{phone});
<selectKey keyProperty="id" order="AFTER" resultType="int">
<!---id指的是將insert之后的id值插入到customer類中的id屬性中 類型為int->
select last_insert_id();
<!-- SQL中select 和 from必須要 這個語句只是省略了from -->
</selectKey>
</insert>
##test中代碼
Customer c = new Customer();
c.setJobs("solds");
c.setUsername("牛逼");
c.setPhone("12333231");
try {
customerMapper.insertCustomer(c);
sqlSession.commit();
System.out.println(c.getId());
System.out.println(c.getId());
} catch (SQLException e) {
e.printStackTrace();
sqlSession.rollback();
}
sqlSession.close();
刪除
/*
* delete
*/
public void deleteCustomer(Integer id) throws SQLException;
##xml中的代碼
<delete id="deleteCustomer" parameterType="int">
delete from t_customer where id = #{id};
</delete>
##test中代碼
try {
customerMapper.deleteCustomer(5);
sqlSession.commit();
} catch (SQLException e) {
e.printStackTrace();
sqlSession.rollback();
}
sqlSession.close();
##xml中的代碼
<insert id="insertCustomer">
insert into t_customer values(null,#{username},#{jobs},#{phone});
<selectKey keyProperty="id" order="AFTER" resultType="int">
<!---id指的是將insert之后的id值插入到customer類中的id屬性中 類型為int->
select last_insert_id();
<!-- SQL中select 和 from必須要 這個語句只是省略了from -->
</selectKey>
</insert>
##test中代碼
Customer c = new Customer();
c.setJobs("solds");
c.setUsername("牛逼");
c.setPhone("12333231");
try {
customerMapper.insertCustomer(c);
sqlSession.commit();
System.out.println(c.getId());
System.out.println(c.getId());
} catch (SQLException e) {
e.printStackTrace();
sqlSession.rollback();
}
sqlSession.close();
更新
///mapper中的Java文件代碼
/*
* update
*/
public void updateCustomer(Customer c) throws SQLException;
##xml中的代碼
<update id="updateCustomer" parameterType="com.wk.po.Customer">
update t_customer set jobs = #{jobs} where id = #{id};
</update>
##test中代碼
Customer c = new Customer();
c.setId(4);
c.setJobs("2651616");
try {
customerMapper.updateCustomer(c);
sqlSession.commit();
} catch (SQLException e) {
e.printStackTrace();
sqlSession.rollback();
}
sqlSession.close();
動態(tài)SQL
##mapper中的Java文件
/*
* 動態(tài)查詢
*/
public List<Customer> getCustomerDynamic(Customer c) throws SQLException;
##mapper中的xml文件
<select id="getCustomerDynamic" resultType="com.wk.po.Customer">
select * from t_customer
<where>
<if test="id!=null">
id = #{id}
</if>
<if test="username!=null and username!=''">
and username = #{username}
</if>
</where>
order by username desc
</select>
##mapper中的Java文件
/*
* 動態(tài)更新
*/
public void updateCustomerDynamic(Customer c) throws SQLException;
##mapper中的xml文件
<update id="updateCustomerDynamic">
update t_customer
<set>
<if test="username!=null and username!=''">
username =#{username} ,
</if>
</set>
<set>
<if test="jobs!=null and jobs!=''">
jobs =#{jobs} ,
</if>
</set>
<set>
<if test="phone!=null and phone!=''">
phone =#{phone} ,
</if>
</set>
where id = #{id}
</update>
##test中代碼
CustomerMapper customerMapper=sqlSession.getMapper(CustomerMapper.class);
Customer c = new Customer();
c.setId(4);
c.setUsername("牛逼拉拉");
try {
customerMapper.updateCustomerDynamic(c);
sqlSession.commit();
} catch (SQLException e) {
e.printStackTrace();
sqlSession.rollback();
}
sqlSession.close();