Service的增删改查项目
搭建后端步骤:
1.创建数据库
2.搭建数据库连接池
#数据库连接池
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///exam?characterEncoding=utf8
username=root
password=123456
#参数
#初始化池子的连接数量
initialSize=10
#最大池子连接数量
maxActive=50
#最长等待时间
maxWait=3000
3.在tools工具包中创建DBUtil德鲁伊的工具类
//德鲁伊的工具类
public class DBUtil {
// 数据库访问池
private static DataSource pool;
static {
Properties pro = new Properties();
InputStream is = DbUtils.class.getClassLoader().getResourceAsStream("db.properties");
try {
pro.load(is);
pool = DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 提供连接数据的方法
public static DataSource getDataSource() {
return pool;
}
public static void main(String[] args) {
Connection con; // 连接
try {
con = pool.getConnection();
System.out.println(con);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
前端页面index.jsp全局格式
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>全局显示</title>
</head>
<body>
<table border="1" align="center" width="600px">
<tr>
<td>客户名称</td>
<td>客户地址</td>
<td>客户来源</td>
<td>客户电话</td>
<td>客户等级</td>
<td>修改操作</td>
<td>删除操作</td>
</tr>
<c:forEach items="${list}" var="customer" varStatus="status" >
<tr>
<td>${customer.name}</td>
<td>${customer.address}</td>
<td>${customer.source}</td>
<td>${customer.phone}</td>
<td>${customer.level}</td>
<td>
<a href="${pageContext.request.contextPath}/update?id=${customer.id}">修改</a>
</td>
<td>
<a href="${pageContext.request.contextPath}/delete?id=${customer.id}">删除</a>
</td>
</tr>
</c:forEach>
<tr>
<td colspan="7"><a href="${pageContext.request.contextPath}/add.jsp">添加用户信息</a></td>
</tr>
</table>
</body>
</html>
添加类
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>页面添加</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/add" method="post">
客户名称:<input type="text" name="name" >
客户地址:<input type="text" name="address" ><br>
客户来源:<input type="text" name="source" >
客户电话:<input type="text" name="phone" ><br>
客户级别:<input type="text" name="level" ><br>
<input type="submit" value="保存">
</form>
</body>
</html>
在pojo中写实体类 带属性的类
public class Customer {
private Integer id;
private String name;
private String address;//地址
private String source;//来源
private String phone;//电话
private String level;//等级
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public Customer(Integer id, String name, String address, String source, String phone, String level) {
super();
this.id = id;
this.name = name;
this.address = address;
this.source = source;
this.phone = phone;
this.level = level;
}
public Customer() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", address=" + address + ", source=" + source + ", phone="
+ phone + ", level=" + level + "]";
}
}
在dao层里面完成接口(接口里面一般是增删改查抽象方法)再把pojo实体类的对象注入进来 在创建dao层实现类(里面一般是增删改查的方法,例如数据库语句)这一层跟数据库关系很大
//在dao层写一个接口
public interface CustomerDao {
// 查询所有客户信息
List<Customer> queryAll();
// 添加客户信息
void addCustomer(Customer customer);
}
public class CustomerDaoImpl implements CustomerDao {
// 使用 DBUtils工具类
QueryRunner qr = new QueryRunner(DBUtil.getDataSource());// 查询数据源
@Override // 查询
public List<Customer> queryAll() {
String sql = "select * from customer";
List<Customer> list = null;
try {
list = qr.query(sql, new BeanListHandler<Customer>(Customer.class));
System.out.println(list);
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public void addCustomer(Customer customer) {
String sql = "insert into customer values(null,?,?,?,?,?)";
try {
int count = qr.update(sql, customer.getName(), customer.getAddress(), customer.getSource(),
customer.getPhone(), customer.getLevel());
if (count > 0) {
System.out.println("添加数据成功!");
} else {
System.out.println("添加数据失败!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
.这一层是服务层service 就是把dao层的数据传输给servlet服务器 有实体类注入的对象,有接口,有实现类,
public interface CustomerService {
// 查询所有客户信息
List<Customer> queryAll();
// 添加客户信息
void addCustomer(Customer customer);
}
//服务层
public class CustomerServiceImpl implements CustomerService{
// 属性注入
private CustomerDao cd = new CustomerDaoImpl();
//查询
@Override
public List<Customer> queryAll() {
return cd.queryAll();
}
//添加
@Override
public void addCustomer(Customer customer) {
cd.addCustomer(customer);
}
}
controller控制器中创建增删改查的最终方法 里面有服务器的dogGet 和dogPost 注意这一层有注解@WebServlet
@WebServlet("/add")
public class AddServlet extends HttpServlet {
// 关联service层
// 属性注入
private CustomerService cs = new CustomerServiceImpl();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
Customer customer = new Customer();
try {
BeanUtils.populate(customer, request.getParameterMap());
} catch (Exception e) {
e.printStackTrace();
}
// 调用service层的方法
cs.addCustomer(customer);
// 跳转到查询所有信息的Servlet中
response.sendRedirect(request.getContextPath()+"/queryAll");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}