mapper中的class文件中的方法名字和要对应的xml文件中的id要一致
mapper的xml中可以不指定传入参数类型 但是结果类型必须要写
静态SQL
多个参数模糊查询
///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映射名字可以随意设置
一个对象入参 对象的属性中有值
///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中的属性名一致 (默认参数绑定为对象的属性值)
需要在测试时将对象设置值 并传入对象
Customer customer2 = new Customer();
customer2.setUsername("a");
customer2.setJobs("teacher");
List<Customer> list=customerMapper.findCustomerByConditions2(customer2);
Map入参 (当有多个值来自不同的对象 就不能放到同一个对象中 这时候需要用到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>
//(默认参数绑定为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);
对于表的增删改统一的返回值为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();
动态SQL
##mapper中的Java文件
/*
* 动态查询
*/
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文件
/*
* 动态更新
*/
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();
网友评论