MyBatis的框架配置与使用2
<ul class="article_tags clearfix csdn-tracking-statistics tracking-click" data-mod="popu_377" style="display: none;">
<li class="tit"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">标签:</font></font></li>
小编上一次写了MyBatis的查询,这一次接着后面的增加改。
首先我们新建一个项目,建好后如下图所示(具体细节步骤请看我的上篇文章):
删除增:
typeMapper.xml代码如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.t.dao.typeMapper">
<select id="selectType" resultType="com.t.entity.typeEntity">
select * from type
<if test="name!=null and name!=''">
where typename like concat('%',#{name},'%')
</if>
</select>
<!-- 删除 -->
<delete id="deleteType">
delete from type where tid = #{tid}
</delete>
<!-- 新增 -->
<insert id="addType" parameterType="com.t.entity.typeEntity">
insert into type values(null,#{typename},#{ftid})
</insert>
<!-- 修改 -->
<update id="updateType" parameterType="com.t.entity.typeEntity">
update type set where tid=#{tid} typename=#{typename},ftid=#{ftid}
</update>
</mapper>
- 1
- 2
- 3
- 4
- 五
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
typeMapper.java代码如下所示:
package com.t.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.t.entity.typeEntity;
public interface typeMapper {
//查询
public List<typeEntity> selectType(@Param("name") String name);
//删除
public int deleteType(@Param("tid")int tid);
//新增
public int addType(typeEntity t);
//修改
public int updateType(typeEntity t);
}
- 1
- 2
- 3
- 4
- 五
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
数据库如下图所示:
测试代码如下所示:
package com.t.test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.t.dao.typeMapper;
import com.t.entity.typeEntity;
import com.t.util.MyBatisUtil;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
//查询
List<typeEntity> tlist = MyBatisUtil.getSqlSession().getMapper(typeMapper.class).selectType("");
for (typeEntity t : tlist) {
System.out.println(t.getTypename());
}
//删除
//获取Session
SqlSession session = MyBatisUtil.getSqlSession();
//调用删除接口
int del =session.getMapper(typeMapper.class).deleteType(4);
//如果del>0就提交事务,小于0就回滚事务
if(del>0){
session.commit();
System.out.println("删除成功");
}else{
session.rollback();
}
//新增
//创建一个新的对象
typeEntity t = new typeEntity();
//赋值
t.setFtid(2);
t.setTypename("aaa");
int add = session.getMapper(typeMapper.class).addType(t);
//如果add>0就提交事务,小于0就回滚事务
if(add>0){
session.commit();
System.out.println("新增成功");
}else{
session.rollback();
}
//修改
//创建一个新的对象
typeEntity t1 = new typeEntity();
//赋值
t1.setTid(1);
t1.setFtid(10);
t1.setTypename("小编");
int upd = session.getMapper(typeMapper.class).updType(t1);
//如果upd>0就提交事务,小于0就回滚事务
if(upd>0){
session.commit();
System.out.println("修改成功");
}else{
session.rollback();
}
MyBatisUtil.closeSqlSession(session);
}
}
- 1
- 2
- 3
- 4
- 五
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 三十
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
运行效果:
把TID为4的记录删除,新增了一行记录,修改了TID为1的记录。
运行后数据库如下图所示:
这就是一个简单的控制台的增删了。
$(".MathJax").remove();