java大渣渣 通过本文主要向大家介绍了mybatis自动生成代码,mybatis代码生成器,mybatis逆向工程代码,mybatis代码生成工具,mybatis 代码生成等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
本文为大家分享了mybatis分页效果展示的具体代码,供大家参考,具体内容如下
mybatis版本3.4以下
结构:

spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 自动扫描加载注解的包 --> <context:component-scan base-package="com.ij34.bean"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"></property> <property name="suffix" value=".jsp" ></property> </bean> </beans></div>
com.ij34.mybatis
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd" default-autowire="byName" default-lazy-init="false"> <!-- Showcase's CustomFreemarkerManager example --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/mybatis"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:com/ij34/mybatis/mybatis-config.xml"></property> <property name="mapperLocations" value="classpath:com/ij34/mybatis/UserMapper.xml"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ij34.model"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> </beans></div>
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> <typeAliases> <typeAlias type="com.ij34.model.Article" alias="Article"/> <typeAlias type="com.ij34.model.User" alias="User"/> <typeAlias type="com.ij34.pages.PageInfo" alias="PageInfo"/> </typeAliases> <plugins> <plugin interceptor="com.ij34.pages.PagePlugin"> <property name="dialect" value="mysql" /> <property name="pageSqlId" value=".*ListPage.*" /> </plugin> </plugins> </configuration></div>
UserMapper.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="com.ij34.model.UserMapper">
<resultMap type="Article" id="resultAticleList">
<id property="id" column="aid"/>
<result property="title" column="title"/>
<result property="content" column="content"/>
<association property="user" javaType="User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</association>
</resultMap>
<select id="selectarticle" parameterType="int" resultMap="resultAticleList">
select users.id,users.name,users.age,article.id aid,article.title,article.content from users,article
where users.id=article.userid and users.id=#{id}
</select>
<select id="ListPage" resultMap="resultAticleList">
select users.id,users.name,users.age,article.id aid,article.title,article.content from users,article
where users.id=article.userid and users.id=#{userid}
</select>
</mapper>
</div>
com.ij34.model
User.java
package com.ij34.model;
public class User {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
</div>
Article.java
package com.ij34.model;
public class Article {
private int id;
private User user;
private String title;
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
</div>
UserMapper.java
package com.ij34.model;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.ij34.pages.PageInfo;
public interface UserMapper {
public List<Article> selectarticle(int id);
public List<Article> ListPage(@Param("page") PageInfo page,@Param("userid") int userid);
}
</div>
com.ij34.pages
参考网上的分页插件
PageInfo.java
package com.ij34.pages;
import java.io.Serializable;
public class PageInfo implements Serializable {
private static final long serialVersionUID = 587754556498974978L;
//pagesize ,每一页显示多少
private int showCount = 9;
//总页数
private int totalPage;
//总记录数
private int totalResult;
//当前页数
private int currentPage;
//当前显示到的ID, 在mysql limit 中就是第一个参数.
private int currentResult;
private String sortField;
private String order;
public int getShowCount() {
return showCount;
}
pu

