public interface TestPaperTestsMapper {
int deleteByPrimaryKey(Integer testpaperTestsId);
int insert(TestPaperTests record);
int insertSelective(TestPaperTests record);
TestPaperTests selectByPrimaryKey(Integer testpaperTestsId);
int updateByPrimaryKeySelective(TestPaperTests record);
int updateByPrimaryKey(TestPaperTests record);
//根据试卷id查询试卷试题
List<TestPaperTestsVo> findSelectTestPaperQuesion(int testpaperId);
//根据试卷id删除试卷试题表信息
int deleteTestPaperTestById(int testpaperId);
//添加试卷试题
int addTestPaperQuestion(List<TestPaperTests> list);
}
id:唯一标识一个statement
#{}:表示一个占位符,如果#{}中传入简单类型的参数,#{}中的名称随意
parameterType:输入参数的类型,通过#{}接收parameterType输入的参数
resultType:输出结果类型,不管返回是多条还是单条,指定单条记录映射的pojo类型
${}
:表示sql的拼接,通过${}接收参数,将参数的内容不加任何修饰拼接在sql中。
parameterType:输入参数的类型,User对象包括username,birthday,sex,address
#{}接收pojo数据,可以使用OGNL解析出pojo的属性值
selectKey:用于进行主键返回,定义了获取主键值的sql
order:设置selectKey中sql执行的顺序,相对于insert语句来说
keyProperty:将主键值设置到哪个属性
resultType:select LAST_INSERT_ID()的结果类型
<mapper namespace="com.exam.dao.TestPaperMapper">
<resultMap id="BaseResultMap" type="com.exam.entity.TestPaper">
<id column="testpaper_id" jdbcType="INTEGER" property="testpaperId" />
<result column="testpaper_name" jdbcType="VARCHAR" property="testpaperName" />
<result column="testpaper_state" jdbcType="INTEGER" property="testpaperState" />
<result column="start_date" jdbcType="TIMESTAMP" property="startDate" />
<result column="end_date" jdbcType="TIMESTAMP" property="endDate" />
<result column="is_start" jdbcType="INTEGER" property="isStart" />
</resultMap>
<sql id="Base_Column_List">
testpaper_id, testpaper_name, testpaper_state, start_date, end_date
</sql>
查询操作
<select id="selectByPrimaryKey" parameterType="java.lang.Integer"
resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from testpaper
where testpaper_id = #{testpaperId,jdbcType=INTEGER}
</select>
删除操作
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from testpaper
where testpaper_id = #{testpaperId,jdbcType=INTEGER}
</delete>
插入操作
<insert id="insert" parameterType="com.exam.entity.TestPaper">
insert into testpaper (testpaper_id, testpaper_name, testpaper_state,
start_date, end_date)
values (#{testpaperId,jdbcType=INTEGER}, #{testpaperName,jdbcType=VARCHAR},
#{testpaperState,jdbcType=INTEGER},
#{startDate,jdbcType=TIMESTAMP}, #{endDate,jdbcType=TIMESTAMP})
</insert>
更新操作
<update id="updateByPrimaryKey" parameterType="com.exam.entity.TestPaper">
update testpaper
set testpaper_name = #{testpaperName,jdbcType=VARCHAR},
testpaper_state = #{testpaperState,jdbcType=INTEGER},
start_date = #{startDate,jdbcType=TIMESTAMP},
end_date = #{endDate,jdbcType=TIMESTAMP}
where testpaper_id = #{testpaperId,jdbcType=INTEGER}
</update>
Mybatis解决了jdbc编程的哪些问题
1、 数据库链接创建、释放频繁造成系统资源浪费从而影响系统性能,如果使用数据库链接池可解决此问题。
解决:在SqlMapConfig.xml中配置数据链接池,使用连接池管理数据库链接。
2、 Sql语句写在代码中造成代码不易维护,实际应用sql变化的可能较大,sql变动需要改变java代码。
解决:将Sql语句配置在XXXXmapper.xml文件中与java代码分离。
3、 向sql语句传参数麻烦,因为sql语句的where条件不一定,可能多也可能少,占位符需要和参数一一对应。
解决:Mybatis自动将java对象映射至sql语句,通过statement中的parameterType定义输入参数的类型。
4、 对结果集解析麻烦,sql变化导致解析代码变化,且解析前需要遍历,如果能将数据库记录封装成pojo对象解析比较方便。
解决:Mybatis自动将sql执行结果映射至java对象,通过statement中的resultType定义输出结果的类型。