首页 > 数据库 > MySQL > 正文

mysql三种批量增加的性能分析

2024-07-24 12:49:08
字体:
来源:转载
供稿:网友
下面把代码写出来,希望大家批评指正.
首先domain对象.在这里使用的注解的方式,都是比较新的版本.
User.java
复制代码 代码如下:

package com.bao.sample.s3h4.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import com.bao.sample.base.domain.BaseDomain;
@Entity
@Table(name = "t_user")
public class User extends BaseDomain {
private static final long serialVersionUID = 1L;
private int id;
private String username;
private String password;
/**
* @Description 注解最好标记在get方法上.注意:采用一致的标记方式,注解是以Id的标记方式为准的,如果标记在get方法上,则忽略property上的注解.
* @return
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(nullable = false)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(nullable = false)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User() {
super();
}
public User(int id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
}

接下来是Dao接口,继承一个BaseDao接口.
复制代码 代码如下:

package com.bao.sample.s3h4.dao;
import java.util.List;
import com.bao.sample.base.dao.BaseDao;
import com.bao.sample.s3h4.domain.User;
public interface UserBatchDao extends BaseDao<User> {
/**
* @Description 批量增加操作
* @return -1:操作失败;0:执行正常;>0:执行成功的数目
*/
public int batchAddUsingJdbc(List<User> users);
public int batchAddUsingHibernate(List<User> users);
public int batchAddUsingJdbcTemplate(List<User> users);
}

UserBatchDao的实现:
复制代码 代码如下:

UserBatchDaoImpl
package com.bao.sample.s3h4.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate4.SessionFactoryUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.bao.sample.base.dao.BaseDaoImpl;
import com.bao.sample.s3h4.domain.User;
/**
*
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表