mybatis学习之路(三)mybatis实现的基本增删改查

①单条删除
一、配置文件中的SQL语句
<select id="findCustomerList" parameterType="long" resultMap="CustomerResult">
SELECT id,name,address,phone FROM customer
</select>
二、DAO层(mybatis需要自己提交事务)
public void deleteOne(int id){
try {
sqlSession = dbAccess.getSqlSession();
sqlSession.delete("Customer.deleteOne",id);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (sqlSession!=null){
sqlSession.close();
}
}
}
三、service层(略)


②多条删除
一、配置文件中的SQL语句
<delete id="deleteBatch" parameterType="List">
DELETE FROM customer WHERE id IN (
<foreach collection="list" item="item" separator=",">
#{item}
</foreach>
)
</delete>
二、DAO层(mybatis需要自己提交事务)
/**
* 多条删除
* @param list
*/
public void deleteBatch(List<Integer> list){
try {
sqlSession = dbAccess.getSqlSession();
sqlSession.delete("Customer.deleteBatch",list);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (sqlSession!=null){
sqlSession.close();
}
}
}
三、service层(略)


③单条新增
一、配置文件中的SQL语句
<insert id="insertCustomer" parameterType="algorithm.offer.pojo.Customer">
INSERT INTO customer
( id,
name,
address,
phone
)
VALUES
( #{id,jdbcType=INTEGER},
#{name,jdbcType=VARCHAR},
#{address,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR}
)
</insert>
二、DAO层(mybatis需要自己提交事务)
/**
*单条新增
* @param customer
*/
public void insertCustomer(Customer customer){
try {
sqlSession = dbAccess.getSqlSession();
sqlSession.insert("Customer.insertCustomer",customer);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (sqlSession!=null){
sqlSession.close();
}
}
}
三、service层(略)


④修改
一、配置文件中的SQL语句
<update id="updateCustomer" parameterType="algorithm.offer.pojo.Customer">
UPDATE customer SET
name = #{name,jdbcType=VARCHAR},
address = #{address,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR}
WHERE
id = #{id,jdbcType=INTEGER}
</update>
二、DAO层(mybatis需要自己提交事务)
/**
* 修改
* @param customer
*/
public void updateCustomer(Customer customer){
try {
sqlSession = dbAccess.getSqlSession();
sqlSession.update("Customer.updateCustomer",customer);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (sqlSession!=null){
sqlSession.close();
}
}
}
三、service层(略)

⑤联合查询
一、配置文件中的SQL语句
<select id="queryCustomer" parameterType="algorithm.offer.pojo.Customer" resultMap="CustomerResult">
SELECT id,name,address,phone FROM customer WHERE 1=1
<if test="name != null and !&quot;&quot;.equals(name.trim())">
AND name =#{name}
</if>
<if test="phone != null and !&quot;&quot;.equals(phone.trim())">
AND phone LIKE '%' #{phone} '%'
</if>
</select>
二、DAO层(mybatis需要自己提交事务)
/**
* 联合查询
* @return
*/
public List<Customer> queryCustomerList(String name,String phone){
List<Customer> customerList = new ArrayList<>();
Customer customer = new Customer();
customer.setName(name);
customer.setPhone(phone);
try {
sqlSession = dbAccess.getSqlSession();
customerList = sqlSession.selectList("Customer.queryCustomer",customer);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (sqlSession!=null){
sqlSession.close();
}
}

return customerList;
}
三、service层(略)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值