zoukankan      html  css  js  c++  java
  • springmvc+mybatis+oracle+druid搭建项目

    第一次写博客,主要是为了记录一下学习新技术的过程,好了,进入主题!

    开发环境:JDK8,eclipse,maven3.5,oracle数据库,druid连接池

    首先,建立一个maven工程,选webapp项目,pom.xml如下:

    <project xmlns="http://maven.apache.org/POM/4.0.0"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<groupId>com.bin.study</groupId>
    	<artifactId>servlet-druid</artifactId>
    	<packaging>war</packaging>
    	<version>0.0.1-SNAPSHOT</version>
    	<name>servlet-druid Maven Webapp</name>
    	<url>http://maven.apache.org</url>
    	<properties>
    		<!--统一版本 -->
    		<spring-version>4.3.4.RELEASE</spring-version>
    		<druid-version>1.1.10</druid-version>
    		<maven.compiler.source.version>1.8</maven.compiler.source.version>
    		<maven.compiler.target.version>1.8</maven.compiler.target.version>
    	</properties>
    	<dependencies>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>3.8.1</version>
    			<scope>test</scope>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    		<dependency>
    			<groupId>javax.servlet</groupId>
    			<artifactId>javax.servlet-api</artifactId>
    			<version>3.1.0</version>
    			<scope>provided</scope>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
    		<dependency>
    			<groupId>javax.servlet</groupId>
    			<artifactId>jstl</artifactId>
    			<version>1.2</version>
    		</dependency>
    		<!-- mybatis核心包 -->
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis</artifactId>
    			<version>3.4.6</version>
    		</dependency>
    		<!-- mybatis/spring包 -->
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis-spring</artifactId>
    			<version>1.3.2</version>
    		</dependency>
    		<!--oracle -->
    		<!-- https://mvnrepository.com/artifact/com.oracle/ojdbc6 -->
    		<dependency>
    			<groupId>com.oracle</groupId>
    			<artifactId>ojdbc6</artifactId>
    			<version>12.1.0.1-atlassian-hosted</version>
    		</dependency>
    		<!-- Jetty -->
    		<dependency>
    			<groupId>org.eclipse.jetty.aggregate</groupId>
    			<artifactId>jetty-all</artifactId>
    			<version>8.0.4.v20111024</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-expression</artifactId>
    			<version>${spring-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-beans</artifactId>
    			<version>${spring-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-aop</artifactId>
    			<version>${spring-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context</artifactId>
    			<version>${spring-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context-support</artifactId>
    			<version>${spring-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-tx</artifactId>
    			<version>${spring-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-jdbc</artifactId>
    			<version>${spring-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-orm</artifactId>
    			<version>${spring-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-web</artifactId>
    			<version>${spring-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-webmvc</artifactId>
    			<version>${spring-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-test</artifactId>
    			<version>${spring-version}</version>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>com.alibaba</groupId>
    			<artifactId>druid</artifactId>
    			<version>${druid-version}</version>
    		</dependency>
    	</dependencies>
    	<profiles>
    		<profile>
    			<id>jdk8</id>
    			<activation>
    				<activeByDefault>true</activeByDefault>
    				<jdk>1.8</jdk>
    			</activation>
    			<properties>
    				<maven.compiler.source>${maven.compiler.source.version}</maven.compiler.source>
    				<maven.compiler.target>${maven.compiler.target.version}</maven.compiler.target>
    			</properties>
    		</profile>
    	</profiles>
    	<build>
    		<finalName>servlet-druid</finalName>
    		<plugins>
    			<plugin>
    				<artifactId>maven-compiler-plugin</artifactId>
    				<version>2.3.2</version>
    				<configuration>
    					<source>${javac.target.version}</source>
    					<target>${javac.target.version}</target>
    					<encoding>UTF-8</encoding>
    				</configuration>
    			</plugin>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-war-plugin</artifactId>
    				<version>2.1.1</version>
    			</plugin>
    			<plugin>
    				<groupId>org.mortbay.jetty</groupId>
    				<artifactId>jetty-maven-plugin</artifactId>
    				<version>8.1.16.v20140903</version>
    				<configuration>
    					<scanIntervalSeconds>10</scanIntervalSeconds>
    					<webApp>
    						<contextPath>/study</contextPath>
    					</webApp>
    					<connectors>
    						<connector
    							implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
    							<port>8090</port>
    						</connector>
    					</connectors>
    				</configuration>
    			</plugin>
    		</plugins>
    	</build>
    </project>
    

    spring配置文件如下,我这边取名为spring-base.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans    
                            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
                            http://www.springframework.org/schema/context    
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd    
                            http://www.springframework.org/schema/mvc    
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    	<context:property-placeholder
    		location="classpath:druid.properties" />
    	<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
    	<context:component-scan
    		base-package="com.bin.study" />
    	<!--静态资源不拦截 -->
    	<mvc:resources location="/css/" mapping="/css/**" />
    	<mvc:resources location="/js/" mapping="/js/**" />
    	<!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
    	<mvc:annotation-driven />
    	<bean id="dataSource"
    		class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
    		destroy-method="close">
    		<property name="url" value="${jdbc.url}" />
    		<property name="username" value="${jdbc.username}" />
    		<property name="password" value="${jdbc.password}" />
    		<property name="initialSize" value="1" />
    		<property name="minIdle" value="1" />
    		<property name="maxActive" value="20" />
    		<property name="maxWait" value="60000" />
    		<property name="timeBetweenEvictionRunsMillis" value="60000" />
    		<property name="minEvictableIdleTimeMillis" value="300000" />
    		<property name="testWhileIdle" value="true" />
    		<property name="testOnBorrow" value="false" />
    		<property name="testOnReturn" value="false" />
    		<property name="poolPreparedStatements" value="true" />
    		<property name="maxPoolPreparedStatementPerConnectionSize"
    			value="20" />
    		<property name="filters" value="stat" />
    	</bean>
    	<!-- spring jdbc NamedParameterJdbcTemplate -->
    	<bean id="namedjdbctemp"
    		class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
    		<constructor-arg ref="dataSource" />
    	</bean>
    
    	<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    	<bean id="sqlSessionFactory"
    		class="org.mybatis.spring.SqlSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<!-- 自动扫描mapping.xml文件 -->
    		<!-- <property name="mapperLocations" value="classpath:com/cwj/mapping/*.xml"></property> -->
    	</bean>
    
    	<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    		<property name="basePackage" value="com.bin.study.dao" />
    		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    	</bean>
    
    	<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    	<bean id="transactionManager"
    		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="dataSource" />
    	</bean>
    	
    	  <!-- 定义跳转的文件的前后缀 ,视图模式配置-->  
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
            <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->  
            <property name="prefix" value="/WEB-INF/" />  
            <property name="suffix" value=".jsp" />  
        </bean> 
    </beans>
    

    数据库连接信息放在druid.properties

    jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
    jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
    jdbc.username=liubin_test 
    jdbc.password=1105915292
    

    web.xml配置如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns="http://java.sun.com/xml/ns/javaee"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    	version="3.0">
    	<display-name>Archetype Created Web Application</display-name>
    	<!-- Spring和mybatis的配置文件 -->
    	<!-- <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mybatis.xml</param-value> 
    		</context-param> -->
    	<!-- 编码过滤器 -->
    	<filter>
    		<filter-name>encodingFilter</filter-name>
    		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    		<async-supported>true</async-supported>
    		<init-param>
    			<param-name>encoding</param-name>
    			<param-value>UTF-8</param-value>
    		</init-param>
    	</filter>
    	<filter-mapping>
    		<filter-name>encodingFilter</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    	<!-- Spring监听器 -->
    	<!-- <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    		</listener> -->
    	<!-- 防止Spring内存溢出监听器 -->
    	<!-- <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> 
    		</listener> -->
    	<servlet>
    		<servlet-name>DruidStatView</servlet-name>
    		<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>DruidStatView</servlet-name>
    		<!--访问路径eg:http://localhost:8080/druid/index.html -->
    		<url-pattern>/druid/*</url-pattern>
    	</servlet-mapping>
    	<filter>
    		<filter-name>DruidWebStatFilter</filter-name>
    		<filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
    		<init-param>
    			<param-name>exclusions</param-name>
    			<param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value>
    		</init-param>
    	</filter>
    	<filter-mapping>
    		<filter-name>DruidWebStatFilter</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    	<!-- Spring MVC servlet -->
    	<servlet>
    		<servlet-name>SpringMVC</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>classpath:spring-base.xml</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    		<async-supported>true</async-supported>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>SpringMVC</servlet-name>
    		<!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
    		<url-pattern>/</url-pattern>
    	</servlet-mapping>
    	<welcome-file-list>
    		<welcome-file>/index.jsp</welcome-file>
    	</welcome-file-list>
    	<jsp-config>
    		<jsp-property-group>
    			<url-pattern>*.jsp</url-pattern>
    			<el-ignored>false</el-ignored>
    			<!-- <el-ignored>true</el-ignored> -->
    		</jsp-property-group>
    	</jsp-config>
    
    </web-app>
    

    项目路径如下:

    AreaDao类代码如下:

    package com.bin.study.dao;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.annotation.Resource;
    
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.jdbc.core.RowMapper;
    import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
    import org.springframework.stereotype.Repository;
    
    import com.bin.study.bean.Area;
    
    @Repository
    public class AreaDao {
    	@Resource
    	private NamedParameterJdbcTemplate namedjdbctemp;
    
    	public Area queryAreaById(Long id) {
    		String sql = "SELECT ID,LNG,LAT,AREA_NAME FROM EIS_WISEYE_AREA WHERE ID=:id";
    		Map<String, Object> map = new HashMap<String, Object>();
    		map.put("id", id);
    		return namedjdbctemp.queryForObject(sql, map, new BeanPropertyRowMapper<Area>(Area.class));
    	}
    
    	public List<Area> queryAll() {
    		String sql = "SELECT LNG,LAT,AREA_NAME,ID
    " + "  FROM (SELECT A.*, ROWNUM RN
    "
    				+ "          FROM (SELECT LNG, LAT, AREA_NAME, id FROM EIS_WISEYE_AREA) A
    "
    				+ "         WHERE ROWNUM <= 40)
    " + " WHERE RN >= 21";
    		RowMapper<Area> rm = BeanPropertyRowMapper.newInstance(Area.class);
    		List<Area> queryForList = namedjdbctemp.query(sql, rm);
    		return queryForList;
    	}
    
    }
    

    jsp代码如下:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <!-- 加/则是不加项目根路径 -->
    <link href="css/bootstrap.css" rel="stylesheet"></link>
    <script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    </head>
    <body>
    	<table
    		class="table table-bordered table-striped table-condensed text-center">
    		<tr class="table-danger">
    			<td>主键</td>
    			<td>经度</td>
    			<td>维度</td>
    			<td>区域名称</td>
    		</tr>
    		<c:forEach items="${areas}" var="area">
    			<tr>
    				<td>${area.id }</td>
    				<td>${area.lng }</td>
    				<td>${area.lat }</td>
    				<td>${area.areaName }</td>
    			</tr>
    		</c:forEach>
    	</table>
    </body>
    </html>
    

    项目使用jetty启动,选择项目run config

     等项目启动起来后,就可以直接访问

    druid监控页面地址:http://localhost:8090/study/druid/index.html

  • 相关阅读:
    A. Vasya and Book
    B. Curiosity Has No Limits
    A. Link/Cut Tree
    C. Yuhao and a Parenthesis
    D2. Magic Powder
    B. Approximating a Constant Range
    51nod1185 威佐夫游戏 V2 (模拟乘法)
    博弈论(巴什博奕,威佐夫博弈,尼姆博弈,斐波那契博弈)
    sg函数模板
    D.Starry的神奇魔法(矩阵快速幂)
  • 原文地址:https://www.cnblogs.com/wnhbx/p/11504693.html
Copyright © 2011-2022 走看看