30 Star 72 Fork 26

闲.大赋(李家智) / dao-benchmark

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
UserDao.java 4.17 KB
一键复制 编辑 原始数据 按行查看 历史
lijiazhi 提交于 2021-08-12 21:10 . 更新到最新代码
package com.ibeetl.dao.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.ibeetl.dao.beetlsql.entity.BeetlSqlUser;
@Repository
public class UserDao {
@Autowired
DataSource ds = null;
public void addUser(BeetlSqlUser user) throws SQLException {
Connection conn = null;
try {
conn = this.getConn();
PreparedStatement ps = conn.prepareStatement("insert into sys_user (id,code) values (?,?)");
ps.setInt(1, user.getId());
ps.setString(2, user.getCode());
ps.executeUpdate();
ps.close();
conn.commit();
} finally {
this.closeConn(conn);
}
}
public BeetlSqlUser unqiue(Integer id) throws SQLException {
Connection conn = null;
BeetlSqlUser user = null;
try {
conn = this.getConn();
PreparedStatement ps = conn.prepareStatement("select id,code from sys_user where id=?");
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
if (user != null) {
// 检查唯一性
throw new RuntimeException("find more");
}
user = new BeetlSqlUser();
user.setId(rs.getInt("id"));
user.setCode(rs.getString("code"));
}
} finally {
this.closeConn(conn);
}
return user;
}
public void updateById(BeetlSqlUser user) throws SQLException {
Connection conn = null;
try {
conn = this.getConn();
PreparedStatement ps = conn.prepareStatement("update sys_user set code=? where id=?");
ps.setString(1, user.getCode());
ps.setInt(2, user.getId());
ps.executeUpdate();
conn.commit();
} finally {
this.closeConn(conn);
}
}
public List<BeetlSqlUser> pageQuery(String code) throws SQLException {
Connection conn = null;
try {
conn = this.getConn();
StringBuilder countSql = new StringBuilder("select count(1) from sys_user where 1=1 ");
if (StringUtils.isNotEmpty(code)) {
countSql.append(" and code=? ");
}
int i = 1;
PreparedStatement countPs = conn.prepareStatement(countSql.toString());
if (StringUtils.isNotEmpty(code)) {
countPs.setString(i++, code);
}
ResultSet countRs = countPs.executeQuery();
countRs.next();
int count = countRs.getInt(1);
StringBuilder sql = new StringBuilder("select * from sys_user where 1=1 ");
if (StringUtils.isNotEmpty(code)) {
sql.append(" and code=? ");
}
// 设置翻页
sql.append(" limit ?,? ");
PreparedStatement ps = conn.prepareStatement(sql.toString());
i = 1;
if (StringUtils.isNotEmpty(code)) {
ps.setString(i++, code);
}
// 翻页
ps.setInt(i++, 1);
ps.setInt(i++, 10);
ResultSet rs = ps.executeQuery();
List<BeetlSqlUser> list = new ArrayList<>(10);
while (rs.next()) {
BeetlSqlUser user = new BeetlSqlUser();
user.setId(rs.getInt("id"));
user.setCode(rs.getString("code"));
list.add(user);
}
return list;
} finally {
this.closeConn(conn);
}
}
protected Connection getConn() throws SQLException {
return ds.getConnection();
}
protected void closeConn(Connection conn) throws SQLException {
if (conn != null) {
conn.close();
}
}
}
Java
1
https://gitee.com/xiandafu/dao-benchmark.git
git@gitee.com:xiandafu/dao-benchmark.git
xiandafu
dao-benchmark
dao-benchmark
master

搜索帮助