zoukankan      html  css  js  c++  java
  • 17_8_17 SpringMvc dbcp 框架开发流程

    点击

    1.maven 项目 配置pom.xml

    dependecies:
    1.Spring :context
    2.Spring :web
    3.Spring :webmvc
    
    4.dbcp:    commons-dbcp
    5.mysql:   mysql-connector //数据库驱动
    
    6.Spring:jdbc //要用到其中的 JdbcTemplate ,所以需要导入
    7.Jstl :           //jsp需要用到 <c:forech>
    

    2.web.xml

    1.context(关键字)--->ContextLoaderListener    //加载其他需要的xml,
    2.dispatcher(关键字)--->DispatcherServlet
        这里注意一点:
    <servlet-mapping>
    	<servlet-name>dispathcerservlet</servlet-name>
    	<url-pattern>/</url-pattern>    
    </servlet-mapping>
    

    写'/' 加载静态资源!!!
    [也可以参考](http://blog.csdn.net/jbgtwang/article/details/7359592)

    3.dispatcherservlet.xml:

    <mvc:annotation-driven/>    //配置注解驱动
    <mvc:resources mapping="/movies/**" location="/movies/"/> //加载静态资源
    
    <context:component-scan base-package="org.rf"></context:component-scan>  //扫描注解的类
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> //解析jsp,目的:jsp放到WEB-INF下面,受保护也可以访问
    	<property name="prefix" value="/WEB-INF/jsp/"></property>
    	<property name="suffix" value=".jsp"></property>
    </bean>
    

    4.0 db.properties

    dbcp.driverClassName=com.mysql.jdbc.Driver
    dbcp.url=jdbc:mysql://127.0.0.1:3306/db_name?useSSL=false    //serverTimezone=UTC&amp;注意了:如果报错The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone
    解决方案是:调整mysql版本,原因过高!    参考1:http://blog.csdn.net/u011063151/article/details/51730954 参考2:http://blog.csdn.net/anaini1314/article/details/71157791
    dbcp.username=root
    dbcp.password=  
    initialSize=0  
    #定义最大连接数  
    maxActive=20  
    #定义最大空闲  
    maxIdle=20  
    #定义最小空闲  
    minIdle=1  
    #定义最长等待时间  
    maxWait=60000  
    

    4.db.xml:目的:加载数据库,获得JJdbcTemplate实例

    <context:property-placeholder location="classpath:db.properties"/> //导入外部db.Properties文件
    
    <bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    		<property name="driverClassName" value="${dbcp.driverClassName}"></property>
    		<property name="url" value="${dbcp.url}"></property>
    		<property name="username" value="${dbcp.username}"></property>
    		<property name="password" value="${dbcp.password}"></property>
    </bean>
    
    <bean class="org.springframework.jdbc.core.JdbcTemplate">    //实例化JdbcTemplate
    		<constructor-arg ref="basicDataSource"></constructor-arg>
    </bean>
    

    导入外部db.Properties文件的方式

    5.各层次+注解方式:

    注解参考

    dao层:

    @Repository    //@Component通用的注解  @Controller控制层直接 @Repository Dao层注解 @Service service层注解
    public class MovieDaoImpl implements MovieDao{
    	
    	@Autowired //    引入属性实例
    	private JdbcTemplate jdbcTemplate;
    	
    	public List<Movie> selectMovies() {
    		String sql="select * from movies";
    		return jdbcTemplate.query(sql, new RowMapper<Movie>(){
    			public Movie mapRow(ResultSet rs, int rowNum) throws SQLException {
    				Movie movie=new Movie(rs.getInt(1),rs.getString(2)
    						,rs.getString(3),rs.getString(4),rs.getString(5));
    				return movie;
    			}
    			});
    	}
    }
    

    service层

    @Service
    public class MovieServiceImpl implements MovieService{
    	@Autowired
    	private MovieDao moviedao;
    
    	public List<Movie> selectMovies() {
    		return moviedao.selectMovies();
    	}
    }
    

    Controller层

    @Controller
    public class MovieCtroller{
    	@Autowired
    	private MovieService movieService; 
    
    	@RequestMapping("/aaa.s")        //访问需要输入的地址
    	public ModelAndView handleRequest(){
    		ModelAndView mav=new ModelAndView("test");    //ModeAndView的构造方法:出入jsp 名字
    		mav.addObject("kk",movieService.selectMovies());
    		for(Movie a:movieService.selectMovies()){
    		}
    		return mav;
    	}
    }
    

    6.jsp页面:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8" isELIgnored="false"%>
        <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>        //引入jstl文件
    <!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>Insert title here</title>
    </head>
    <body>
    	 <c:forEach items="${kk}" var="t">        //el表达式和jstl的配合使用
    		<video width="320" height="240" controls autoplay>
      			<source src="${t.getName()}" type="video/mp4">
    		</video> 
    	</c:forEach> 
    </body>
    </html>
    

  • 相关阅读:
    ssh:connect to host xxx port 22: Connection refused---------you don't have permission to acccess the requested location--------remote host identification has changed
    YOLOV3算法笔记
    jQuery元素查找方法
    jquery获取元素方式
    jQuery常用事件
    Jquery常用方法
    JavaScript数组操作
    jQuery中.bind() .live() .delegate() .on()的区别
    CSS初始化代码
    jQuery判断浏览器
  • 原文地址:https://www.cnblogs.com/du1991/p/7380563.html
Copyright © 2011-2022 走看看