spring整合javaweb(第一版)
Posted 努力奋斗吧
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring整合javaweb(第一版)相关的知识,希望对你有一定的参考价值。
1.导入依赖(jar包)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!-- 2.javaee的依赖-->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<!-- 3.jsp进行循环迭代数据 使用jstl-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
以添加图书为例:
Entity层
public class Book {
private Integer bookid;
private String bookname;
private String bookprice;
private String booktype;
public Integer getBookid() {return bookid;}
public void setBookid(Integer bookid) {
this.bookid = bookid;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getBookprice() {
return bookprice;
}
public void setBookprice(String bookprice) {
this.bookprice = bookprice;
}
public String getBooktype() {
return booktype;
}
public void setBooktype(String booktype) {
this.booktype = booktype;
}
}
Dao层
public interface IBookDAO {
/**
*
* @param book
* @return
*/
public int addBook(Book book);
}
IBookDAO.xml文件
<?xml version=‘1.0‘ encoding=‘UTF-8‘?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.happy.javaweb.dao.IBookDAO">
<!--添加后返回自增列的值-->
<insert id="addBook" parameterType="Book">
insert into book(bookname,bookprice,booktype) value(#{bookname},#{bookprice},#{booktype})
</insert>
</mapper>
Service层
public interface IBookService {
/**
*
* @param book
* @return
*/
public int addBook(Book book);
}
public class BookServiceImpl implements IBookService {
//植入dao
private IBookDAO dao;
public int addBook(Book book) {
return dao.addBook(book);
}
public IBookDAO getDao() {return dao;}
public void setDao(IBookDAO dao) {
this.dao = dao;
}
}
<?xml version=‘1.0‘ encoding=‘UTF-8‘?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration> <!--根节点 -->
<!--配置类型别名-->
<typeAliases>
<!--将该类中的简单类型作为别名 -->
<package name="cn.happy.javaweb.entity"></package>
</typeAliases>
</configuration>
jdbc.properties文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///school
jdbc.username=root
jdbc.password=123156
applicationContextJavaWed.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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
">
<!--01识别jdbc.properties-->
<!--方式二-->
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean>
<!--2.数据源 spring内置数据源 DriverManagerDataSource-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--03工厂配置-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--大配置文件-->
<property name="configLocation" value="classpath:mybatis-config01.xml"></property>
</bean>
<!--04dao 实现类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.happy.javaweb.dao"></property>
</bean>
<!--05service-->
<bean id="studentService" class="cn.happy.javaweb.service.BookServiceImpl">
<property name="dao" ref="IBookDAO"></property>
</bean>
</beans>
页面:
success.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加图书</title>
</head>
<body>
<center>
<form method="post" action="/BookServlet">
图书名称:<input type="text" name="name"><br/>
图书价格:<input type="text" name="price"><br/>
图书类型:<input type="text" name="type"><br/>
<input type="submit" value="提交">
</form>
</center>
</body>
</html>
index.jsp页面
<%@ page language="java" pageEncoding="UTF-8" %>
<html>
<head>
<title>添加图书</title>
</head>
<body>
<h2>添加成功</h2>
</body>
</html>
Servlet层
@WebServlet(name = "BookServlet",urlPatterns = {"/BookServlet"})
public class BookServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String name= request.getParameter("name");
String price= request.getParameter("price");
String type= request.getParameter("type");
Book book=new Book();
book.setBookname(name);
book.setBookprice(price);
book.setBooktype(type);
//调用添加的方法
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextJavaWed.xml");
IBookService service = (IBookService)context.getBean("studentService");
int count = service.addBook(book);
if (count>0){ //添加成功
request.getRequestDispatcher("/index.jsp").forward(request,response);
}else { //添加失败
request.getRequestDispatcher("/success.jsp").forward(request,response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
有时jar包可能会发生冲突,这就需要我们这就不断调试
以上是关于spring整合javaweb(第一版)的主要内容,如果未能解决你的问题,请参考以下文章
JavaWeb_(Spring框架)用户登陆Spring整合到Servlet中
JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)Spring整合web开发整合Junit4测试