MyBatis基本的增删改查操作,有图有真相,项目截图[MyBatis-0200]
项目依赖及表参阅
[user.xml]
[ IUserMapper.java ]id ,account ,password ,nickname ,email ,birthday ,status ,registed_time insert into tbl_user (account ,password ,nickname ,gender ,email ,status ,registed_time) values (#{account},#{password},#{nickname},#{gender},#{email},#{status},#{registedTime}) update tbl_user set nickname = #{nickname} , email = #{email} , status = #{status} , birthday = #{birthday} where id = #{id} delete from tbl_user where id = #{id}
package net.yeah.likun_zhang.mapper;import java.util.List;import net.yeah.likun_zhang.model.User;/** * 用户表映射接口 * @author ZhangLiKun * @mail likun_zhang@yeah.net * @date 2013-3-31 */public interface IUserMapper { User get(Long id) ; List[测试用例]listByAccount(String account) ; int save(User user) ; void update(User user) ; void delete(Long id) ; }
package net.yeah.likun_zhang.example;import static org.junit.Assert.assertEquals;import static org.junit.Assert.assertNotNull;import static org.junit.Assert.assertNull;import static org.junit.Assert.assertTrue;import java.io.IOException;import java.io.InputStream;import java.util.Calendar;import java.util.List;import net.yeah.likun_zhang.mapper.IUserMapper;import net.yeah.likun_zhang.model.User;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.After;import org.junit.Before;import org.junit.Test;public class SqlSessionTest { private SqlSessionFactory sessionFactory ; private SqlSession session ; private IUserMapper um ; @Before public void setUp() throws IOException { String resource = "mybatis.xml"; InputStream in = Resources.getResourceAsStream(resource); sessionFactory = new SqlSessionFactoryBuilder().build(in); session = sessionFactory.openSession(); um = session.getMapper(IUserMapper.class); } @Test public void testSelectOne() { User user = um.get(1L); assertNotNull(user); assertEquals("admin", user.getAccount()); } @Test public void testSelectList() { List【 】users = um.listByAccount("admin") ; assertNotNull(users); assertEquals("admin", users.get(0).getAccount()); } @Test public void testInsert() { User user = new User() ; user.setAccount("jack") ; user.setPassword("123456") ; user.setNickname("jack") ; user.setEmail("jack@yeah.net") ; user.setGender(0) ; user.setStatus(0) ; user.setRegistedTime(Calendar.getInstance().getTime()) ; int result = um.save(user) ; assertEquals(1 ,result) ; // 返回的是插入记录条数而非自增长ID assertTrue(user.getId() > 0) ; // 自增长ID被set到要保存对象中去了,类似于JPA的persist方法 } @Test public void testUpdate() { User user = um.get(1L) ; user.setStatus(0) ; um.update(user) ; user = um.get(1L) ; // 重新获取User对象 assertEquals("admin", user.getAccount()) ; assertEquals(0 ,user.getStatus()) ; } @Test public void testDelete() { Long id = 15L ; assertNotNull(um.get(id)) ; um.delete(id) ; assertNull(um.get(id)) ; } @After public void tearDown() {// session.commit() ; // 需手动提交事务,否则数据不会被写入数据库中,增删改测试需提交事务 session.close() ; } }