1.反射
public class ObjectHelper {
List list=new ArrayList();
List l=new ArrayList();
//获取类方法
public static String[] getMethodName(Object obj){
Field[] fields=obj.getClass().getDeclaredFields();
String[] str=new String[fields.length];
for(int i=0;i<fields.length;i++)
{
str[i]=fields[i].getName();
}
return str;
}
//调用get方法
public Object getValueMethod(String methodname,Object obj) throws Exception{
Method method=obj.getClass().getDeclaredMethod(methodname);
Object o=method.invoke(obj);
System.out.println(o+"aaaaaaaaa");
return o;
}
//获取get方法名字且方法与sql语句的问号属性名一致
public List<String> getParamMethodName(String[] str,List l){
List l1=new ArrayList();
for(int i=0;i<l.size();i++){
for(int j=0;j<str.length;j++){
if(str[j].equals(l.get(i))){
/*String s = str[i].toUpperCase();*/
StringBuffer sb=new StringBuffer("get");
sb.append(str[j].toUpperCase().charAt(0)) ;
sb.append(str[j].substring(1));
System.out.println(sb.toString());
l1.add(sb.toString());
//System.out.println(l1.get(0)+"eeeee");
}
}
}
return l1;
}
//获取方法中的值
public List getValue(Object o,List l) throws Exception{
List l2=new ArrayList();
List l3=new ArrayList();
String[] str=this.getMethodName(o); //获取类方法名字
l2=this.getParamMethodName(str,l); // 获取get方法名字
for (int i=0;i<l2.size();i++) {
Object obj=this.getValueMethod((String)l2.get(i), o);
l3.add(obj);
}
return l3;
}
//强转
public Object[] powerChange(List m){
Object[] arr = new Object[m.size()] ;
for(int i=0;i<m.size();i++){
arr[i]=m.get(i);
System.out.println("强转"+m.get(i));
}
return arr;
}
public List findParam(String sql,Object o){
String[] str=this.getMethodName(o);
String[] s=sql.split(" ");
List t=new ArrayList();
for(int i=0;i<s.length;i++){
for(int j=0;j<str.length;j++){
if(s[i].equals(str[j])){
t.add(str[j]);
}
}
}
return t;
}
}
2.数据库操作
/**
*
*/
package com.clps.manage.tools;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* @author sue wong 2017年7月27日下午11:52:16 since v1.0
*/
public class DBTools {
private int i;
private PreparedStatement ps = null;
private Connection conn = null;
// 连接数据库
public Connection getConnection() throws Exception {
ReadProperties rp = new ReadProperties();
String filename = "database.properties";
String driver = rp.readProperties(filename, "jdbc.driverClassName");
String url = rp.readProperties(filename, "jdbc.url");
String username = rp.readProperties(filename, "jdbc.username");
String password = rp.readProperties(filename, "jdbc.password");
// 加载驱动
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
//
return conn;
}
// 关闭数据库
public void closeConnection(Connection conn) throws SQLException {
if (conn != null) {
conn.close();
}
}
// 添加修改删除数据
public int insertDB(String sql, Object[] object) throws Exception {
conn = this.getConnection();
ps = conn.prepareStatement(sql);
System.out.println("sssssssss");
if (object != null) {
for (int i = 0; i < object.length; i++) {
ps.setObject(i + 1, object[i]);
}
}
i = ps.executeUpdate();
this.closeConnection(conn);
return i;
}
//判断返回值
public boolean judgeFlag(int i){
if(i==1){
return true;
}else{
return false;
}
}
}
3.daoimpl
/**
*
*/
package com.clps.manage.daoimpl;
import java.io.IOException;
import java.util.List;
import com.clps.manage.dao.RoleDao;
import com.clps.manage.model.Role;
import com.clps.manage.tools.DBTools;
import com.clps.manage.tools.ObjectHelper;
import com.clps.manage.tools.ReadProperties;
/**
* @author sue wong
* 2017年7月28日上午2:14:21
* since v1.0
*/
public class RoleDaoImpl implements RoleDao {
DBTools db=new DBTools();
ReadProperties rp=new ReadProperties();
String sql;
String filename="sql.properties";
ObjectHelper oh=new ObjectHelper();
Object[] o;
List list;
List l;
/* (non-Javadoc)
* @see com.clps.manage.dao.RoleDao#addRole(com.clps.manage.model.Role)
*/
@Override
public boolean addRole(Role role) throws Exception {
// TODO Auto-generated method stub
sql=rp.readProperties(filename, "sql.addRole");
l=oh.findParam(sql, role);
list=oh.getValue(role,l);
o=oh.powerChange(list);
int i=db.insertDB(sql, o);
return false;
}
/* (non-Javadoc)
* @see com.clps.manage.dao.RoleDao#removeRole(com.clps.manage.model.Role)
*/
@Override
public boolean removeRole(Role role) throws Exception {
// TODO Auto-generated method stub
sql=rp.readProperties(filename, "sql.removeRole");
l=oh.findParam(sql, role);
list=oh.getValue(role,l);
o=oh.powerChange(list);
int i=db.insertDB(sql, o);
return false;
}
/* (non-Javadoc)
* @see com.clps.manage.dao.RoleDao#updateRole(com.clps.manage.model.Role)
*/
@Override
public boolean updateRole(Role role) throws Exception {
// TODO Auto-generated method stub
sql=rp.readProperties(filename, "sql.updateRole");
//System.out.println(sql);
l=oh.findParam(sql, role);
//System.out.println(l.size());
list=oh.getValue(role,l);
o=oh.powerChange(list);
int i=db.insertDB(sql, o);
return false;
}
}
4.model类
/**
*
*/
package com.clps.manage.model;
/**
* @author sue wong
* 2017年7月27日下午11:33:55
* since v1.0
*/
public class Role {
private Integer roleid;
private String username;
private String password;
/**
* @return the roleid
*/
public Integer getRoleid() {
return roleid;
}
/**
* @param roleid the roleid to set
*/
public void setRoleid(Integer roleid) {
this.roleid = roleid;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}
5.读取properties工具类
/**
*
*/
package com.clps.manage.tools;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @author sue wong
* 2017年7月20日下午10:51:46
* since v1.0
*/
public class ReadProperties {
//读取配置文件
public String readProperties(String filename,String propertiesname) throws IOException{
InputStream in=this.getClass().getClassLoader().getResourceAsStream(filename);
Properties prop=new Properties();
try{
prop.load(in);
}catch (IOException e) {
e.printStackTrace();
}
return prop.getProperty(propertiesname);
}
}
6.数据库properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/homework
jdbc.username=root
jdbc.password=123456
7.sql语句properties
sql.addRole=insert into role ( username , password ) values( ? , ? )
sql.removeRole=delete from role where roleid = ?
sql.updateRole=update role set password = ? where roleid = ?
8.测试类
/**
*
*/
package com.clps.manage.daoimpl;
import static org.junit.Assert.*;
import org.junit.Test;
import com.clps.manage.dao.RoleDao;
import com.clps.manage.model.Role;
/**
* @author sue wong
* 2017年7月28日上午2:22:37
* since v1.0
*/
public class RoleDaoImplTest {
RoleDao rd=new RoleDaoImpl();
Role role=new Role();
/**
* Test method for {@link com.clps.manage.daoimpl.RoleDaoImpl#addRole(com.clps.manage.model.Role)}.
* @throws Exception
*/
@Test
public void testAddRole() throws Exception {
//fail("Not yet implemented");
role.setUsername("123");
role.setPassword("123");
rd.addRole(role);
}
@Test
public void testRemoveRole() throws Exception {
//fail("Not yet implemented");
role.setRoleid(19);
rd.removeRole(role);
}
@Test
public void testUpdateRole() throws Exception {
//fail("Not yet implemented");
role.setRoleid(20);
role.setPassword("33322");
rd.updateRole(role);
}
}
9.表结构