- 编写接口
- 编写对应的Mapper.
- 测试(增删改需要提交事务)
<mapper namespace="com.perwrj.dao.UserMapper"><!-- 查询 id="重写的方法名" resultType="返回值类型(全类名)" --> <select id="getUserList" resultType="com.perwrj.pojo.User"> select * from mybatis.user </select></mapper>
- id:就是对应的namespace中的方法名
- resultType:sql语句的返回值类型
- parameterType:传入参数的类型
package com.perwrj.dao;import java.util.List;import com.perwrj.pojo.User;public interface UserMapper {// 查询全部用户 List<User> getUserList();// 根据id查询用户 User getUserById(int id);// 添加一个用户 int addUser(User user);// 修改用户 int updateUser(User user);// 删除用户 int deleteUser(int id);}
<?
package com.perwrj.dao;import java.util.List;import org.apache.ibatis.session.SqlSession;import org.junit.Test;import com.perwrj.pojo.User;import com.perwrj.utils.MyBatisUtil;public class UserMapperTest { @Test public void test() { //获得SqlSession对象, SqlSession sqlSession = MyBatisUtil.getSqlSession(); try {// 方式一:getMapper执行sql:更加简洁,类型更加安全,推荐使用 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> userList = userMapper.getUserList(); for (User user : userList) { System.out.println(user); } //方式一结束 // 方式二:老方法,不建议使用, /* * List<User> list = * sqlSession.selectList("com.perwrj.dao.UserMapper.getUserList"); * * for (User object : list) { System.out.println(object); } */ //方式二结束 }catch(Throwable e) { e.printStackTrace(); }finally {// 关闭sqlSession sqlSession.close(); } } /** * 删除操作 */ @Test public void deleteUserTest() { SqlSession sqlSession = MyBatisUtil.getSqlSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); int num = userMapper.deleteUser(4); if(num > 0) {// 提交事务 System.out.println(num);//1 sqlSession.commit(); sqlSession.close(); } }catch(Throwable e) { e.printStackTrace(); }finally { sqlSession.close(); } } /** * 改操作 */ @Test public void updateUserTest() { SqlSession sqlSession = MyBatisUtil.getSqlSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); int num = userMapper.updateUser(new User(2,"杰瑞","987654")); if(num > 0) {// 提交事务 System.out.println(num);//1 sqlSession.commit(); sqlSession.close(); } }catch(Throwable e) { e.printStackTrace(); }finally { sqlSession.close(); } } /** * 增删改需要提交事务 */ @Test public void addUserTest() { SqlSession sqlSession = MyBatisUtil.getSqlSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); int num = userMapper.addUser(new User(4,"王瑞杰","123568")); if(num > 0) {// 提交事务 System.out.println(num);//1 sqlSession.commit(); sqlSession.close(); } }catch(Throwable e) { e.printStackTrace(); }finally { sqlSession.close(); } } @Test public void testGetUserById() { //获得SqlSession对象, SqlSession sqlSession = MyBatisUtil.getSqlSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.getUserById(1); System.out.println(user); }catch(Throwable e) { e.printStackTrace(); }finally { sqlSession.close(); } } }
原文转载:http://www.shaoqun.com/a/587592.html
执御:https://www.ikjzd.com/w/717.html
万国邮政联盟:https://www.ikjzd.com/w/861
编写接口编写对应的Mapper.编写接口编写对应的Mapper.测试(增删改需要提交事务)<mappernamespace="com.perwrj.dao.UserMapper"><!--查询id="重写的方法名"resultType="返回值类型(全类名)"--><selectid="getUserList"resultTyp
epa认证:https://www.ikjzd.com/w/1769
tenso:https://www.ikjzd.com/w/1552.html
派代:https://www.ikjzd.com/w/2197
虚拟信用卡:https://www.ikjzd.com/w/1055
跨境电商的物流服务专题培训会:https://www.kjyunke.com/courses/186
优质网红:https://www.ikjzd.com/tl/4207
没有评论:
发表评论