第一步,创建数据库,根据库创建表,并插入数据;
CREATE DATABASE user;
USE user;
CREATE TABLE dept(
did INT(4) NOT NULL PRIMARY KEY AUTO_INCREMENT,
dname VARCHAR(50),
dhobby VARCHAR(255)
)CHARSET UTF8;
INSERT INTO dept VALUES(NULL,"sdafs","sadfe")............
第二步,创建项目,项目如下
1.导包,搭建Spring环境,需要的包如下
aopalliance-1.0.jar
aspectjweaver-1.6.9.jar
commons-logging-1.1.1.jar
jstl.jar
log4j-1.2.17.jar
mysql-connector-java-5.1.0-bin.jar
spring-aop-3.2.13.RELEASE.jar
spring-beans-3.2.13.RELEASE.jar
spring-context-3.2.13.RELEASE.jar
spring-core-3.2.13.RELEASE.jar
spring-expression-3.2.13.RELEASE.jar
spring-web-3.2.13.RELEASE.jar
spring-webmvc-3.2.13.RELEASE.jar
standard.jar
2,创建实体类
package com.bdqn.entity; import java.io.Serializable; public class Dept implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private Integer did; private String dname; private String dhobby; public Integer getDid() { return did; } public void setDid(Integer did) { this.did = did; } public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname; } public String getDhobby() { return dhobby; } public void setDhobby(String dhobby) { this.dhobby = dhobby; } @Override public String toString() { return "Dept [did=" + did + ", dname=" + dname + ", dhobby=" + dhobby + "]"; } }3.创建utils工具类,加载数据库
package com.bdqn.utils; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ConfigManager { private static ConfigManager configManager; private static Properties properties; private ConfigManager(){ try { properties = new Properties(); InputStream is = ConfigManager.class.getClassLoader().getResourceAsStream("database.properties"); properties.load(is); } catch (IOException e) { e.printStackTrace(); } } public static synchronized ConfigManager getConfigManager(){ if (configManager == null) { synchronized (ConfigManager.class) { configManager = new ConfigManager(); } } return configManager; } public String getValues(String key){ return properties.getProperty(key); } }4.创建dao层下的基础类和接口
package com.bdqn.utils; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ConfigManager { private static ConfigManager configManager; private static Properties properties; private ConfigManager(){ try { properties = new Properties(); InputStream is = ConfigManager.class.getClassLoader().getResourceAsStream("database.properties"); properties.load(is); } catch (IOException e) { e.printStackTrace(); } } public static synchronized ConfigManager getConfigManager(){ if (configManager == null) { synchronized (ConfigManager.class) { configManager = new ConfigManager(); } } return configManager; } public String getValues(String key){ return properties.getProperty(key); } }
package com.bdqn.utils; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ConfigManager { private static ConfigManager configManager; private static Properties properties; private ConfigManager(){ try { properties = new Properties(); InputStream is = ConfigManager.class.getClassLoader().getResourceAsStream("database.properties"); properties.load(is); } catch (IOException e) { e.printStackTrace(); } } public static synchronized ConfigManager getConfigManager(){ if (configManager == null) { synchronized (ConfigManager.class) { configManager = new ConfigManager(); } } return configManager; } public String getValues(String key){ return properties.getProperty(key); } }5.实现接口
package com.bdqn.dao.impl; import com.bdqn.dao.BaseDao; import com.bdqn.dao.DeptDao; import com.bdqn.entity.Dept; import org.springframework.stereotype.Repository; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @Repository("DeptDao") public class DeptDaoImpl extends BaseDao implements DeptDao{ @Override public List6.实现service层selectDept() { String sql ="select * from dept"; ResultSet rs = this.getResultSet(sql, null); List list = new ArrayList (); try { while(rs.next()){ Dept dept = new Dept(); dept.setDid(rs.getInt("did")); dept.setDname(rs.getString("dname")); dept.setDhobby(rs.getString("dhobby")); list.add(dept); } } catch (SQLException e) { e.printStackTrace(); } return list; } @Override public int addDept(Dept dept) { String sql = "insert into dept values(?,?,?)"; Object[] objs={null,dept.getDname(),dept.getDhobby()}; int num = this.insert(sql, objs); return num; } @Override public int deleteDept(String dname) { String sql = "delete from dept where dname=?"; Object[] objs = {dname}; int num = this.insert(sql, objs); return num; } @Override public int updateDept(Dept dept) { String sql ="update dept set dname=?,dhobby=? where did=?"; Object[] objs = {dept.getDname(),dept.getDhobby(),dept.getDid()}; int num = this.insert(sql, objs); return num; } }
package com.bdqn.dao.impl; import com.bdqn.dao.BaseDao; import com.bdqn.dao.DeptDao; import com.bdqn.entity.Dept; import org.springframework.stereotype.Repository; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @Repository("DeptDao") public class DeptDaoImpl extends BaseDao implements DeptDao{ @Override public ListselectDept() { String sql ="select * from dept"; ResultSet rs = this.getResultSet(sql, null); List list = new ArrayList (); try { while(rs.next()){ Dept dept = new Dept(); dept.setDid(rs.getInt("did")); dept.setDname(rs.getString("dname")); dept.setDhobby(rs.getString("dhobby")); list.add(dept); } } catch (SQLException e) { e.printStackTrace(); } return list; } @Override public int addDept(Dept dept) { String sql = "insert into dept values(?,?,?)"; Object[] objs={null,dept.getDname(),dept.getDhobby()}; int num = this.insert(sql, objs); return num; } @Override public int deleteDept(String dname) { String sql = "delete from dept where dname=?"; Object[] objs = {dname}; int num = this.insert(sql, objs); return num; } @Override public int updateDept(Dept dept) { String sql ="update dept set dname=?,dhobby=? where did=?"; Object[] objs = {dept.getDname(),dept.getDhobby(),dept.getDid()}; int num = this.insert(sql, objs); return num; } }
package com.bdqn.service.impl; import com.bdqn.dao.DeptDao; import com.bdqn.entity.Dept; import com.bdqn.service.DeptService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service("deptService") public class DeptServiceImpl implements DeptService{ @Autowired private DeptDao deptDao; @Override public List7.内部测试下,看数据库是否连接上了,以及功能是否都能实现。selectDept() { // TODO Auto-generated method stub return deptDao.selectDept(); } @Override public int addDept(Dept dept) { return deptDao.addDept(dept); } @Override public int deleteDept(String dname) { return deptDao.deleteDept(dname); } @Override public int updateDept(Dept dept) { return deptDao.updateDept(dept); } }
package com.bdqn.service.impl; import com.bdqn.dao.DeptDao; import com.bdqn.entity.Dept; import com.bdqn.service.DeptService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service("deptService") public class DeptServiceImpl implements DeptService{ @Autowired private DeptDao deptDao; @Override public List8.配置web.xml,Spring.xml,applicationContext.xml(此3个文件重要,不能出错)selectDept() { // TODO Auto-generated method stub return deptDao.selectDept(); } @Override public int addDept(Dept dept) { return deptDao.addDept(dept); } @Override public int deleteDept(String dname) { return deptDao.deleteDept(dname); } @Override public int updateDept(Dept dept) { return deptDao.updateDept(dept); } }
package com.bdqn.service.impl; import com.bdqn.dao.DeptDao; import com.bdqn.entity.Dept; import com.bdqn.service.DeptService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service("deptService") public class DeptServiceImpl implements DeptService{ @Autowired private DeptDao deptDao; @Override public ListselectDept() { // TODO Auto-generated method stub return deptDao.selectDept(); } @Override public int addDept(Dept dept) { return deptDao.addDept(dept); } @Override public int deleteDept(String dname) { return deptDao.deleteDept(dname); } @Override public int updateDept(Dept dept) { return deptDao.updateDept(dept); } }
xml version="1.0" encoding="UTF-8"?>xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.bdqn.controllers" /> <mvc:annotation-driven /> class="org.springframework.web.servlet.view.InternalResourceViewResolver"> name="prefix" value="/WEB-INF/jsp/" /> name="suffix" value=".jsp" />