时间:2021-07-01 10:21:17 帮助过:5人阅读
pox.xml的东西不要全部复制粘贴,比如工程的名字就不一样,复制dependencies里的就行,
这里说一下,ojdbc6的jar包,因为版权原因,maven不能下载,需要先下载到本地再在引用(非maven的小伙伴可跳过) 友情链接:https://blog.csdn.net/qq_38263715/article/details/80698499
导包成功后,我们进行第二步
二。我的web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 5 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 6 id="WebApp_ID" version="3.1"> 7 8 9 10 <context-param> 11 <param-name>contextConfigLocation</param-name> 12 <param-value>classpath:app.xml</param-value> 13 </context-param> 14 <listener> 15 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 16 </listener> 17 18 <servlet> 19 <servlet-name>springmvc</servlet-name> 20 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 21 <init-param> 22 <param-name>contextConfigLocation</param-name> 23 <param-value>classpath:springmvc.xml</param-value> 24 </init-param> 25 <load-on-startup>1</load-on-startup> 26 </servlet> 27 28 29 <servlet-mapping> 30 <servlet-name>springmvc</servlet-name> 31 <url-pattern>*.do</url-pattern> 32 </servlet-mapping> 33 34 35 36 </web-app>
其实我这里只有两个东西:1是加载app.xml(大多数人写的是applicationContext.xml,我简写了)
2是加载springmvc.xml
下面那个<servlet-mapping>是拦截请求用的,他会拦截所有以.do结尾的url,这个可以自己选,*.什么都可以
既然要加载这两个文件,我们就要写出来,要不然会报错的
三。app.xml
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 9 http://www.springframework.org/schema/mvc 10 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-3.2.xsd 13 http://www.springframework.org/schema/aop 14 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 15 http://www.springframework.org/schema/tx 16 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> 17 18 19 <context:component-scan base-package="com.impl"></context:component-scan> 20 21 22 <context:property-placeholder location="classpath:mysql.properties"/> 23 <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" 24 destroy-method="close"> 25 <property name="driverClassName" value="${jdbc.driverClassName}" /> 26 <property name="url" value="${jdbc.url}" /> 27 <property name="username" value="${jdbc.username}" /> 28 <property name="password" value="${jdbc.password}" /> 29 </bean> 30 31 32 33 34 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 35 <property name="dataSource" ref="dataSource" /> 36 <property name="mapperLocations" value="classpath:resource/userMapper.xml" /> 37 </bean> 38 39 40 41 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 42 <property name="basePackage" value="com.dao"/> 43 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 44 </bean> 45 46 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 47 <property name="dataSource" ref="dataSource"/> 48 </bean> 49 50 </beans>
这里配置了包扫描,连接数据库的,有几处用到了包路径的名字!!仔细改成你的路径(4处)
四。springmvc.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 开启controller注解支持 --> <!-- 注意事项请参考:http://jinnianshilongnian.iteye.com/blog/1762632 --> <!-- 扫描注解的,context是负责扫描的 --> <context:component-scan base-package="com.controller" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> </context:component-scan> <!--使用mvc:annotation-driven代替上边注解映射器和注解适配器 配置 如果使用mvc:annotation-driven就不用配置上面的 RequestMappingHandlerMapping和RequestMappingHandlerAdapter--> <mvc:annotation-driven></mvc:annotation-driven> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--配置jsp路径的前缀 --> <property name="prefix" value="/WEB-INF/view/"/> <!--配置jsp路径的后缀 --> <property name="suffix" value=".jsp"/> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>app/json;charset=UTF-8</value> </list> </property> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <!--json视图拦截器,读取到@ResponseBody的时候去配置它 --> <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> </bean> </beans>
这里配置了controller的扫描和一个试图解析器,就是controller不用@ResponseBody时,返回页面的时候,会用到他,加一个前缀,加一个后缀
下面那俩配完了就允许返回java类的数据
五。这两个xml我们写完了,我们再连接一个数据库
db.properties(oracle)
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=zxm
jdbc.password=zxm
mysql.properties(mysql)
1 jdbc.driverClassName=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/database01useUnicode=true&characterEncoding=UTF8 3 jdbc.username=root 4 jdbc.password=root
我也不打码了,这个数据库,用户名,密码要改成你们的
mysql的我加了一个转码的
六。下一步是controller dao entity service service_impl usermapper.xml
这几个分别是 控制层 dao层 实体类 服务层 实现服务层 和 写语句的表
0.首先建一个实体类(要与数据库里的属性相对应)
1.controller 被。do拦截的页面先来这里(a是我测试用的)
2.然后走service的方法
3.然后impl实现这个接口
4.再去找dao
5.dao里的名字与usermapper.xml的id相对应,去找usermapper.xml
6.至此,后台的东西就写完了,框架也搭