Mybatis进阶

Mybatis进阶

1.分页:

7.1使用limit分页

sql:

select * from user limit  0,1

Mybatis实现:

在Usermapper.xml下加入

<resultMap id="userbylimit" type="user">
    <result column="pwd" property="passwd"></result>
</resultMap>
<select id="getUserListlimit" parameterType="map" resultMap="userbylimit">
    select * from user limit  #{startIndex},#{pageIndex}
</select>

在UserMapper加入:

List<User> getUserListlimit(Map<String,Object> map);

测试:

@Test public void getUserInfolimit(){
   
    SqlSession sqlSession = Mybatisutils.getSqlSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("startIndex",1);
    map.put("pageIndex",2) ;
    List<User> userListlimit = mapper.getUserListlimit(map);
    for (User user : userListlimit) {
   
        System.out.println(user);
    }
    sqlSession.close();
}

7.2Rowbunds分页

麻烦不写了

2.注释开发

普通查个询:

1.UserMapper接口

public interface UserMapper {
   
    @Select("select * from user")
    List<User> getUserList();
}

2.mybatis-config.xml

<mappers>
    <mapper class="com.su.dao.UserMapper"></mapper>
</mappers>

3.test:

@Test
public void testV(){
   
    SqlSession sqlSession = Mybatisutils.getSqlSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    List<User> userList = mapper.getUserList();
    for (User user : userList) {
   
        System.out.println(user);
    }
    sqlSession.close();
}
有参数的查询

1.UserMapper接口

上面#{里的值}以Pararm括号里为准,多条件只用往后接着加@Param 即可

public interface UserMapper {
   
    @Select("select * from user where id = #{id} ")
    User getUserbyid(@Param("id") int nothisid);
    
    @Select("select * from user where id = #{id} and name = #{name} ")
    User getUserbyid(@Param("id") int nothisid,@Param("name") String nothisStr);
}

2.mybatis-config.xml

<mappers>
    <mapper class="com.su.dao.UserMapper"></mapper>
</mappers>

3.test:

@Test
public void testV(){
   
    SqlSession sqlSession = Mybatisutils.getSqlSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    List<User> userList = mapper.getUserList();
    for (User user : userList) {
   
        System.out.println(user);
    }
    sqlSession.close();
}
增:

1.UserMapper接口

public interface UserMapper {
   
    @Insert("insert into user(id,name,pwd)values(#{id},#{name},#{passwd})")
    int addUser(User user);
}

2.mybatis-config.xml

<mappers>
    <mapper class="com.su.dao.UserMapper"></mapper>
</mappers>

3.test:

    @Test
    public void adduserbyzs(){
   
        SqlSession sqlSession = Mybatisutils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int smw = mapper.addUser(new User(5,"smw","123456"));
        if (smw > 0){
   
            System.out.println("cg");
        }
        sqlSession.commit();
        sqlSession.close();

    }

4.小知识:

在Mybatisutils工具类中

public static SqlSession getSqlSession(){
   
    SqlSession sqlSession = sqlSessionFactory.openSession(true);
    return sqlSession;
}

将此处后面加个true

就不用在test中commit 会自动帮你提交

加完后test修改为

public void adduserbyzs(){
   
    SqlSession sqlSession = Mybatisutils.getSqlSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    int smw = mapper.addUser(new User(6,"smw2","123456"));
    if (smw > 0){
   
        System.out.println("cg");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值