mybatis简单运用(添删改查)

mybatis-config.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
	PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
	<settings>
		<!--set lazy fetching-->
		<setting name="lazyLoadingEnabled" value="false" />
	</settings>
	
	
	<typeAliases>
		<typeAlias alias="trialNote" type="cn.com.chnsys.rcs.caseinfo.pojo.TrialNote"/>
	</typeAliases>
	
	<mappers>
		<mapper resource="ibatis/caseinfo/trialnote.xml"/>
	</mappers>
	
</configuration>

trialnote.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="cn.com.chnsys.rcs.caseinfo.dao.TrialNoteDao">

 <resultMap id="trialNoteResult" type="cn.com.chnsys.rcs.caseinfo.pojo.TrialNote">
  <result property="noteId" column="tsblid"></result>
  <result property="fileName" column="wjm"></result>
  <result property="ftpUrl" column="ftpurl"></result>
  <result property="vodInfoId" column="dbxxid"></result>
 </resultMap>
 
 <sql id="t_tsbl_columns">tsblid,wjm,ftpurl,dbxxid</sql>
 
 <select id="getTrialNote" resultMap="trialNoteResult" parameterType="string">
  SELECT <include refid="t_tsbl_columns" />
  FROM t_tsbl 
  WHERE dbxxid = #{vodInfoId}
 </select>

 <insert id="insertTrialNote" parameterType="trialNote">
   INSERT INTO t_tsbl (tsblid
  <if test="fileName != null">
    ,wjm
  </if>
  <if test="ftpUrl != null">
    ,ftpurl
  </if>
  <if test="vodInfoId != null">
    ,dbxxid
  </if>
    )VALUES ( #{noteId}
  <if test="fileName != null">
     ,#{fileName}
  </if>
  <if test="ftpUrl">
     ,#{ftpUrl}
  </if>
  <if test="vodInfoId != null">
     ,#{vodInfoId}
  </if>
    )
 </insert>
 
 <update id="updatetTrialNote" parameterType="trialNote">
        UPDATE t_tsbl
  <set>
   <if test="fileName != null">wjm = #{fileName},</if>
   <if test="ftpUrl != null">ftpurl = #{ftpUrl},</if>
   <if test="vodInfoId != null">dbxxid = #{vodInfoId}</if>
  </set>
    WHERE tsblid = #{noteId}
 </update>
 
 <delete id="deleteTrialNote" parameterType="string">
        DELETE FROM t_tsbl WHERE dbxxid = #{vodInfoId}
 </delete>
</mapper>

 

与ibatis2.0不同的地方在于paramter的别名位置只能出现在config配置文件下,并且paramClass变为paramType,结果集同理。还有一点mybatis框架不再需要实现DAO接口,直接
<delete id="deleteTrialNote" parameterType="string">把其中的id项对应DAO接口方法即可。

MyBatis-Test 是一个用于 MyBatis 测试的工具库,它提供了一些实用的测试工具类和方法,可以方便地进行 MyBatis 单元测试。下面是 MyBatis-Test 的增删改示例: 1. 加数据 ``` @Test public void testInsert() throws SQLException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession sqlSession = sqlSessionFactory.openSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = new User(); user.setName("Lucy"); user.setAge(18); user.setGender(1); userMapper.insert(user); sqlSession.commit(); } finally { sqlSession.close(); } } ``` 2. 询数据 ``` @Test public void testSelect() throws SQLException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession sqlSession = sqlSessionFactory.openSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> userList = userMapper.getAllUsers(); for (User user : userList) { System.out.println(user); } } finally { sqlSession.close(); } } ``` 3. 更新数据 ``` @Test public void testUpdate() throws SQLException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession sqlSession = sqlSessionFactory.openSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.getUserById(1L); user.setName("Tom"); user.setAge(20); user.setGender(0); userMapper.update(user); sqlSession.commit(); } finally { sqlSession.close(); } } ``` 4. 删除数据 ``` @Test public void testDelete() throws SQLException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession sqlSession = sqlSessionFactory.openSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); userMapper.delete(1L); sqlSession.commit(); } finally { sqlSession.close(); } } ``` 以上是 MyBatis-Test 的基本增删改操作示例,具体实现还需根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值