sql的分页语句为(第一个参数为当前页面的索引起始位置,第二个参数为页面的展示个数)
select * from user limit #{startIndex},#{pageSize};
# startIndex : 起始位置 ,默认是0开始
# pageSize :页面大小
# 如何计算出当前页面索引起始位置
# currentPage = (currentPage-1)* pageSize
//查询全部用户实现分页
List<User> selectUserByLimit(Map<String,Integer> map);
<select id="selectUserByLimit" parameterType="Map" resultType="User">
select * from mybatis.user limit #{startIndex},#{pageSize}
select>
package com.MLXH.utils;
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 java.io.IOException;
import java.io.InputStream;
//mybatis的工具类,重复的代码的提纯
public class MyBatisUtils {
//类变量不需要设置默认值;
private static SqlSessionFactory sqlSessionFactory;
static {
//在maven中,所有的资源文件一般都放在resources目录下,我们可以直接拿到。
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//设置SqlSessionFactory公共的方法
public static SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
//获得一个带事务自动提交功能的SqlSession公共的方法
public static SqlSession getSqlSession(){
//自动提交事务
return sqlSessionFactory.openSession(true);
}
}
@Test
public void selectUserByLimit(){
//创建sqlSession
SqlSessionFactory sqlSessionFactory = MyBatisUtils.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
//准备数据
int currentPage = 2;//当前是第几页
int pageSize = 2; //页面大小
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("startIndex",(currentPage-1)*pageSize);
map.put("pageSize",pageSize);
//测试
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<User> users = mapper.selectUserByLimit(map);
for (User user : users) {
System.out.println(user);
}
sqlSession.close();//关闭连接
}
//查询全部用户实现分页使用RowBounds
List<User> selectUserByRowBounds();
2.写Mapper映射文件
<select id="selectUserByRowBounds" resultType="User">
select * from mybatis.user
select>
3.工具类同上(只是为了获取sqlSession对象简单而已)
4. 编写测试代码
@Test
public void selectUserByRowBounds(){
//创建sqlSession
SqlSessionFactory sqlSessionFactory = MyBatisUtils.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
int currentPage = 2; //当前页
int pageSize = 2; //页面大小
RowBounds rowBounds = new RowBounds((currentPage - 1) * pageSize, pageSize);
//注意点;使用RowBounds就不能使用getMapper了
//selectList: 接收一个List
//selectMap: 接收一个Map
//selectOne : 接收只有一个对象的时候
List<User> users = sqlSession.selectList("com.MLXH.dao.UserDao.selectUserByRowBounds", null, rowBounds);
for (User user : users) {
System.out.println(user);
}
}
根据之前写的这篇博客,链接: 使用Mybatis实现数据库的增删改查实例.(内有详细的代码和目录布局)
可以发现,对UserDao接口,我们需要创建一个userMapper.xml来实现这个接口,并在其内部写入sql语句
接下来我们使用注解来简化其代码操作
package com.MLXH.dao;
import com.MLXH.pojo.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface UserDao {
//查询全部用户
@Select("select * from user")
List<User> getUserList();
//通过ID查询用户
@Select("select * from user where id = #{id}")
User getUserById(@Param("id") int id);
//添加用户
@Insert("insert into user(id,name,pwd) values (#{id},#{name},#{pwd})")
int addUser(User user);
//修改用户信息
@Update("update user set name = #{name}, pwd = #{pwd} where id = #{id}")
int updateUser(User user);
//删除用户
@Delete("delete from user where id =#{uid}")
int deleteUser(@Param("uid") int id);
}
UserDao
package com.MLXH.dao;
import com.MLXH.pojo.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface UserDao {
//查询全部用户
@Select("select * from user")
List<User> getUserList();
//通过ID查询用户
@Select("select * from user where id = #{id}")
User getUserById(@Param("id") int id);
//添加用户
@Insert("insert into user(id,name,pwd) values (#{id},#{name},#{pwd})")
int addUser(User user);
//修改用户信息
@Update("update user set name = #{name}, pwd = #{pwd} where id = #{id}")
int updateUser(User user);
//删除用户
@Delete("delete from user where id =#{uid}")
int deleteUser(@Param("uid") int id);
}
@Param为参数注解,当所传入的参数为一个时,默认为Param,但当传入的参数为多个时,每一个参数前都要加入@Param来标识其是一个参数
例如:int update(@Param("id") int id,@Param("name") String name,@Param("pwd") String pwd)
Mybatis核心配置文件
<configuration>
<properties resource="database.properties"/>
<settings>
<setting name="logImpl" value="LOG4J"/>
settings>
<typeAliases>
<package name="com.MLXH.pojo"/>
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
dataSource>
environment>
environments>
<mappers>
<mapper class="com.MLXH.dao.UserDao"/>
mappers>
configuration>
这里我们将创建sqlSession对象封装为一个工具方法,并设置了事务的自动提交(sqlSessionFactory.openSession(true))
工具类
package com.MLXH.utils;
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 java.io.IOException;
import java.io.InputStream;
//mybatis的工具类,重复的代码的提纯
public class MyBatisUtils {
//类变量不需要设置默认值;
private static SqlSessionFactory sqlSessionFactory;
static {
//在maven中,所有的资源文件一般都放在resources目录下,我们可以直接拿到。
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//设置SqlSessionFactory公共的方法
public static SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
//获得一个带事务自动提交功能的SqlSession公共的方法
public static SqlSession getSqlSession(){
//自动提交事务
return sqlSessionFactory.openSession(true);
}
}
测试
package com.MLXH.dao;
import com.MLXH.pojo.User;
import com.MLXH.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import sun.rmi.server.UnicastServerRef;
import java.util.List;
public class UserDaoTest {
@Test
public void getUserList(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();//带自动提交事务
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();//关闭sqlSession;
}
@Test
public void getUserById(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();//带自动提交事务
UserDao mapper = sqlSession.getMapper(UserDao.class);
User user = mapper.getUserById(1);
System.out.println(user);
sqlSession.close();//关闭sqlSession;
}
@Test
public void addUser(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();//带自动提交事务
UserDao mapper = sqlSession.getMapper(UserDao.class);
User user = new User(5, "寒雪", "hanxue");
int i = mapper.addUser(user);
System.out.println(i);
sqlSession.close();//关闭sqlSession;
}
@Test
public void updateUser(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();//带自动提交事务
UserDao mapper = sqlSession.getMapper(UserDao.class);
User user = new User(5, "暮凌寒雪", "mulinghanxue");
int i = mapper.updateUser(user);
System.out.println(i);
sqlSession.close();//关闭sqlSession;
}
@Test
public void deleteUser(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();//带自动提交事务
UserDao mapper = sqlSession.getMapper(UserDao.class);
int i = mapper.deleteUser(5);
System.out.println(i);
sqlSession.close();//关闭sqlSession;
}
}
总结:可以看到,注解开发其实就是将接口的mapper实现文件封装,并在接口方法上添加了sql语句…