首先第一步引依赖:
2.创建数据库:做一个简单的添加吧:
还得创建两个jsp
3.写dao,service,entity,servlet
实现类
dao层
service:
然后就是servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String name = request.getParameter("bookname");
String author = request.getParameter("bookauthor");
String bookprice = request.getParameter("bookprice");
Integer price = Integer.parseInt(bookprice);
Book book = new Book();
book.setBookname(name);
book.setBookauthor(author);
book.setBookprice(price);
//ApplicationContext cOntext= new ClassPathXmlApplicationContext("applicationContext18ssm.xml");
ApplicationContext cOntext= WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
IBookService service= (IBookService)context.getBean("bookservice");
try {
int count= service.addBook(book);
if (count>0){
request.getRequestDispatcher("/index.jsp").forward(request,response);
}else {
response.sendRedirect("/addBook.jsp");
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
最后配置文件
xmlns:cOntext="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">