Spring JdbcTemplate CRUD 조작
15270 단어 spring
/**
* Project Name:cjxy
* File Name:AdminDaoImpl.java
* Package Name:edu.bjfu.cjxy.daoimpl
* Date:2013 9 3 10:53:14
* Copyright (c) 2013, [email protected] All Rights Reserved.
*
*/
package edu.bjfu.cjxy.daoimpl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import edu.bjfu.cjxy.dao.AdminDao;
import edu.bjfu.cjxy.dao.BaseDao;
import edu.bjfu.cjxy.databean.SubAdminListRecord;
import edu.bjfu.cjxy.domain.Admin;
import edu.bjfu.cjxy.domain.SubstationAdmin;
import edu.bjfu.cjxy.utils.Page;
/**
* ClassName:AdminDaoImpl <br/>
* Function: . <br/>
* Reason: . <br/>
* Date: 2013 9 3 10:53:14 <br/>
* @author zhangzhaoyu
* @version
* @since JDK 1.6
* @see
*/
@Repository
public class AdminDaoImpl implements BaseDao<Admin>, AdminDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public Admin getAdminByUserId(String userId) {
String sql = " SELECT * FROM superadmin WHERE userId = ? ";
return jdbcTemplate.queryForObject(sql, new Object[]{userId}, new RowMapper<Admin>() {
@Override
public Admin mapRow(ResultSet rs, int index) throws SQLException {
Admin admin = new Admin();
admin.setId(rs.getInt("id"));
admin.setUserId(rs.getString("userId"));
admin.setPassword(rs.getString("password"));
admin.setType(rs.getString("type"));
admin.setPhone(rs.getString("phone"));
admin.setEmail(rs.getString("email"));
return admin;
}
});
}
@Override
public SubstationAdmin getSubstationAdminByUserId(String userId) {
String sql = " SELECT * FROM admin WHERE userId = ? ";
return jdbcTemplate.queryForObject(sql, new Object[]{userId}, new RowMapper<SubstationAdmin>(){
@Override
public SubstationAdmin mapRow(ResultSet rs, int index) throws SQLException {
SubstationAdmin subAdmin = new SubstationAdmin();
subAdmin.setId(rs.getInt("id"));
subAdmin.setUserId(rs.getString("userId"));
subAdmin.setPassword(rs.getString("password"));
subAdmin.setType(rs.getString("type"));
subAdmin.setBranchId(rs.getInt("branchId"));
subAdmin.setLeixing(rs.getInt("leixing"));
subAdmin.setPhone(rs.getString("phone"));
subAdmin.setEmail(rs.getString("email"));
return subAdmin;
}});
}
@Override
public Page<Admin> getAdminByPage(Page<Admin> page) {
int countOfsuperadmin = getTotalCountOfAdmin();
//
page.setTotalRecord(countOfsuperadmin);
String sql = "SELECT * FROM superadmin ORDER BY id LIMIT ?, ? ";
List<Admin> adminList = jdbcTemplate.query(
sql,
new Object[]{page.getOffset(),
page.getPageSize()},
new RowMapper<Admin>() {
@Override
public Admin mapRow(ResultSet rs, int index) throws SQLException {
Admin admin = new Admin();
admin.setId(rs.getInt("id"));
admin.setUserId(rs.getString("userId"));
admin.setPassword(rs.getString("password"));
admin.setType(rs.getString("type"));
admin.setPhone(rs.getString("phone"));
admin.setEmail(rs.getString("email"));
return admin;
}});
page.setDatas(adminList);
return page;
}
@Override
public int getTotalCountOfAdmin() {
return getTotalCount();
}
@Override
public Page<SubstationAdmin> getSubstationAdminByPage(Page<SubstationAdmin> page) {
int totalCountOfSubAdmin = getTotalCountOfSubstationAdmin();
//
page.setTotalRecord(totalCountOfSubAdmin);
String sql = "SELECT * FROM admin ORDER BY id LIMIT ?, ? ";
final List<SubstationAdmin> subAdminList = new ArrayList<SubstationAdmin>();
jdbcTemplate.query(sql, new Object[]{page.getOffset(), page.getPageSize()},
new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
SubstationAdmin subAdmin = new SubstationAdmin();
subAdmin.setId(rs.getInt("id"));
subAdmin.setUserId(rs.getString("userId"));
subAdmin.setPassword(rs.getString("password"));
subAdmin.setBranchId(rs.getInt("branchId"));
subAdmin.setType(rs.getString("type"));
subAdmin.setBranchId(rs.getInt("branchId"));
subAdmin.setLeixing(rs.getInt("leixing"));
subAdmin.setPhone(rs.getString("phone"));
subAdmin.setEmail(rs.getString("email"));
subAdminList.add(subAdmin);
}
});
page.setDatas(subAdminList);
return page;
}
@Override
public int getTotalCountOfSubstationAdmin() {
String sql = " SELECT count(*) FROM admin ";
return jdbcTemplate.queryForObject(sql, Integer.class);
}
@Override
public boolean insert(Admin obj) {
String sql = " INSERT INTO superadmin(userId, password, type, phone, email) VALUES(?, ?, ?, ?, ?)";
Object[] args = new Object[]{
obj.getUserId(),
obj.getPassword(),
obj.getType(),
obj.getPhone(),
obj.getEmail()
};
int[] argTypes = new int[] {
java.sql.Types.VARCHAR,
java.sql.Types.VARCHAR,
java.sql.Types.VARCHAR,
java.sql.Types.VARCHAR,
java.sql.Types.VARCHAR
};
return jdbcTemplate.update(sql, args, argTypes) > 0;
}
@Override
public boolean delete(Admin obj) {
String sql = null;
Object[] args = null;
if (obj.getId() != null) {
sql = " DELETE FROM superadmin WHERE id = ? ";
args = new Object[]{obj.getId()};
} else if(obj.getUserId() != null) {
sql = " DELETE FROM superadmin WHERE userId = ? ";
args = new Object[]{obj.getUserId()};
} else {
return false;
}
return jdbcTemplate.update(sql, args) > 0;
}
@Override
public boolean update(Admin obj) {
String sql = " UPDATE superadmin SET userId = ?, password = ?, type = ?, phone = ?, email = ? WHERE id = ? ";
Object[] args = new Object[]{
obj.getUserId(),
obj.getPassword(),
obj.getType(),
obj.getPhone(),
obj.getEmail(),
obj.getId()
};
int[] argTypes = new int[]{
java.sql.Types.VARCHAR,
java.sql.Types.VARCHAR,
java.sql.Types.VARCHAR,
java.sql.Types.VARCHAR,
java.sql.Types.VARCHAR,
java.sql.Types.INTEGER
};
return jdbcTemplate.update(sql, args, argTypes) > 0;
}
@Override
public Admin queryForObject(int id) {
String sql = " SELECT * FROM superadmin WHERE id = ? ";
return jdbcTemplate.queryForObject(sql, new Object[]{id},
BeanPropertyRowMapper.newInstance(Admin.class));
}
@Override
public int getTotalCount() {
String sql = " SELECT count(*) FROM superadmin ";
return jdbcTemplate.queryForObject(sql, Integer.class);
}
@Override
public Page<Admin> queryForDataByPage(Page<Admin> page, Admin obj) {
if (obj != null) {
String sql = " select count(*) from superadmin where userId like '%" + obj.getUserId() + "%'";
int totalCount = jdbcTemplate.queryForObject(sql, Integer.class);
page.setTotalRecord(totalCount);
sql = "SELECT * FROM superadmin where userId like '%?%' LIMIT ?, ? ";
Object[] args = new Object[]{
obj.getUserId(),
page.getOffset(),
page.getPageSize()
};
List<Admin> adminList = jdbcTemplate.query(sql, args, new RowMapper<Admin>() {
@Override
public Admin mapRow(ResultSet rs, int index) throws SQLException {
Admin admin = new Admin(rs.getInt("id"), rs.getString("userId"),
rs.getString("password"), rs.getString("type"));
admin.setPhone(rs.getString("phone"));
admin.setEmail(rs.getString("email"));
return admin;
}
});
page.setDatas(adminList);
return page;
}
return null;
}
@Override
public void executeSql(String sql) {
jdbcTemplate.execute(sql);
}
@Override
public boolean modifyAdminPassword(Admin admin) {
String sql = " UPDATE superadmin SET password = ? WHERE id = ? ";
return jdbcTemplate.update(sql, new Object[]{admin.getPassword(), admin.getId()},
new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER}) > 0;
}
@Override
public boolean modifySubAdminPassword(SubstationAdmin subadmin) {
String sql = " UPDATE admin SET password = ? WHERE id = ? ";
return jdbcTemplate.update(sql, new Object[]{subadmin.getPassword(), subadmin.getId()},
new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER}) > 0;
}
@Override
public Page<SubAdminListRecord> getSubstationAdminListRecordByPage(
Page<SubAdminListRecord> page, SubAdminListRecord sub) {
if (sub == null) {
int totalCount = getTotalCountOfSubstationAdmin();
page.setTotalRecord(totalCount);
String sql = " SELECT admin.id, admin.userId, admin.password, admin.branchId, admin.leixing, "
+ " admin.phone, admin.email, branchschool.name FROM admin, branchschool where "
+ " admin.branchId = branchschool.id ORDER BY id LIMIT ?, ? ";
List<SubAdminListRecord> datas = jdbcTemplate.query(sql, new Object[]{page.getOffset(), page.getPageSize()},new int[]{java.sql.Types.INTEGER, java.sql.Types.INTEGER},
new RowMapper<SubAdminListRecord>() {
@Override
public SubAdminListRecord mapRow(ResultSet rs, int rowNum) throws SQLException {
SubAdminListRecord data = new SubAdminListRecord();
data.setId(rs.getInt("id"));
data.setUserId(rs.getString("userId"));
data.setPassword(rs.getString("password"));
data.setLeixing(rs.getInt("leixing"));
data.setPhone(rs.getString("phone"));
data.setEmail(rs.getString("email"));
data.setBranchSchoolName(rs.getString("name"));
return data;
}
});
page.setDatas(datas);
return page;
}
return null;
}
@Override
public SubstationAdmin getSubstationAdminById(int subAdminId) {
String sql = " SELECT * FROM admin WHERE id = ? ";
return jdbcTemplate.queryForObject(sql, new Object[]{subAdminId}, new RowMapper<SubstationAdmin>(){
@Override
public SubstationAdmin mapRow(ResultSet rs, int index) throws SQLException {
SubstationAdmin subAdmin = new SubstationAdmin();
subAdmin.setId(rs.getInt("id"));
subAdmin.setUserId(rs.getString("userId"));
subAdmin.setPassword(rs.getString("password"));
subAdmin.setType(rs.getString("type"));
subAdmin.setBranchId(rs.getInt("branchId"));
subAdmin.setLeixing(rs.getInt("leixing"));
subAdmin.setPhone(rs.getString("phone"));
subAdmin.setEmail(rs.getString("email"));
return subAdmin;
}});
}
@Override
public boolean insertSubAdmin(SubstationAdmin subAdmin) {
String sql = " INSERT INTO admin(userId, password, type, branchId, leixing, phone, email) VALUES(?, ?, ?, ?, ?, ?, ?)";
Object[] args = new Object[]{
subAdmin.getUserId(),
subAdmin.getPassword(),
subAdmin.getType(),
subAdmin.getBranchId(),
subAdmin.getLeixing(),
subAdmin.getPhone(),
subAdmin.getEmail()
};
return jdbcTemplate.update(sql, args) > 0;
}
@Override
public boolean deleteSubAdmin(SubstationAdmin subAdmin) {
String sql = "DELETE FROM admin WHERE id = ? ";
return jdbcTemplate.update(sql, new Object[]{subAdmin.getId()},
new int[]{java.sql.Types.INTEGER}) > 0;
}
@Override
public boolean updateSubAdmin(SubstationAdmin subAdmin) {
String sql = "UPDATE admin SET userId = ?, password = ?, type = ?, branchId = ?, "
+ " leixing = ?, phone = ?, email = ? WHERE id = ? ";
Object[] args = new Object[]{
subAdmin.getUserId(),
subAdmin.getPassword(),
subAdmin.getType(),
subAdmin.getBranchId(),
subAdmin.getLeixing(),
subAdmin.getPhone(),
subAdmin.getEmail(),
subAdmin.getId()
};
return jdbcTemplate.update(sql, args) > 0;
}
@Override
public int getNameNum(String userId) {
String sql = " SELECT count(*) FROM superadmin WHERE userId = '"+userId+"'";
return jdbcTemplate.queryForObject(sql, Integer.class);
}
@Override
public int getSubNameNum(String userId) {
String sql = " SELECT count(*) FROM admin WHERE userId = '"+userId+"'";
return jdbcTemplate.queryForObject(sql, Integer.class);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
thymeleaf로 HTML 페이지를 동적으로 만듭니다 (spring + gradle)지난번에는 에서 화면에 HTML을 표시했습니다. 이번에는 화면을 동적으로 움직여보고 싶기 때문에 입력한 문자를 화면에 표시시키고 싶습니다. 초보자의 비망록이므로 이상한 점 등 있으면 지적 받을 수 있으면 기쁩니다! ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.