mysql
mysql-connector-java
5.1.47
org.mybatis
mybatis
3.5.4
int del(@Param("key") int id);
//取出
#{key}
insert into goods values
(#{list.id},#{list.name},#{list.price})
insert into goods values
(#{list.id},#{list.name},#{list.price})
前两种都是字符串拼接的,而mysql的底层逗号、分号实际拼接的是一个很长的字符串,其工作效率是很低的。
1、确定拦截的签名
2、实现拦截方法
3、配置和运行
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.*;
import java.sql.Statement;
import java.util.Properties;
/*
* Interceptor是apache下的接口
* @Intercepts:插件签名,告诉mybatis单线插件用来拦截那个对象的那个方法
* type:拦截的类或接口
* method:拦截的方法
* args:拦截被方法中的参数
* 注意:注解中{}表示数组
* */
@Intercepts({@Signature(type= ResultSetHandler.class,method ="handleResultSets",args = Statement.class)})
public class MyFirstInterceptor implements Interceptor {
//拦截目标对象的目标方法的
public Object intercept(Invocation invocation) throws Throwable {
System.out.println("拦截的对象 :"+invocation.getTarget());
System.out.println("拦截被方法 :"+invocation.getMethod());
System.out.println("拦截被方法中的参数 :"+invocation.getArgs());
Object object=invocation.proceed();
return object;
}
//包装目标对象,为目标对象创建代理对象的
public Object plugin(Object target) {
System.out.println("将要包装的对象 :"+target);
return Plugin.wrap(target,this);
}
//插件初始化的
public void setProperties(Properties properties) {
System.out.println("插件配置的初始化 :"+properties);
}
}
select *from `order` LIMIT 0,10 //0:从0开始;10:10行数据
package com.imooc.shop.utils;
// 分页实体
public class Pager {
// 1.当前页码
private int pageIndex;
// 2.一页需要展示多少条数据
private int pageSize = 3 ;
// 3.当前条件下总的数据量
private int totalCount ;
// 4.总共可以分多少页
private int totalPages ;
public int getPageIndex() {
// 取页码的时候,做一些判断
pageIndex = pageIndex <= 0 ? 1 : pageIndex ;
// 判断页码是否越界 了
pageIndex = pageIndex>= getTotalPages() ? getTotalPages(): pageIndex;
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPages() {
// 总页数
// 根据总数据量和每页最多展示多少来确定的
// 10条数据 3 -> 4页
// 9条数据 3 -> 3页
return (this.getTotalCount() -1 ) / this.getPageSize() + 1;
}
// 分页的第一个参数
public int getFirstParam(){
return (this.getPageIndex() -1)*this.getPageSize();
}
}
可见官网:添加链接描述
com.github.pagehelper
pagehelper
5.1.10
1、mybatis接口:
public interface orderDao {
int deleteByPrimaryKey(String id);
int insert(Order record);
int insertSelective(Order record);
Order selectByPrimaryKey(String id);
List selectByPrimaryKeylist();
int updateByPrimaryKeySelective(Order record);
int updateByPrimaryKey(Order record);
}
2、Mybatis配置接口:
id, gid, num, price, up_date, hair_date, confirm_date, consignee, consignee_Tel,
consignee_address, state
delete from `order`
where id = #{id,jdbcType=CHAR}
insert into `order` (id, gid, num, price,
up_date, hair_date, confirm_date,
consignee, consignee_Tel, consignee_address,
state)
values (#{id,jdbcType=CHAR}, #{gid,jdbcType=CHAR}, #{num,jdbcType=INTEGER}, #{price,jdbcType=DOUBLE},
#{upDate,jdbcType=TIMESTAMP}, #{hairDate,jdbcType=TIMESTAMP}, #{confirmDate,jdbcType=TIMESTAMP},
#{consignee,jdbcType=VARCHAR}, #{consigneeTel,jdbcType=CHAR}, #{consigneeAddress,jdbcType=VARCHAR},
#{state,jdbcType=VARCHAR})
insert into `order`
id,
gid,
num,
price,
up_date,
hair_date,
confirm_date,
consignee,
consignee_Tel,
consignee_address,
state,
#{id,jdbcType=CHAR},
#{gid,jdbcType=CHAR},
#{num,jdbcType=INTEGER},
#{price,jdbcType=DOUBLE},
#{upDate,jdbcType=TIMESTAMP},
#{hairDate,jdbcType=TIMESTAMP},
#{confirmDate,jdbcType=TIMESTAMP},
#{consignee,jdbcType=VARCHAR},
#{consigneeTel,jdbcType=CHAR},
#{consigneeAddress,jdbcType=VARCHAR},
#{state,jdbcType=VARCHAR},
update `order`
gid = #{gid,jdbcType=CHAR},
num = #{num,jdbcType=INTEGER},
price = #{price,jdbcType=DOUBLE},
up_date = #{upDate,jdbcType=TIMESTAMP},
hair_date = #{hairDate,jdbcType=TIMESTAMP},
confirm_date = #{confirmDate,jdbcType=TIMESTAMP},
consignee = #{consignee,jdbcType=VARCHAR},
consignee_Tel = #{consigneeTel,jdbcType=CHAR},
consignee_address = #{consigneeAddress,jdbcType=VARCHAR},
state = #{state,jdbcType=VARCHAR},
where id = #{id,jdbcType=CHAR}
update `order`
set gid = #{gid,jdbcType=CHAR},
num = #{num,jdbcType=INTEGER},
price = #{price,jdbcType=DOUBLE},
up_date = #{upDate,jdbcType=TIMESTAMP},
hair_date = #{hairDate,jdbcType=TIMESTAMP},
confirm_date = #{confirmDate,jdbcType=TIMESTAMP},
consignee = #{consignee,jdbcType=VARCHAR},
consignee_Tel = #{consigneeTel,jdbcType=CHAR},
consignee_address = #{consigneeAddress,jdbcType=VARCHAR},
state = #{state,jdbcType=VARCHAR}
where id = #{id,jdbcType=CHAR}
3、Mybatis配置文件:
4、调用:
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ico.mybatis.bean.Order;
import ico.mybatis.orderDao;
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.Test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
public class TestUser {
//获取连接
public static SqlSessionFactory sqlSessionFactory = null;
public static SqlSessionFactory getSqlSessionFactory() {
if (sqlSessionFactory == null) {
String resource = "mybatis-config.xml";
try {
Reader reader = Resources.getResourceAsReader(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
return sqlSessionFactory;
}
@Test
public void testcollection(){
SqlSession sqlSession=this.getSqlSessionFactory().openSession();
orderDao daom=sqlSession.getMapper(orderDao.class);
Page