使用idea 搭建SpringMVC+Mybatis

原创 2016年08月29日 08:10:42


1.  工程目录结构整理





注意在Idea中所有的配置文件都必须放在Resource包中!! 不然编译器无法编译配置文件(除了web.xmlspringMvc-servlet.xml外)




2.  配置mavenPOM.xml文件



<?xml version="1.0"encoding="UTF-8"?>
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<
modelVersion>4.0.0</modelVersion>

<
groupId>com.demo</groupId>
<
artifactId>nfm</artifactId>
<
version>1.0-SNAPSHOT</version>

<
build>
<
plugins>
<
plugin>
<
groupId>org.apache.maven.plugins</groupId>
<
artifactId>maven-compiler-plugin</artifactId>
<
configuration>
<
source>1.7</source>
<
target>1.7</target>
</
configuration>
</
plugin>
</
plugins>
</
build>

<
properties>

<!-- spring版本号-->
<spring.version>4.3.2.RELEASE</spring.version>
<!-- mybatis版本号-->
<mybatis.version>3.2.4</mybatis.version>
<!-- log4j日志文件管理包版本-->
<slf4j.version>1.6.6</slf4j.version>
<
log4j.version>1.2.9</log4j.version>
</
properties>

<
dependencies>

<
dependency>
<
groupId>org.springframework</groupId>
<
artifactId>spring-webmvc</artifactId>
<
version>${spring.version}</version>
</
dependency>

<
dependency>
<
groupId>org.springframework</groupId>
<
artifactId>spring-oxm</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-aspects</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-aop</artifactId>-->
<!--<version>${spring.version}</version>-->
<!--</dependency>-->

<dependency>
<
groupId>org.springframework</groupId>
<
artifactId>spring-test</artifactId>
<
version>${spring.version}</version>
</
dependency>
<!-- springframe end -->

<!-- mybatis核心包-->
<dependency>
<
groupId>org.mybatis</groupId>
<
artifactId>mybatis</artifactId>
<
version>${mybatis.version}</version>
</
dependency>
<!-- mybatis/spring-->
<dependency>
<
groupId>org.mybatis</groupId>
<
artifactId>mybatis-spring</artifactId>
<
version>1.2.2</version>
</
dependency>
<!-- mysql驱动包-->
<dependency>
<
groupId>mysql</groupId>
<
artifactId>mysql-connector-java</artifactId>
<
version>5.1.29</version>
</
dependency>
<!-- junit测试包-->
<dependency>
<
groupId>junit</groupId>
<
artifactId>junit</artifactId>
<
version>4.12</version>
<
scope>test</scope>
</
dependency>
<!--阿里巴巴数据源包-->
<dependency>
<
groupId>com.alibaba</groupId>
<
artifactId>druid</artifactId>
<
version>1.0.2</version>
</
dependency>

<!-- json数据-->
<dependency>
<
groupId>org.codehaus.jackson</groupId>
<
artifactId>jackson-mapper-asl</artifactId>
<
version>1.9.13</version>
</
dependency>

<!--日志文件管理包-->
<!-- log start -->
<dependency>
<
groupId>log4j</groupId>
<
artifactId>log4j</artifactId>
<
version>${log4j.version}</version>
</
dependency>
<
dependency>
<
groupId>org.slf4j</groupId>
<
artifactId>slf4j-api</artifactId>
<
version>${slf4j.version}</version>
</
dependency>
<
dependency>
<
groupId>org.slf4j</groupId>
<
artifactId>slf4j-log4j12</artifactId>
<
version>${slf4j.version}</version>
</
dependency>

<
dependency>
<
groupId>commons-logging</groupId>
<
artifactId>commons-logging</artifactId>
<
version>1.2</version>
</
dependency>

<!-- oracle数据源-->
<dependency>
<
groupId>com.oracle</groupId>
<
artifactId>ojdbc14</artifactId>
<
version>10.2.0.4.0</version>
</
dependency>

<!--c3p0数据源-->
<dependency>
<
groupId>c3p0</groupId>
<
artifactId>c3p0</artifactId>
<
version>0.9.1.2</version>
</
dependency>

</
dependencies>

</
project>





3.  配置web.xml文件



<?xml version="1.0"encoding="UTF-8"?>
<web-appxmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
>
<!--解决工程编码过滤器-->
<filter>
<
filter-name>SpringCharacterEncodingFilter</filter-name>
<
filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<
init-param>
<
param-name>encoding</param-name>
<
param-value>UTF-8</param-value>
</
init-param>
<
init-param>
<
param-name>forceEncoding</param-name>
<
param-value>true</param-value>
</
init-param>
</
filter>
<
filter-mapping>
<
filter-name>SpringCharacterEncodingFilter</filter-name>
<
url-pattern>/*</url-pattern>
</
filter-mapping>


<!--Spring开始-->
<context-param>
<
param-name>contextConfigLocation</param-name>
<
param-value>classpath:applicationContext.xml</param-value>
</
context-param>

<!-- spring监听器-->
<listener>
<
listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</
listener>

<!--配置中央控制器SpringMVC配置-->
<servlet>
<
servlet-name>springMvc</servlet-name>
<
servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<
load-on-startup>1</load-on-startup>
</
servlet>
<
servlet-mapping>
<
servlet-name>springMvc</servlet-name>
<
url-pattern>/</url-pattern>
</
servlet-mapping>

<
welcome-file-list>
<
welcome-file>/index</welcome-file>
</
welcome-file-list>


</
web-app>


 


4 . 配置springMvc_servlet.xml文件


 


<?xml version="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"
>

<
mvc:default-servlet-handler/><!--静态资源处理一般选择这个方案-->
</beans>


 


5  .配置applicationContext.xml文件



<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
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/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://mybatis.org/schema/mybatis-springhttp://mybatis.org/schema/mybatis-spring.xsd"
>

<!--通过aop命名空间的<aop:aspectj-autoproxy />声明自动为spring容器中那些配置 @aspectJ切面的bean创建代理,织入切面-->
<aop:aspectj-autoproxy/>

<!--自动扫描的包名-->
<context:component-scanbase-package="com.geok"use-default-filters="true"></context:component-scan>

<!--默认的注解映射的支持-->
<mvc:annotation-driven/>

<!--定义视图解析器(action中跳转的页面配置)-->
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--<property name="viewClass"value="org.springframework.web.servlet.view.JstlView"/>-->
<!--<property name="prefix" value="/"/>-->
<propertyname="suffix"value=".jsp"/>
</
bean>


<!--引入我们的jdbc.properties-->
<beanid="property"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<
propertyname="locations">
<
list>
<
value>classpath:jdbc.properties</value><!-- classpath代表类路径,src下面的文件-->
</list>
</
property>
</
bean>


<!--配置druid数据源-->
<beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource"init-method="init"
destroy-method="close"
>

<
propertyname="driverClassName">
<
value>${jdbc.driver}</value>
</
property>
<
propertyname="url">
<
value>${jdbc.url}</value>
</
property>
<
propertyname="username">
<
value>${jdbc.username}</value>
</
property>
<
propertyname="password">
<
value>${jdbc.password}</value>
</
property>
<!--连接池最大使用连接数-->
<propertyname="maxActive">
<
value>20</value>
</
property>
<!--初始化连接大小-->
<propertyname="initialSize">
<
value>1</value>
</
property>
<!--获取连接最大等待时间-->
<propertyname="maxWait">
<
value>60000</value>
</
property>
<!--连接池最大空闲-->
<propertyname="maxIdle">
<
value>20</value>
</
property>
<!--连接池最小空闲-->
<propertyname="minIdle">
<
value>3</value>
</
property>
<!--自动清除无用连接-->
<propertyname="removeAbandoned">
<
value>true</value>
</
property>
<!--清除无用连接的等待时间-->
<propertyname="removeAbandonedTimeout">
<
value>180</value>
</
property>
<!--连接属性-->
<propertyname="connectionProperties">
<
value>characterEncoding=UTF-8</value>
</
property>
</
bean>



<!--定义jdbcTemplate用于批量操作-->
<beanid="searchJdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate">
<
propertyname="dataSource"ref="dataSource"/>
</
bean>

<!--配置数据源C3P0数据库连接-->
<!-- <bean id="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
&lt;!&ndash;
用户名&ndash;&gt;
<property name="user" value="fda" />
&lt;!&ndash;
用户密码&ndash;&gt;
<property name="password" value="fda" />
<property name="driverClass"value="oracle.jdbc.driver.OracleDriver" />
<property name="jdbcUrl"value="jdbc:oracle:thin:@192.168.161.201:1521:orcl" />
&lt;!&ndash;
连接池中保留的最大连接数。默认值: 15&ndash;&gt;
<property name="maxPoolSize" value="20" />
&lt;!&ndash;
连接池中保留的最小连接数,默认为:3&ndash;&gt;
<property name="minPoolSize" value="2" />
&lt;!&ndash;
初始化连接池中的连接数,取值应在minPoolSizemaxPoolSize之间,默认为3&ndash;&gt;
<property name="initialPoolSize" value="2" />
&lt;!&ndash;
最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。默认值: 0 &ndash;&gt;
<property name="maxIdleTime" value="60"/>
</bean>-->

<!-- mybatis文件配置,扫描所有mapper文件-->
<!--
配置SqlSessionFactoryBean -->
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<
propertyname="dataSource"ref="dataSource"/>
<
propertyname="configLocation"value="classpath:mybatis.xml"/>
<!-- mapperresultmap配置路径-->
<propertyname="mapperLocations">
<
list>
<
value>classpath:com/geok/mapperConfig/*.xml</value>
</
list>
</
property>
</
bean>

<!--定义SqlSessionTemplate -->
<beanid="sqlSessionTemplate"class="org.mybatis.spring.SqlSessionTemplate">
<
constructor-argname="sqlSessionFactory"ref="sqlSessionFactory"></constructor-arg>
</
bean>


<
mybatis:scanbase-package="com.geok.mapper"/>
<!--自动扫描mapper接口,注入sqlsessionfactory-->
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<
propertyname="basePackage"value="com.geok.mapper"/>
<
propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>
</
bean>


<!--开启mybatis事务-->
<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<
propertyname="dataSource"ref="dataSource"/>
</
bean>

<
tx:adviceid="transactionAdvice"transaction-manager="transactionManager">
<
tx:attributes>
<
tx:methodname="create*"propagation="REQUIRED"/>
<
tx:methodname="add*"propagation="REQUIRED"/>
<
tx:methodname="insert*"propagation="REQUIRED"/>
<
tx:methodname="save*"propagation="REQUIRED"/>
<
tx:methodname="update*"propagation="REQUIRED"/>
<
tx:methodname="modify*"propagation="REQUIRED"/>
<
tx:methodname="edit*"propagation="REQUIRED"/>
<
tx:methodname="delete*"propagation="REQUIRED"/>
<
tx:methodname="remove*"propagation="REQUIRED"/>
<
tx:methodname="get*"propagation="SUPPORTS"/>
<
tx:methodname="find*"propagation="SUPPORTS"/>
<
tx:methodname="select*"propagation="SUPPORTS"/>
<
tx:methodname="*"propagation="SUPPORTS"/>
</
tx:attributes>
</
tx:advice>

<
aop:configproxy-target-class="false">
<
aop:pointcutid="transactionPointcut"
expression="execution(* com.geok.serviceImpl.*.*(..))"
/>
<
aop:advisorpointcut-ref="transactionPointcut"
advice-ref="transactionAdvice"
/>
</
aop:config>

<!--配置声明试事务配置-->
<tx:annotation-driventransaction-manager="transactionManager"proxy-target-class="true"/>


</
beans>




6  .配置jdbc.properties文件



jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url
=jdbc:oracle:thin:@192.168.161.201:1521:orcl
jdbc.username
=fda
jdbc.password
=fda






7  .定义一个接口存放增删改查方法


 


如:




8 配置映射User.xml文件



<mappernamespace="com.geok.mapper.UserMapper">

<
resultMapid="UserResultMap"type="com.geok.entity.UserInfo">
<
resultcolumn="NAME"property="name"jdbcType="VARCHAR"/>
<
resultcolumn="PASSWORD"property="password"jdbcType="VARCHAR"/>
<
resultcolumn="AGE"property="age"jdbcType="INTEGER"/>
</
resultMap>


<selectid="selectUser"resultMap="UserResultMap">
SELECT*FROM USERINFO
</select>
</
mapper>



9 .注入Mapper接口的映射类(注入后就可以调用mapper接口中的增删改查方法)




10.启动项目,在controller中调用方法






版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Spring4.2.6+SpringMVC+Mybatis+mysql+Gradle使用IDEA搭建

前段时间刚换成IDEA+Gradle开发  自己稍微的记录一下咯 先创建项目 选择自己本地安装的gradle   和maven差不多,算是个本地库吧。然后next输入你的项目名进行创建 最后会生成...

使用intellij idea搭建spring-springmvc-mybatis整合框架环境

1.打开idea,创建maven项目,File-New-Project 2.选择Maven,勾选Create from archetype,选择org.apache.cocoon:cocoon-22...

IDEA下搭建SpringMVC、MySql、Mybatis框架

IDEA SpringMvc Mysql Mybatis

idea中springmvc, mybatis, gradle环境搭建

Spring mvc和mybatis框架配置总结 开发环境: Jdk: 1.8 开发IDE:IntelliJ IDEA 2016.3(64) 数据库:mysql 5.7   配置文件: ...

Springmvc + Mybatis + IntelliJ IDEA 环境搭建

Springmvc + Mybatis + IntelliJ IDEA 环境搭建

idea 搭建 springMVC+mybatis+maven 项目(一)

.环境 idea 2016.2.1 jdk 1.7 tomcat 1.7 maven 3.3.9 idea创建maven项目 打开idea,点击File->New 点击next 继...

idea 下搭建springMvc + mybatis+Maven

刚开始使用idea,发现idea与myEclipse差别还是很大的,从搭建的简单的SpringMvc开始,一路遇到种种困难。本文讲诉idea搭建springMvc + mybatis+Maven,环境...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)